#pragma GCC optimize("Ofast") #include using namespace std; typedef long long int ll; typedef unsigned long long ull; mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); ll myRand(ll B) { return (ull)rng() % B; } int main(){ cin.tie(nullptr); ios::sync_with_stdio(false); int n,m; cin >> n >> m; vector> a(n,vector(m)); map>> mp; for(int i=0;i> a[i][j]; mp[a[i][j]].push_back({i,j}); } } int res=0; vector v(n),vv(m); vector c(n),d(m); for(auto p:mp){ if(p.first==0)continue; fill(v.begin(), v.end(),0); fill(vv.begin(), vv.end(),0); fill(c.begin(), c.end(),0); fill(d.begin(), d.end(),0); for(auto pp:p.second){ v[pp.first]++; vv[pp.second]++; } priority_queue>> pq; for(auto pp:p.second){ pq.push(make_pair(v[pp.first]+vv[pp.second]-1,pp)); } while(pq.size()){ auto pp=pq.top(); pq.pop(); if(c[pp.second.first] or d[pp.second.second])continue; if(pp.first!=v[pp.second.first]+vv[pp.second.second]-1){ pq.push(make_pair(v[pp.second.first]+vv[pp.second.second]-1,pp.second)); continue; } if(v[pp.second.first]>=vv[pp.second.second]){ c[pp.second.first]=1; res++; vv[pp.second.second]--; } else{ d[pp.second.second]=1; res++; v[pp.second.first]--; } } } cout << res << endl; }