#include using namespace std; #include #ifdef _RUTHEN #include #else #define show(...) true #endif using ll = long long; #define rep(i, n) for (int i = 0; i < (n); i++) template using V = vector; int main() { ios::sync_with_stdio(false); cin.tie(0); int H, W; cin >> H >> W; V> A(H, V(W)); rep(i, H) rep(j, W) cin >> A[i][j]; map mph[H], mpw[W]; atcoder::mf_graph G(H * W * 2 + 2); int s = H * W * 2, t = s + 1; rep(i, H) { rep(j, W) { if (A[i][j] == 0) continue; if (mph[i].count(A[i][j]) == 0) { mph[i][A[i][j]] = i * W + j; G.add_edge(s, i * W + j, 1); } if (mpw[j].count(A[i][j]) == 0) { mpw[j][A[i][j]] = i * W + j; G.add_edge(i * W + j + H * W, t, 1); } G.add_edge(mph[i][A[i][j]], mpw[j][A[i][j]] + H * W, 1); } } int ans = G.flow(s, t); cout << ans << '\n'; return 0; }