Brazil's notoriously overcrowded prisons have become the battleground in the escalating war between the country's two biggest drug gangs. So far this year, 140 prisoners have died in a series of bloody prison massacres – many of them brutally beheaded and butchered.

Brazil drugs gangs prison riots
Brazil drugs gangs prison riots
Brazil drugs gangs prison riots
Brazil drugs gangs prison riots
Brazil drugs gangs prison riots

For two decades, the two gangs – the Sao Paulo-based Primeiro Comando da Capital (First Capital Command or PCC) and the Comando Vermelho (Red Command) based in Rio de Janeiro – maintained a working relationship, ensuring a steady flow of drugs and arms over Brazil's porous borders. But about six months ago, the PCC began trying to muscle the Red Command out of key drug routes. In response, the Red Command allied itself with local gangs, enlisting them to take on the PCC.

The killings began on 1 January, when the powerful North Family gang, an ally of the Red Command, killed 56 inmates at a prison in Amazonas state, mostly PCC members. The North Family controls a lucrative cocaine route along the Solimoes, a branch of the Amazon that flows from Colombia and Peru, the world's top two cocaine-producing nations.

Brazil drugs gangs prison riots
5 January 2017: Police investigators and forensic technicians investigate a crime scene where a man was killed in Manaus after more than 100 inmates escaped from a prison during a riot Ueslei Marcelino/Reuters

The PCC retaliated on 6 January by killing 33 inmates at the Monte Cristo prison in the neighbouring state of Roraima. This was followed by a riot in which 26 members of a rival gang were savagely murdered at the Alcacuz prison in Rio Grande do Norte state. Mobile phone videos of the clashes circulated widely on social media, some showing gang members chopping the heads off their rivals, slicing their hearts out and disemboweling them.

A SWAT team entered the Alcacuz prison to transfer over 200 prisoners belonging to the local gang. Police said they searched cells and confiscated several guns, knives, homemade weapons and bullet-resistant vests. Major Eduardo Franco, a spokesman for state police, said the prison was back under control.

However, footage broadcast live on Globo TV showed prisoners from both the PCC and the local gang on the roofs of their respective wings, waving banners with gang insignia and wielding knives, clubs and other homemade weapons. Scores of prisoners were seen running free within the prison – clearly indicating that security officials did not have control of the prison as they had claimed.

The prison bears the scars of the violence that has raged within it. Holes can be seen in the walls and rooftops of buildings within the complex. Even before the recent riots, the prison was commonly called "Swiss cheese" because it was built on sand dunes, and prisoners over the years have managed to tunnel out.

Security have now installed an improvised wall of metal shipping containers inside the prison in a desperate attempt to keep control of Alcacuz.

The series of riots and grisly killings has put a spotlight on overcrowding, underfunding and understaffing in the prison system. For instance, Alcacuz is home to more than 1,000 inmates, though it was built for 620. The bloodshed has mostly played out among inmates allied with the rival gangs, but security experts fear it will soon spill into Brazil's already violent city streets.

Brazil is the world's biggest consumer of cocaine after the US, according to United Nations data. In the vast state of Amazonas, the North Family gang has for years dominated the smuggling of cocaine that is shipped to Europe or sold in Brazil's inner cities in a business believed to be worth £3.6 billion ($4.5bn) a year.

President Michel Temer has vowed to improve military surveillance along the border. The military plans to increase its patrols on the Solimoes River, one of the main smuggling routes, and share intelligence with officials in neighbouring Colombia and Peru.

However, senior commanders acknowledge drugs and arms will continue to flow in. Security agencies fighting drugs and arms trafficking in this isolated swath of the jungle are severely stretched. Amazonas needs an extra 7,000 civil and military police to keep up with the increase in drug activity, according an internal report by the state security secretary.

"We are 30 officers overseeing an area the size of France," said Marcos Vinicius Menezes, the federal police chief in Tabatinga, a city washed by the Solimoes that borders Colombia and Peru.