Two married geezers from the Upper West Side kept countless junkies stoned for years by turning the hubby’s medical office into a “pill mill” for prescription narcotics, authorities charged Tuesday.

Dr. Rogelio Lucas, 77, and his office-manager wife, Lydia, 79, allegedly made millions by peddling so many crooked prescriptions that the overflow crowds in their waiting room forced them to relocate three times.

Lucas, who earned his medical degree in the Philippines, treated the elderly and Medicaid patients before turning to the far more lucrative racket of drug dealing in 2009, Special Narcotics Prosecutor Bridget Brennan said.

He allegedly worked with several major drug rings to write more than 23,600 prescriptions for 3 million-plus tablets of the highly addictive painkiller oxycodone worth more than $77 million on the streets.

Between 45 and 50 people who served as “runners” for the drug rings would routinely pick up prescriptions each day, then get them filled at local pharmacies and turn over the pills to the dealers, authorities said.

Lydia served as “the brains behind the operation,” scheduling appointments, maintaining files and collecting $120 in cash for each bogus script, Manhattan prosecutor Ryan Sakacs said.

The couple allegedly deposited about $500,000 in cash into various bank accounts between June 2013 and July 2014, and investigators seized more than $600,000 in cash from a home they have in Scarsdale.

The couple lives in a West 95th Street apartment, and also has other homes in Hawaii and Florida, and are building a place in the Philippines, authorities said.

They pleaded not guilty to conspiracy and drug charges in Manhattan Criminal Court, where they were ordered held on $500,000 bond each and led off in handcuffs.

Defense lawyer Liam Malanaphy said their family, which includes eight kids, would “scramble to come up with some bail” to get them sprung.

Residents of 215 W. 101st St., where Rogelio kept his latest office, said there were signs his practice was shady.

“He had all these handwritten notes all over his door that said ‘Please do not wait outside the door,’” one female tenant said.

“I thought if my doctor had notes like that, I wouldn’t want to see him.”

Tenant Stephen Greenberg, 68, also said he complained to the cops because Rogelio would “send the doorman on frequent errands,” leaving the building unprotected.

“He was nasty, and I hope he serves some serious time,” Greenberg said.

Additional reporting by Lorena Mongelli