It was the bad old days of 1974, the South Bronx, and NYPD Detective Ralph Friedman and his partner, Robert De Matas, were working undercover, posing as cab drivers.

They watched a group of teens ask a man, Joaquin Castro, 37, for money. Castro turned them down.

“The kid called him a cheap bastard, so [Castro] pulls out a gun and shoots him right in front of us — his chest explodes,” Friedman recalled.

The cops sprinted after Castro along John Street, the killer firing at Friedman as he fled. De Matas tried to outflank him. Castro jumped behind cars and reloaded.

“I stop by a stoop, so I’m covered,” Friedman said. “I shoot at him. He shoots at me, I duck. He turns and shoots at my partner. Then he turns and shoots at me. Then he reloads again. I go to a second gun. I’m lining him up in my sights like I’m shooting at paper, and I’m like, ‘Why isn’t this guy going down?’

“My partner fires one round, and the guy grabs his shoulder. He goes down. I run up and am standing right over him, and he starts to lift his hand up, and I shoot him right in the head. That did it. He dropped the gun. His brains fell out onto the street. And when they get him downtown, would you believe I’d hit him like nine times — in a good grouping right around the belly?”

Friedman was given the Combat Cross, the department’s second-highest honor, for his actions.

For that battle, and the three other men he killed when they tried to murder him in the line of duty, Friedman gave himself something else, a tattoo on his right trigger finger that reads:

“Justified 4X.”

Friedman , now 66, is the most decorated detective in the history of the NYPD.

He has been in 15 gun battles and shot eight perps, including the four he killed. He collected 219 NYPD awards and 36 civilian honors, while piling up more than 2,000 arrests, 105 off duty. He has been stabbed, broke his hand twice, fractured his skull, and was smashed over the head with a tire iron.

He has other ink on his chest for his years at The Bronx’s notorious 41st Precinct during the city’s most violent era. “Fort Apache ’70-’75,” it reads, using the precinct station’s old nickname, popularized by the 1981 film “Fort Apache, The Bronx.” Another tattoo on his back reads, “The rush was worth the risk.”

Friedman once cut short a double date with his brother Stu, a decorated transit cop, because he suspected youths outside a deli were up to no good. The brothers hid and watched in the snow for 30 minutes. When one kid pulled a shotgun, the two arrested all three suspects before they could rob the place or fire a shot.

“The ’70s were a different animal,” said Friedman, a Bronx-born DeWitt Clinton High School grad and massive bodybuilder who could once bench press 400 pounds. “You had to be combat ready.”

He had no friends or family on the force — his dad was a hotel manager — but when two pals asked if he wanted to take the police test with them, he agreed. After he passed, he ditched his furniture-moving job to join the NYPD in 1968 as a trainee handling calls for the city’s new 911 system. When Friedman hit the streets in 1970 — the same year as future Commissioner Ray Kelly — he couldn’t wait to get started. “I wanted the action,” he said.

He came to the right place. Fort Apache was a festival of murder, arson, drug dealing, robbery, burglaries and car theft. “It was probably the most dangerous neighborhood in the world,” he said.

Cops in the 41st — like Richie Biller, who once dragged two suspects into the station house with a knife still in his shoulder — would “come in with a collar every day. These were the guys I looked up to. They made me want to be like them,” Friedman said.

His first arrest was of two suspects he saw passing money and slips with numbers. When he hauled them in, a supervisor said, “Hey, kid. Do you even know what you got here?” He had no clue. “You got a KG, a known gambler.”

His first shooting came in June 1971. His girlfriend called, saying she and her mom were shopping on Jerome Avenue and got into a dispute with two guys in a truck over a parking space. The men came after him with tire irons. “I’m amazed. I got a gun in my hand, and they’re swinging at me,” Friedman said. “I didn’t want to shoot.”

He blocked one swing with his gun, which broke his hand, then got clubbed on the head from behind, fracturing his skull. Friedman shot the suspect in front of him. The bullet ripped through the perp’s neck, into his arm and out.

The second suspect was about to hit him again with the iron when another cop intervened.

“We all went to the same hospital,” Friedman said. “Everybody recovered, even the guy I shot.”

Eight weeks later, Friedman was back at work. “My first day, we get a burglary run.” Someone had broken into a store. While cops rushed in, the thief came out on the roof and jumped down to the street. “He landed right in front of me. I punched him right in the face. He goes down. Right away, I knew I broke my hand again. First day back and I’m in a cast and out of work,” he said.

But he soon returned to Fort Apache’s plainclothes unit. In 1972, he and partner Kalman Unger arrived at an apartment where a resident, Charles Williams, was “beating up his girlfriend.”

Just as they entered, Williams “jumps out of the back and starts firing at us. We were three feet away. We all open fire. Six shots apiece, 18 bullets. Smoke everywhere. My partner’s hit seven times, once in the heart,” he said.

“The guy runs into me. I grabbed him with my left, and he ran into the gun, right on his nipple. I fired. The bullet killed him. We rush my partner to the hospital. He needed 72 pints of blood in three hours. But he lived.” Somehow, Friedman was not hit.

After that, he made sure to arm himself with a second gun — his “detective’s special,” a snub-nosed Colt .38 revolver strapped to his ankle. Paired with his NYPD-issued Smith & Wesson revolver, he had 12 rounds at the ready.

There were two other fatal gunfights, including a rooftop scrum in the 52nd Precinct in 1977 when he and his boss, Detective Sgt. Steve Cantor, traded shots with weapons dealer Hector Nuñez, who was armed with a Winchester .30-.30 rifle. After they hit and wounded Nuñez, a second man came after Friedman with a knife. The cop put him down with a shot to the head.

Friedman was more than an extraordinary gunfighter. He made headlines for dozens of drug collars and 15 bribery arrests. He foiled crooks wherever he went. While looking at holsters in a police-supply store, he nailed a shoplifter who had pocketed badges. At a baseball game, he arrested a guy for mugging a spectator.

It came to an end in 1984, when another cop car rammed his as both raced to aid an officer. The impact shattered his hip and broke 22 other bones. “It knocked the s- -t out of me,” he said.

The accident nearly gave Friedman what he didn’t want: the Medal of Honor, the NYPD’s highest award — given posthumously in most cases. Instead, he spent two months in traction.

At the time, Friedman says, he was dating eight women — and when he woke up in the hospital, he was horrified to see them all at his bedside.

“I saw them and made like I passed out again,” he recalled.

One of them, Grace, “stuck with me” — and became his wife.

Friedman says he misses the job, misses the action. “I’d go back tomorrow,” he said. “I miss it, but I can’t run.”

He retired and recently moved with his wife into a new house in Danbury, Conn., where he rises at 5 a.m. to work on his 185-pound frame in his home gym.

Thirty years later, he still has unpleasant dreams in which he’s shooting at a bad guy but having no effect, or he fires and watches in slow motion as his bullet slowly exits the barrel and drops.

But his “4X” fatalities don’t bother him a lick.

“I was never traumatized,” he said. “I never felt bad. I felt I saved my life or someone else’s life.”