By KAWEEWIT KAEWJINDA and STEPHEN WRIGHT , Associated Press

MAE SAI, Thailand (AP) — A daring rescue mission in the treacherous confines of a flooded cave in northern Thailand has saved all 12 boys and their soccer coach who were trapped deep within the labyrinth, ending a grueling 18-day ordeal that claimed the life of an experienced volunteer diver and riveted people around the world.

Thailand's Navy SEALs, who were central to the rescue effort, said on their Facebook page that the remaining four boys and their 25-year-old coach were all brought out safely Tuesday. Eight of the boys were rescued by a team of Thai and international divers on Sunday and Monday.

"We are not sure if this is a miracle, a science, or what. All the thirteen Wild Boars are now out of the cave," the SEALs said, referring to the name of the boys' soccer team. "Everyone is safe."

They said they were waiting for a medic and three SEALs who had stayed with the boys in their dark refuge deep inside the cave complex to come out.

Cheers erupted at a local government office where dozens of volunteers and journalists were awaiting news of whether the intricate and high-risk rescue mission had succeeded. Helicopters taking the boys to a hospital roared overhead.

People on the street cheered and clapped when ambulances ferrying the boys arrived at the hospital in Chiang Rai city.

Payap Maiming, 40, who helped provide food and necessities to rescue workers and journalists, said a "miracle" had happened.

"I'm happy for Thais all over the country, for the people of Mae Sai, and actually just everyone in the world because every news channel has presented this story and this is what we have been waiting for," she said. Mae Sai is the district where the cave is located, in the northern part of Chiang Rai province, near the border with Myanmar.

"It's really a miracle," Payap said. "It's hope and faith that has brought us this success."

The plight of the boys and their coach has captivated Thailand and much of the world — from the heart-sinking news that they were missing to the first flickering video of the huddle of anxious yet smiling boys when they were found 10 days later by a pair of British divers. They were trapped in the Tham Luan Nang Non cave on June 23, when they were exploring it after a soccer practice and it became flooded by monsoon rains.

Each of the boys, ages 11 to 16 and with no diving experience, was guided out by a pair of divers in three days of intricate and high-stakes operations. The route, in some places just a crawl space, had oxygen canisters positioned at regular intervals to refresh each team's air supply.

Highlighting the dangers, a former Thai navy SEAL died Friday while replenishing the canisters.

Cave diving experts had warned it was potentially too risky to dive the youngsters out.

But Thai officials, acutely aware that the boys could be trapped for months by monsoon rains that would swell waters in the cave system, seized a window of opportunity provided by relatively mild weather. A massive water pumping effort also made the winding cave more navigable. The confidence of the diving team, and expertise specific to the cave, grew after its first successful mission.

Thai Prime Minister Prayuth Chan-ocha, speaking before the final rescue was completed, said the boys were given an anti-anxiety medication to help with their perilous removal from the cave.

Asked at a weekly press conference Tuesday in Bangkok if the boys had been sedated, Prayuth said: "Who would chloroform them? If they're chloroformed, how could they come out? It's called anxiolytic, something to make them not excited, not stressed."

Prayuth said the Tham Luan Nang Non cave would be closed for some time to make it safe for visitors.

The eight boys brought out by divers on Sunday and Monday were doing well and were in good spirits, a senior health official said. There were given a treat Tuesday: bread with chocolate spread that they'd requested.

Jedsada Chokdumrongsuk, permanent secretary at the Public Health Ministry, said the first four boys rescued were able to eat normal food, though they couldn't yet take the spicy dishes favored by many Thais.

Two of the boys possibly have a lung infection but all eight are generally "healthy and smiling," he said.

"The kids are footballers so they have high immune systems," Jedsada said. "Everyone is in high spirits and are happy to get out. But we will have a psychiatrist to evaluate them."

It could be at least seven days before they can be released from the hospital, Jedsada told a news conference.

Family members have seen at least some of the boys from behind a glass isolation barrier.

It was clear doctors were taking a cautious approach. Jedsada said they were uncertain what type of infections the boys could face "because we have never experienced this kind of issue from a deep cave."

If medical tests show no dangers, after another two days, parents will be able to enter the isolation area dressed in sterilized clothing and staying 2 meters (yards) away from the boys, said Tosthep Bunthong, a public health official.

John Tangkitcharoenthawon, a local village chairman who was working as a volunteer translator for the tourist police, was bursting with happiness at the successful rescue.

"If this place had a roof, the morale has gone straight through it," he said.

President Donald Trump joined those paying tribute to the rescuers.

"On behalf of the United States, congratulations to the Thai Navy SEALs and all on the successful rescue of the 12 boys and their coach from the treacherous cave in Thailand," he tweeted.

He added: "Such a beautiful moment - all freed, great job!"

One of soccer's most popular teams, Manchester United, expressed its relief over the rescue and invited the boys and their coach, as well as those who saved them, to come see them play on their home ground this season.

A message posted on the English Premier League club's Twitter account said: "Our thoughts and prayers are with those affected. We would love to welcome the team from Wild Boars Football Club and their rescuers to Old Trafford this coming season."

The international soccer federation, FIFA, had already invited the boys to attend the World Cup final in Russia this Sunday. However, doctors treating the boys said it would be too soon for them to make the trip.