A homeless man admitted to Suffolk County police homicide detectives that he killed another homeless man last month, prosecutors said Tuesday.

Thomas Lamartina, 30, pleaded not guilty to second-degree murder during his arraignment in Central Islip. Judge G. Ann Spelman called it a “hideous charge” and ordered Lamartina held without bail.

The victim, 65-year-old Nicholas MacQueen, was found dead outside Emanuel Lutheran School on June 28.

Prosecutors alleged that Lamartina repeatedly struck MacQueen in the head with a brick, causing his skull to become “unhinged.”

Police first began investigating Lamartina after a dispute with his girlfriend. She called police and told them about the alleged murder, said his Hauppague-based lawyer, Craig E. McElwee.

McElwee said he still didn’t have all the information on the case, but was told that Lamartina made a statement to police, “apparently on video.” Prosecutors said it was an admission.

Lamartina was also accused of harassing his girlfriend.

McElwee said Lamartina had just recently became homeless.