Originally published in The New York Times on February 10, 2015

GONTOR, Indonesia — The silhouette of the large mosque, brick-like but for a bulbous dome, looked blurry in the downpour. The rain of East Java is heavy, lending a sparkle to the green paddies and the scent of moist earth to the air. As the evening prayer ended, hundreds of boys rushed out of the building in waves, mats slung over their shoulders, sarongs hitched up to their knees, flip-flops squelching in the wet.

These students attend Pondok Modern Darussalam Gontor, one of Indonesia’s many Islamic boarding schools, or pesantren. (Estimates range from about 13,000 to 30,000.) Almost three-quarters of the schools, including Gontor, teach secular subjects like science and history in addition to classical Islamic texts and vocational courses in agriculture and mechanics.

Pesantren have existed for centuries in Indonesia, which is home to the world’s largest Muslim population. But their reputation has taken a battering in recent decades, thanks to a wave of terrorist attacks, including bombings in Bali in 2002 that killed over 200 people. By December 2014, militias in Syria and Iraq, including the radical Sunni Islamist group that calls itself the Islamic State, had attracted some 100 recruits from Indonesia, according to the country’s counterterrorism force.

Some pesantren are indeed linked to terrorist groups. The most notorious is the Al Mukmin school in Ngruki, a short drive from Gontor, some of whose graduates have been associated with the Jemaah Islamiyah, the organization responsible for the Bali bombings. Yet Sidney Jones, director of the Jakarta-based Institute for Policy Analysis of Conflict (I.P.A.C.) and a leading authority on terrorist movements in Southeast Asia, estimates that only around 40 pesantren have terrorist connections. Another 200 or so emphasize an orthodox Wahhabi philosophy but do not preach violence.

According to Ms. Jones, many more pesantren focus on grooming “upstanding citizens in a way that reinforces their own local settings and values.” Known for strict discipline and character-building, they may actually serve as a bulwark against extremism.

On my visit to the school in November, I was chaperoned by Ustad Aliy, a recent alumnus and young teacher. Wearing spectacles, a pinstriped shirt and tie, he played Bollywood hits in the car on our way in from the nearest airport. He was not paid for his work, he said, but was content with room and board because of the sense of community he felt at Gontor.

Mr. Aliy was vague about what he taught — “some English, and science, and religion” — offering a first hint that academic rigor was not the school’s strong suit. At a public-speaking exam I attended later, young men stood around a courtyard, each with an assessor, shouting out random sentences that were barely audible in the general din. Even some of the older students struggled to communicate with me in basic English, despite having studied the language for years.

But many of Gontor’s pupils come from poor and remote parts of Indonesia, and they cannot afford a better education elsewhere. The regional mix within the student body has a valuable leveling effect in a country marked by deep inequalities.

The children here also learn independence, frugality and egalitarianism. They sleep 30 to a room and have few possessions save a mattress, some clothes, toiletries and books. School days run from 4:30 a.m. until 9 p.m. Discipline is tight. Students take turns serving meals in the dining hall and staffing the supply shop. So-called courts convene every evening to hand out punishments for infractions like littering or violating the dress code. And though Gontor’s response to Islamism isn’t entirely coherent, the school is consistent in encouraging respect for religious diversity and tolerance.

Dihyatun Masqoon Ahmad, one of Gontor’s most senior teachers, told me with pride that the school was nonpartisan on theological matters, and served as a meeting point “for all groups.” Mostly he meant that Gontor, unlike most pesantren, is not exclusively associated with either of Indonesia’s rival Islamic organizations, the traditionalist Nahdlatul Ulama and the modernist Muhammadiyah, which together have tens of millions of members and often provide education and health care in rural areas where government services are lacking.

Mr. Aliy admitted, after I asked, that the beliefs of Shiites and Ahmadiyya, two Muslim minorities that represent less than 1 percent of Indonesia’s population and are considered to be apostate by the mainstream, are not discussed at the school. But no hostility against them is preached either. And Gontor engages with competing beliefs outside Islam, hosting Christian leaders and encouraging research in interfaith studies.

Robin Bush, a political scientist and expert on Islamic politics in Indonesia, argues that many Islamic boarding schools, including Gontor, foster a culture of debate by introducing students to the four major schools of Sunni jurisprudence. “The concept of the contestation of ideas emerges naturally in pesantren,” Ms. Bush told me.

In fact, some researchers have concluded that secular universities, not religious schools, are the main breeding grounds of Islamist radicalism in Indonesia. Ms. Jones, of I.P.A.C., pointed out that most suspected terrorists arrested in recent years have come from state schools, rather than Islamic ones.

One explanation, according to Mark Woodward, Inayah Rohmaniyah and other scholars of religion in Indonesia, is that the informal and secretive extremist Quranic study groups known as pengajian tertutup flourish more readily in the relatively lax and unsupervised environments of secular universities and village mosques.

Students unfamiliar with the intricacies of their own faith can be swayed by arguments that seem to call for jihad when taken out of context. Pesantren graduates, who have been schooled in the Quran and the Hadith, are better able to spot scriptural distortions. They also tend to be more connected to their communities, and their stable sense of identity, religious and otherwise, shields them against radicalism. Pesantren are allies, not suspects, in Indonesia’s fight against Islamism.