#include <iostream> #include <vector> #include <atcoder/maxflow> #include <utility> #include <map> using namespace std; using namespace atcoder; int a[510][510]; map<int,int> mp1[510]; map<int,int> mp2[510]; int main(){ int i,j,h,w; cin >> h >> w; int now = 0; for(i=0;i<h;i++){ for(j=0;j<w;j++){ cin >> a[i][j]; if(a[i][j]==0) continue; if(mp1[i].find(a[i][j])==mp1[i].end()){ mp1[i][a[i][j]] = now; now++; } if(mp2[j].find(a[i][j])==mp2[j].end()){ mp2[j][a[i][j]] = now; now++; } } } mf_graph<int> g(now + 2); int s = now,t = now + 1; for(i=0;i<h;i++){ for(auto x:mp1[i]) g.add_edge(s,x.second,1); } for(j=0;j<w;j++){ for(auto x:mp2[j]) g.add_edge(x.second,t,1); } for(i=0;i<h;i++){ for(j=0;j<w;j++){ if(a[i][j]==0) continue; g.add_edge(mp1[i][a[i][j]],mp2[j][a[i][j]],1); } } cout << g.flow(s,t) << endl; }