#define LOCAL #define _USE_MATH_DEFINES #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; template ostream& operator <<(ostream& out, const pair& a) { out << "(" << a.first << "," << a.second << ")"; return out; } template ostream& operator <<(ostream& out, const array& a) { out << "["; bool first = true; for (auto& v : a) { out << (first ? "" : ", "); out << v; first = 0;} out << "]"; return out; } template ostream& operator <<(ostream& out, const vector& a) { out << "["; bool first = true; for (auto v : a) { out << (first ? "" : ", "); out << v; first = 0;} out << "]"; return out; } template ostream& operator <<(ostream& out, const set& a) { out << "{"; bool first = true; for (auto& v : a) { out << (first ? "" : ", "); out << v; first = 0;} out << "}"; return out; } template ostream& operator <<(ostream& out, const map& a) { out << "{"; bool first = true; for (auto& p : a) { out << (first ? "" : ", "); out << p.first << ":" << p.second; first = 0;} out << "}"; return out; } #ifdef LOCAL #define trace(...) __f(#__VA_ARGS__, __VA_ARGS__) #else #define trace(...) 42 #endif template void __f(const char* name, Arg1&& arg1){ cerr << name << ": " << arg1 << endl; } template void __f(const char* names, Arg1&& arg1, Args&&... args){ const char* comma = strchr(names + 1, ','); cerr.write(names, comma - names) << ": " << arg1 << " |"; __f(comma + 1, args...); } template auto vect(const T& v, int n) { return vector(n, v); } template auto vect(const T& v, int n, D... m) { return vector(n, vect(v, m...)); } typedef long long int64; typedef pair ii; #define SZ(x) (int)((x).size()) template static constexpr T inf = numeric_limits::max() / 2; const int MOD = 1e9 + 7; // const int MOD = 998244353; mt19937 mrand(random_device{}()); int rnd(int x) { return mrand() % x; } // mt19937_64 mrand(random_device{}()); // int64 rnd(int64 x) { return mrand() % x; } template void out(const vector& a) { for (int i = 0; i < SZ(a); ++i) cout << a[i] << " \n"[i + 1 == SZ(a)]; } template bool ckmin(T& a, const T& b) { return b < a ? a = b, 1 : 0; } template bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; } template void dedup(vector& v) { sort(v.begin(), v.end()); v.erase(unique(v.begin(), v.end()), v.end()); } void add(int& x, int y) { x += y; if (x >= MOD) x -= MOD; } struct fast_ios { fast_ios() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(10); }; } fast_ios_; template struct maxflow { struct Edge { int v, dual; T c; Edge(int v, int dual, T c) : v(v), dual(dual), c(c) {} }; vector a[N]; typename vector::iterator idx[N]; int d[N]; void add_edge(int u, int v, int c, bool bidirection = false) { a[u].emplace_back(v, SZ(a[v]), c); a[v].emplace_back(u, SZ(a[u]) - 1, bidirection ? c : 0); } T augment(int u, int t, T bound) { if (u == t) return bound; T ret = 0; // current arc optimization for (auto& it = idx[u]; it != a[u].end() && ret < bound; ++it) { if (it->c && d[it->v] == d[u] + 1) { T aug = augment(it->v, t, min(it->c, bound - ret)); ret += aug; it->c -= aug; a[it->v][it->dual].c += aug; } } if (ret == 0) d[u] = -1; return ret; } int n; bool path(int s, int t) { fill(d, d + n, -1); d[s] = 0; queue Q; Q.push(s); while (!Q.empty()) { int u = Q.front(); Q.pop(); for (auto& p : a[u]) { if (p.c && d[p.v] == -1) { Q.push(p.v); d[p.v] = d[u] + 1; if (p.v == t) return true; } } } return false; } maxflow(int n) { this->n = n; for (int i = 0; i < n; ++i) a[i].clear(); } T dinic(int s, int t) { T ret = 0; while (path(s, t)) { // current arc optimization for (int i = 0; i < n; ++i) idx[i] = a[i].begin(); while (T aug = augment(s, t, inf)) { ret += aug; } } return ret; } vector reach(int s) { vector Q; Q.push_back(s); vector visit(n); visit[s] = true; for (int k = 0; k < SZ(Q); ++k) { int u = Q[k]; for (auto& p : a[u]) { if (p.c && !visit[p.v]) { visit[p.v] = true; Q.push_back(p.v); } } } return Q; } }; int main() { int n, m; cin >> n >> m; const int K = 5e5 + 10; vector>> pos(K); for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { int x; cin >> x; pos[x].push_back({i, j}); } } int ret = 0; for (int k = K - 1; k > 0; --k) { if (SZ(pos[k]) == 0) continue; map row, col; for (auto& [x, y] : pos[k]) { if (!row.count(x)) row[x] = SZ(row); if (!col.count(y)) col[y] = SZ(col); } const int N = 1e3 + 10; maxflow flow(SZ(row) + SZ(col) + 2); int s = SZ(row) + SZ(col), t = s + 1; for (int i = 0; i < SZ(row); ++i) flow.add_edge(s, i, 1); for (int i = 0; i < SZ(col); ++i) flow.add_edge(SZ(row) + i, t, 1); for (auto [x, y] : pos[k]) { flow.add_edge(row[x], SZ(row) + col[y], 1); } ret += flow.dinic(s, t); } cout << ret << '\n'; return 0; }