#include #include #include #include using namespace std; constexpr int X = 500000; void solve() { int h, w; cin >> h >> w; vector>> ess(X); for (int i = 0; i < h; ++i) { for (int j = 0; j < w; ++j) { int x; cin >> x; if (x > 0) ess[--x].emplace_back(i, j); } } int ans = 0; for (const auto& es : ess) { if (es.empty()) continue; atcoder::mf_graph graph(h + w + 2); int s = h + w, g = s + 1; set is, js; for (auto [i, j] : es) { is.insert(i); js.insert(j); graph.add_edge(i, h + j, 1); } for (auto i : is) graph.add_edge(s, i, 1); for (auto j : js) graph.add_edge(h + j, g, 1); ans += graph.flow(s, g); } cout << ans << "\n"; } int main() { cin.tie(nullptr); ios::sync_with_stdio(false); solve(); return 0; }