#include using namespace std; #include using namespace atcoder; using mint=modint998244353; //1000000007; using ll=long long; using pp=pair; #define sr string #define vc vector #define fi first #define se second #define rep(i,n) for(int i=0;i<(int)n;i++) #define pb push_back #define all(v) v.begin(),v.end() #define pque priority_queue #define bpc(a) __builtin_popcount(a) int main(){ int h,w;cin>>h>>w; vc v(5e5+1,vc(0)); rep(i,h)rep(j,w){ int a;cin>>a; if(a)v[a].pb({i,j}); } int ans=0; for(auto vx:v){ if(!vx.size())continue; int n=vx.size(); unordered_maphm,wm; int ch=0,cw=0; for(auto [a,b]:vx)hm[a]=0,wm[b]=0; for(auto& [a,b]:hm)b=ch++; for(auto&[a,b]:wm)b=cw++; mf_graphg(ch+cw+2); int sv=ch+cw; int gv=sv+1; for(auto [a,b]:vx)g.add_edge(hm[a],ch+wm[b],2); rep(i,ch)g.add_edge(sv,i,1); rep(i,cw)g.add_edge(ch+i,gv,1); int res=g.flow(sv,gv); ans+=res; //cout<