ORCHARD PARK, N.Y. (AP)Josh Allen and the Buffalo Bills opened the season with Super Bowl aspirations.

One week into the playoffs, their objective remains in sight – but barely.

Allen shrugged off a three-turnover outing, and the three-time defending AFC East champion Bills avoided a major collapse by rallying for a 34-31 victory over the AFC’s seventh-seeded and injury-depleted Miami Dolphins in a wild-card playoff game on Sunday.

In acknowledging he was hardly at his best, Allen said all that mattered was the final score.

”One-week seasons, man, that’s it,” he said. ”All that matters is surviving and advancing. Doesn’t matter how we win, it’s if we win.”

The ”if” part of the equation hung in the balance until the final minutes of a game in which the Bills squandered a 17-0 lead against an opponent that limped into the playoffs by losing five of its final six and was down to its third-string quarterback, rookie Skylar Thompson.

It wasn’t until Allen’s fumble was returned 5 yards for a touchdown by Zach Sieler 61 seconds into the third quarter to put the Dolphins up 24-20 that the Bills’ offense finally reawakened.

Allen threw two touchdown passes over a span of 3:11, with Cole Beasley scoring the go-ahead TD with a 6-yard catch, before Gabe Davis extended the lead to 34-24 with a 23-yard TD reception.

”That’s just playoff football for the most part. All the games I’ve played in the playoffs, I don’t even know if we’ve had a blowout,” Beasley said. ”The thing is you’ve got to keep your foot on the gas. You can’t ever get complacent. You gotta play consistent. We were a little inconsistent tonight. We can’t do that going forward.”

The game wasn’t decided until Buffalo’s defense, which forced six punts and two turnovers, stopped Miami as it turned the ball over on downs on its final possession.

Thompson’s pass on fourth-and-6 went just off the fingertips of tight end Mike Gesicki with 2:22 remaining.

Buffalo ran out the clock when Devin Singletary bulled his way for a 7-yard gain to convert a third-and-7.

It was a sloppy game and a lengthy saga lasting nearly four hours between two division rivals, and the outcome fitting after the two teams split their regular-season series in games decided by a combined five points. The Dolphins beat Buffalo 21-19 at Miami in September, with the Bills rallying to beat Miami 32-29 last month.

Allen finished 23 of 39 for 352 yards and three TDs, but also threw two interceptions, which resulted in the Dolphins scoring 11 points.

Buffalo advances to host the third-seeded Cincinnati Bengals next Sunday in the divisional round. Three weeks earlier, the Bills’ game against the Bengals in Cincinnati was canceled when Buffalo safety Damar Hamlin went into cardiac arrest and needed to be resuscitated on the field. Hamlin is recovering and watched Sunday’s game at home.

The Dolphins played tough in a game they entered as 13 1/2-point underdogs, according to FanDuel Sportsbook, without numerous offensive starters, including quarterback Tua Tagovailoa, who is still in the concussion protocol, and backup Teddy Bridgewater who is dealing with a dislocated pinky finger.

”That was the hand that was dealt, and that was part of the story that we were willing to complete,” coach Mike McDaniel said. ”I’m heartbroken for the guys because they didn’t want their season to end and they did everything they could.”

The Dolphins extended their run of playoff loses to five. Miami’s most recent playoff win was a 23-17 overtime victory over Indianapolis on Dec. 30, 2000 in the wild-card round.

Miami’s defense and special teams made up for an offense that managed just 16 first downs and 231 yards. Jevon Holland and Xavien Howard’s interceptions, along with Cedric Wilson’s 50-yard punt return, led to the Dolphins taking advantage of short fields to tie the game at 17 on Gesicki’s 7-yard catch late in the second quarter.

Making just his third career start, Thompson finished 18 of 45 for 220 yards with a touchdown and two interceptions.

”Man, our defense played so good today. That’s tough. Because those guys gave us every chance to win,” Thompson said. ”Feel like that’s a game we definitely had the opportunity to win. I will take (responsibility) for the loss, and shoulder that, and move forward.”

Buffalo didn’t resemble a team that closed the season reeling off seven straight victories.

Instead, the Bills looked as if they were running out of adrenaline after an emotionally draining week, and a season full of disruptions.

Hamlin was with the team in spirit, live-tweeting during the game from home, where he continues to recover. A message Hamlin posted on his Twitter account about an hour before kickoff read: ”My heart is with my guys as they compete today! … Nothing I want more than to be out there with them.”

The 24-year-old Hamlin was released from a Buffalo hospital on Wednesday and visited with his teammates at the Bills facility on Saturday.

Cornerback Tre’Davious White said Hamlin’s visit ”gave us a lot of juice.” for this game. ”It lifted our spirits for sure,” he added.

NOTABLE OUTINGS

Dolphins: Sieler had two of Miami’s playoff-record seven sacks. Jeff Wilson closed the scoring with a 1-yard run, capping an 11-play, 75-yard drive with 10:53 left. Jason Sanders hit all three field-goal attempts, including a 48-yarder. Tyreek Hill led Miami with seven catches for 69 yards.

Bills: LB Matt Milano, fresh off being selected as an All-Pro, had two sacks. Dawson Knox also scored on a 6-yard catch for Buffalo, while James Cook scored on 12-yard run. Bass hit both field-goal attempts, including a 39-yarder. WR Stefon Diggs had seven catches for 114 yards, and Davis finished with six catches for 113.

UP NEXT

Bills: Host Cincinnati next Sunday at 3 p.m. EST.

AP NFL: https://apnews.com/hub/nfl and https://twitter.com/AP-NFL