They say solitary confinement is a prison within a prison. If so, Matthew Hamm just engineered one of the more impressive prison breaks of our time.

He left no knotted bedsheets, no tunnels dug by soup spoon. Rather, Mr. Hamm sprung himself and two other men from Edmonton Institution's notorious "hole" using a judicial writ dating back to the time of the Magna Carta and legal knowledge sharpened over years bouncing among the country's federal prisons.

Earlier this month, Alberta Court of Queen's Bench Justice J.B. Veit largely agreed with Mr. Hamm's legal argument that the Correctional Service of Canada unlawfully placed him and three other inmates in solitary confinement, and ordered their immediate release to a less restrictive ward.

Story continues below advertisement

It was a rare victory for a self-represented inmate and raises questions about how a 37-year-old convict with multiple mental-health diagnoses managed to out-flank the $2.5-billion-a-year prison agency. The answer rests with a judge's patience and Mr. Hamm's determination to overcome personal and institutional hurdles.

"I do have serious mental-health issues, but I'm high-functioning," Mr. Hamm said by phone from prison, where he is now in a mental-health ward.

"One day I spoke in court for five straight hours. At times, I probably sounded manic to the judge, but she always allowed me to continue."

He had learned the details of habeas corpus law – the medieval writ stating "no man shall be arrested or imprisoned … except by the lawful judgment of his peers or by the law of the land" – two years earlier, when a Saskatchewan prison bumped him from a mental-health ward to segregation. Today, the writ allows a judicial review of prison actions that further erode an inmate's residual liberties. Before Mr. Hamm could argue against the transfer before a judge, the prison removed him from solitary, rendering his application moot.

On Jun. 28, 2016, he got a chance for another legal challenge when guards moved him and several other inmates from a mental-health unit to an isolation unit, assuring them it was an unofficial and temporary admission to segregation.

"They started off giving us extra time out," he said, "but by the second week, we were locked up 23 hours a day. We were in the hole. We said, 'This is bullcrap, we're in here for no reason.'"

Soon, a reason emerged. Officials claimed a confidential informant had come forward saying Mr. Hamm and four others were planning to attack several guards.

Story continues below advertisement

The inmates knew the information was bogus. The informant recanted his story in writing, noting he had concocted the allegations because staff had offered to transfer him from segregation and provide him a television.

"They made him an offer he couldn't refuse, I guess," Mr. Hamm said. "We showed this evidence [to staff]. They didn't seem to care."

With only two months remaining until his sentence ends in the fall, he opted to battle the prison in court, despite the potential for repercussions.

After he made his legal filings, his life became harder. Correctional officers confiscated a bag of canteen food stored in his cell. When they returned it eight days later, he opened it to discover he was no longer alone in the isolation cell.

"There were maggots everywhere," he said. "They scattered all over the cell and I had to walk around killing them by hand with toilet paper. I finally got them all, but the floor was sticky with dead maggot juice."

At other times, his court filings were held up at the prison, a complaint noted by the judge. Five days before the first court hearing, guards took away Mr. Hamm's clothing, forced him to dress in a smock and placed him on suicide watch, another action noted by the judge. Mr. Hamm said he did nothing to warrant the invasive attention.

Story continues below advertisement

The hearing stretched to three-and-a-half days, during which Mr. Hamm argued his segregation was unlawful on several grounds: the confidential informant was unreliable, prison authorities failed to follow the principles of procedural fairness and fundamental justice by withholding information the inmates needed to counter the accusations, and the prison authorities failed to consider the mental health and aboriginal background of the inmates.

Mr. Hamm was sitting in his segregation cell when the judge's decision was delivered on the morning of Aug. 10, a day known as Prisoners' Justice Day in jails across the country. By then, he had spent nearly half his life incarcerated for various non-violent offences. Judgments had rarely gone his way. This day would be different.

"It took a while to really sink in," he said of reading the 44-page decision. "It was so thorough and so scathing towards the institution. She agreed with every single one of my arguments."

In short, the judge called the prison's actions unreasonable and ordered officials to release the men from segregation immediately. A CSC spokeswoman said the agency is reviewing the decision. "We will not be able to offer comment until that process is complete," Lori Halfper said.

"My fight is not done," Mr. Hamm declared over the phone. "Now I have to file in federal court to have these allegations stripped from our files. Getting out of solitary is just a Band-Aid on a festering open wound. Not everyone has the ability to bring an action like this or the motivation. I'm doing it for them."