Who wrote “Good Will Hunting”? Bernard Cohen says the idea was his.

Cohen, MIT class of ’62, is a lifelong Upper West Sider who is an artist and writer, as well as a waiter, bartender, chauffeur and movie extra.

He says he was hanging out at Columbus Bakery (83rd Street and Columbus Avenue) in the mid-’90s when he met aspiring producer Chris Moore and told him about his idea for a movie about a janitor who is also a math genius. Cohen said he got the idea from a gifted frat brother who came to MIT at the age of 16.

“I thought [Moore] was someone else and started talking to him,” Cohen, who has kept his secret for 20 years, told me. “I asked him, ‘Do you know someone younger who could help me finish it?’”

Moore was a Harvard classmate of Matt Damon’s, who showed up at the bakery a few days later and met with Cohen.

“I didn’t have anything in writing. It was all verbal. I didn’t even ask for a part,” said Cohen. “But I said, ‘When it wins Best Original Screenplay, I want a thank you, and I want you to finance my next film.’”

As soon as childhood friends Damon and Ben Affleck won the Oscar for the screenplay in 1998, skeptics scoffed that the two neophytes could possibly have written such a polished work.

Ace screenwriter William Goldman — in his book “Which Lie Did I Tell?” — wrote, “In my obit it will say that I wrote it. People don’t want to think those two cute guys wrote it . . . but I did not write [‘Good Will Hunting’], alas.”

The fact that Damon and Affleck didn’t write much else in the following 20-plus years — concentrating on acting — has helped sow doubt.

Cohen said he contacted Moore after the Oscars. “Moore acted like he didn’t know anything and told me not to call him again.”

“I was double-crossed,” Cohen said. “You can’t do it the way I did it, obviously. I figured I was such a ball of fire it didn’t matter what I gave away.”

A spokesman for Damon said, “Matt is unavailable. He is currently out of the country on an extended holiday with his family.”

This article originally appeared in Page Six.