#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; using ll = long long; constexpr int INF = 1001001001; constexpr int mod = 1000000007; // constexpr int mod = 998244353; template inline bool chmax(T& x, T y){ if(x < y){ x = y; return true; } return false; } template inline bool chmin(T& x, T y){ if(x > y){ x = y; return true; } return false; } struct HopcroftKarp { vector> graph; vector dist, match; vector used, visited; HopcroftKarp(int n, int m) : graph(n), match(m, -1), used(n) {} void add_edge(int u, int v){ graph[u].emplace_back(v); } void bfs(){ dist.assign(graph.size(), -1); queue que; for(int i = 0; i < (int)graph.size(); ++i){ if(!used[i]){ que.emplace(i); dist[i] = 0; } } while(!que.empty()){ int from = que.front(); que.pop(); for(auto& to : graph[from]){ int v = match[to]; if(v >= 0 && dist[v] == -1){ dist[v] = dist[from] + 1; que.emplace(v); } } } } bool dfs(int s){ visited[s] = true; for(auto& u : graph[s]){ int v = match[u]; if(v < 0 || (!visited[v] && dist[v] == dist[s] + 1 && dfs(v))){ match[u] = s; used[s] = true; return true; } } return false; } int bipartite_matching(){ int ret = 0; while(true){ bfs(); visited.assign(graph.size(), false); int flow = 0; for(int i = 0; i < (int)graph.size(); ++i){ if(!used[i] && dfs(i)) ++flow; } if(flow == 0) return ret; ret += flow; } } void output(){ for(int i = 0; i < (int)match.size(); ++i){ if(~match[i]){ cout << match[i] << '-' << i << '\n'; } } } }; int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); int H, W; cin >> H >> W; vector A(H, vector(W)); vector vals; for(int i = 0; i < H; ++i){ for(int j = 0; j < W; ++j){ cin >> A[i][j]; if(A[i][j]) vals.emplace_back(A[i][j]); } } sort(begin(vals), end(vals)); vals.erase(unique(begin(vals), end(vals)), end(vals)); int sz = vals.size(); vector>> ps(sz); for(int i = 0; i < H; ++i){ for(int j = 0; j < W; ++j){ if(!A[i][j]) continue; int k = lower_bound(begin(vals), end(vals), A[i][j]) - begin(vals); ps[k].emplace_back(i, j); } } int ans = 0; for(int i = 0; i < sz; ++i){ if(ps[i].size() <= 1) { ans += 1; continue; } HopcroftKarp g(H, W); for(auto& [x, y] : ps[i]) g.add_edge(x, y); ans += g.bipartite_matching(); } cout << ans << endl; return 0; }