Cablevision’s AMC Networks and Charlie Ergen’s Dish Network have ended their nasty legal feud, The Post has learned.

The two companies today have agreed to a settlement, with Ergen agreeing to paying $700 million in cash and return AMC Networks to Dish’s 14 million customers, sources said.

The judge agreed to the settlement this afternoon, according to sources familiar with the agreement.

The two sides have been in a long-running dispute after Dish predecessor, EchoStar, backed out of a 15-year agreement to carry HD programming created by AMC’s predecessor, Rainbow Media, back in 2006.

When EchoStar reneged on the deal, AMC, then a unit of Cablevision, filed a $2.4 billion lawsuit.

The decision will see AMC and its hugely popular show, “The Walking Dead,” back on Dish today, with episode two of the new season, according to a joint release from the companies.

Dish also gains some of Cablevision’s spectrum as part of its agreement and gives up its 20 percent stake in Voom, the HD programming venture.

Cablevision and AMC Networks are to divide the $700 million.