Several dozen Occupy Wall Street demonstrators were sleeping on the cushioned pews of a United Methodist church on the Upper West Side on Thursday morning when one of them spotted a man in plainclothes wandering through the sanctuary, apparently counting heads.

Flustered, the demonstrator confronted the man, who after several moments identified himself as a plainclothes detective. The demonstrator called the pastor, who was sleeping next door. But by then, the detective had left, along with his partner, who had been asking questions at a homeless shelter in the church’s basement.

Several area churches have been sheltering protesters in the days since the city banned sleeping in Zuccotti Park, and church officials said they were alarmed by the idea that the police might be monitoring their conduct.

“It is disconcerting that they would actually enter the sanctuary,” said the Rev. James Karpen, known as Reverend K, senior pastor of the United Methodist Church of St. Paul and St. Andrew, on West 86th Street. “Here we had offered hospitality and safety, which is our business as a church; it just felt invasive.”