SEI MENCIRIM, Indonesia -- The slim boys in Muslim caps and robes at the Al Hidayah Islamic boarding school are grinning bolts of energy who love football, need a little coaxing to do their math and Quran lessons assiduously and aspire to become policemen or respected preachers.

Their school, like many in rural Indonesia, started as a modest affair with a dusty yard, spartan sleeping quarters and an open-air classroom with a dirt floor and corrugated iron roofing. The boys, though, have been spoken to roughly by villagers, the school's banners and billboards trampled and burned, and its head teacher reported to police.

The 20 pupils are the sons of Islamic militants, reviled by most Indonesians for killings and other acts of violence that they justified with distorted interpretations of Islam. Nearly half of the boys' fathers were killed in police raids, and in some cases the children witnessed the deaths. Most of the other fathers are in prison for terrorism offenses.

Get Breaking News Delivered to Your Inbox

Al Hidayah's founder, Khairul Ghazali, is a former radical preacher whose involvement in militancy stretches back decades. Nowadays, the soft-spoken 52-year-old professes to be a changed man who wants to atone by preventing his young charges, who were ostracized and taunted at mainstream schools, from becoming the next generation of Indonesian jihadists. His three sons attend the school.

Former radical preacher Khairul Ghazali, top, teaches at Al Hidayah Islamic Boarding School in Sei Mencirim, North Sumatra, Indonesia, July 22, 2017. AP

A turning point, he said, came in 2010 when anti-terror police raided his home and shot dead two other militants, wanted for killing police officers, in front of him, his wife and children. In prison, he dwelt on his decades of jihad and in the hours spent poring over the Quran found his past wanting. He has written several books against radicalism.

"It's hurt our innocent children. It's hurt us," said Ghazali, who was released in 2015 after serving four years for offenses that included a major bank robbery to fund attacks.

Ghazali's school in North Sumatra is supported by counterterrorism officials but is only a small dent in a largely undiscussed problem. By his reckoning, there are at least 2,000 sons and daughters of killed and imprisoned militants at risk of becoming battle fodder for a new wave of jihadism.

ISIS' declaration of a caliphate over swaths of territory it temporarily held in Iraq and Syria, and more recently the occupation of the southern Philippine city of Marawi by ISIS sympathizers, has provided a psychological boost to militant networks in Indonesia that had been atomized by a sustained crackdown. As the group's territory in the Middle East shrinks, officials fear Indonesians who fought there, or in Marawi, will return.

Sitting in a classroom just after dawn with students whose ages range from 9 to 15, Ghazali tells stories about the life of the Prophet Muhammad to show them, he said, that Islam is a religion of love and mercy, not an ideology to justify a war against police, currently the most frequent target of militant attacks in Indonesia.

Abdullah, 13, and his two younger brothers were sent by their mother to Ghazali's boarding school last year because of the hostility they faced at their regular school.

"I can't stand the taunts at school," he said, his lips trembling. "I dropped out when I was in the third grade and I had to move from place to place. I was insulted as a terrorist kid when my father was in prison. I was so sad."

Abdullah said his favorite activities at school are football and Arabic lessons. He aspires to become an Islamic teacher because "there are many people who claim to know Islam but actually they don't know what Islam is and how to practice it."

The initial hostility the school faced when it opened in 2015 has faded. Local police talked to villagers, raising awareness about its purpose. A stream of officials from the district and provincial governments and the military visited to show their support.

Another school that can cater to 100 students has been set up with the National Agency for Counter-Terrorism's assistance in Lamongan, the East Java hometown of three brothers who assembled a massive bomb used in the 2002 Bali attack.

Suhardi Alius, chief of the counterterrorism agency, describes Ghazali as "totally changed" and taking the right course by drawing on his experience to prevent others from taking up violence.

"The state should support him because people become radical not within a day or two, but it takes a long process, so to deradicalize them we also need a long process," he said.