The extraordinary effort to rescue 12 Thai boys and their football coach ended in success last night (NZ time) when the last of the trapped group emerged from a cave.
The news was greeted with jubilation from rescuers, officials, volunteers and from people watching on around the world.
It was an amazing end to a mission many said was impossible: To rescue a group of children trapped 4km underground and teach them to dive their way out of flooded caves and tunnels, while all the time the threat of rain meant the situation in the Tham Luang Nang Non cave system could have got far worse.
Nineteen divers helped escort the final five members of the group out of Tham Luang cave, in the toughest rescue operation of the past few days.
The youngest of the group, aged just 11, was one of the last to reach the surface.
The coach left the cave after the boys, followed by a doctor and three Thai Navy SEALs who stayed with the boys overnight.
Rescue commander Narongsak Osottanakorn said it was "mission complete" in a press conference at 9.40pm (2.40am Wednesday NZST), confirming that the doctor and Navy SEALs were out of the cave and in good health, to huge cheers.
The four boys rescued on Tuesday have now been transported by ambulance and helicopter to a hospital in Chiang Rai province in northern Thailand, where they will join their teammates.
Narongsak said parents of the rescued boys would be able to see the children tonight, albeit through glass because they are still in quarantine.
He hailed military diver Saman Gunan, who died on Friday, as the "hero of Tham Luang cave".
The Chiang Rai governor said he had never seen such co-operation as had been witnessed over the past heartstopping 17 days, adding that it boded well for Thailand's development.
"We did something nobody thought possible," he said.
"This was mission possible".
The navy posted on their Facebook page: "We are not sure if this is a miracle, a science, or what. All the 13 Wild Boars are now out of the cave."
The 11th boy came to the surface at 5.15pm local time (10.15pm NZST), the tenth at around 4.30pm local time (9.30pm NZST) and the ninth just minutes earlier at 4.06pm.
The first four boys emerged on Sunday, followed by another four on Monday.
The Australian's Amanda Hodge has reported that Adelaide anaesthetist Richard Harris has continued to play a central role in the rescue, diving each day all the way to where the boys were trapped to check their condition before clearing them to dive.
"He only leaves the cave when the last boy is rescued each day," she said.
It's an extraordinary end to a terrifying mission many said was impossible. Rescuers saved a group of children trapped almost 4km underground and taught them to dive their way out of flooded caves and tunnels.
The constant threat of rain meant the situation in the Tham Luang Nang Non cave system could have got far worse.
And yet, the impossible was achieved.
There must have been dark times when the 12 members of the Wild Boars soccer team, holed up in the dank and pitch black surroundings of the caves, wondered if they would ever be found. Or if the steadily rising waters would slowly, but ever so surely, engulf them all.
Yet, 17 days after they first entered the notorious natural tunnel system in northern Thailand, after battling loneliness, cold and the intimidating task of learning how to dive, the outcome no one dared to dream of has become reality — all the boys and their coach are back out in the open, seemingly only slightly worse for wear given their ordeal.
Earlier on Tuesday, the Seals posted a message that said: "Tonight, all the Wild Boars will be reunited."
Chiang Rai province governor Narongsak said: "We expect that everybody will be out today, the children and coach and everybody will be out today."
However, Narongsak sounded a note of caution, telling reporters that the third mission was more perilous than the previous two. Not four, but nine people needed to make it out this time: four boys, the coach, a doctor and three Navy Seals.
The death of military diver Saman Kunan on Friday only underscored the huge risks the boys faced.
But out they came, one by one. At 4.06pm local time (9.06pm NZST) the ninth boy emerged, followed around half an hour later by the tenth and then at 5.15pm by the 11th. The 12th emerged later. The team's coach came last.
Aged between 11 and 16, the BBC has named the people trapped in the caves as:
— Chain Vibulrungruang (known as Titan), 11
— Panamas Sangdee (known as Mig) 13
— Duganpet Promtep (known as Dom), 13
— Adul Sam-on, 14
— Somepong Jaiwong (known as Pong), 13
— Mongkol Booneiam (known as Mark), 14
— Nattawut Takamrong (known as Tern), 14
— Peerapat Sompiangjai (known as Night), 17
— Ekarat Wongsukchan (known as Bew), 14
— Prajak Sutham (known as Note), 15
— Pipat Pho (known as Nick), 15
— Pornchai Kamluang (known as Tee), 16
— Assistant coach Ekapol Chantawong (known as Ake), 25
The 12 schoolboys' awful adventure began uneventfully enough. All part of a soccer team called the Wild Boars, the trip into the caves was apparently a "rite of passage". But it quickly turned into a nightmare.
Some of the local boys knew the Tham Luang caves, Thailand's longest, and had explored them. But they were notorious nonetheless, the challenging landscape within including slippery rocks with razor-sharp edges and cliffs with stark drop-offs shrouded in darkness
Ake, the young coach, was said to be keeping the boys on a strict training schedule to keep them fit and bond the teammates.
On June 23, the group set off on a 45-minute bike ride from their school, close to the border town of Mae Sai, to the cave.
It wasn't until 7pm that night, when the team failed to return, that the Wild Boars' 37-year-old head coach, Nopparat Kathawong, realised something had gone terribly wrong.
NOWHERE TO BE SEEN
He retraced their steps and to his horror found the boys' bikes and bags close to the entrance. The team were nowhere to be seen.
"I screamed — 'Ek! Ek! Ek!' My body went completely cold," he told the Washington Post.
Heavy rain had filled the tunnels with water and cut off the team's exit route. They had no choice but to keep forging ahead, along elevated slopes, where they found a dry ledge in a claustrophobic chamber almost 4km into the cave, which would ultimately make their rescue even more treacherous, leaving them stranded for days.
That triggered an international rescue operation and on July 2, more than a week after they went missing, two divers found the group alive, huddled on the 10sq m ledge deep inside the mountain.
Ekapol, a former monk, reportedly led the boys in group meditation to calm their nerves. But it's likely that for much of that week, the boys would have been in pitch darkness with no idea if they would ever be found.
HOW TO GET TO THE TEAM
Initially, the idea of the boys retracing their steps through the now flooded cave was firmly in the category of last resort. Rescuers investigated whether there was another route or a way in could be drilled through the hard rock.
There was even a suggestion the boys and their coach might have to remain in place for months, until the monsoon rains subsided and the system emptied of water.
While the Thai authorities, now supplemented by an international cast of rescue experts and divers — including from Australia — decided how to proceed, the boys were sent supplies to keep them warm, fed and entertained.
They passed the time writing letters to their parents, which were ferried to the outside in exhausting 11-hour round-trips by tireless foreign dive teams.
In one, Duangpetch "Dom" Phromthep, wrote; "I'm fine but it's a little bit cold. Don't worry and don't forget my birthday."
Another, Ekarat "Bew" Wongsukchan, simply penned: "Mum and Dad, don't worry that I [am] missing for two weeks. I will help you at the shop soon."
But crunch time was coming. A break in the clouds was welcome but should the rains return, it could easily see water levels in the cave engulf the stranded team, no matter how much they tried to pump the tide out.
The massively risky decision was taken to lead the boys, none of who could dive, few of who could swim, through the water to the surface.
Rescuers spent days teaching them basic diving and breathing skills. To make things easier, they would have full face masks attached during the swim, not the usual regulator, which is easy to knock off which could lead the novice divers to panic.
They would be strapped to a leading "buddy" diver, who would carry two oxygen tanks and share air with them.
A second diver was to follow the pair through the cold water and airless chambers that have already claimed one life.
A cave explorer who has been inside the Tham Luang cave complex described it a "labyrinth", adding it was much more difficult to navigate than any he had experienced.
The first, nearly 1km long section from where the boys had been huddling in darkness, is believed to be the most difficult, requiring a long dive and crawling through mud and debris.
The most difficult part of the trek, known as the "choke point", was a crevice just 38cm wide.
Close to where the boys were marooned, the divers and their charge would head beneath the water and ascend as the walls closed in around them to little more than the width of a ruler. The children would have to swim alone, the space was so tight.
"The hole is really small, I have to take off my air tank to crawl through it," a 25-year-old Thai Navy Seal who declined to be named, told Reuters before the first rescue attempt. "As I do, I feel the edges of the hole on both my back and chest."
But it was a risk the multinational team felt they had no choice but to take.
Thirteen divers entered the cave at 10am Sunday local time (1pm AEST) — some heading straight for the trapped group and others taking up stations along the string of flooded chambers.
Ten rescuers headed to the boys in chamber nine and to the junction at chamber six, while the others headed to support positions shortly afterwards.
Each boy wore a scuba mask, wetsuit, boots and a helmet as they followed a rope through the dark and perilous passageways.
Around 2.4 km from where they began, they reached a rescue base, a key milestone on their journey to freedom.
Upon leaving the cave, each boy was taken by ambulance or helicopter to a hospital in Chiang Rai, the regional capital.
In a press conference at the hospital on Tuesday, a health official said all of the boys are suffering ailments including fevers, mild coughs and low heart rates. Two of the boys may have a lung infection, one of the chief concerns given the amount of time they have spent underground in damp conditions.
In addition to lung infections, there are fears the boys may have developed pneumonia, leptospirosis — a bacterial disease that affects humans and animals — and melioidosis, a potentially-fatal disease found in contaminated soil. It's also likely they will be tested for hypothermia.
The official said the second group will be able to see their parents, but they will have to communicate behind the glass wall of a quarantined hospital room.
The first four who were rescued have been able to move on to soft food.
Their chief request? What would a bunch of teenagers want after a fortnight underground but chocolate? They deserve many, many bars.