#include #include using namespace std; using namespace atcoder; const long long INF_LL = 2000000000000000000LL; const int INF = 2000000000; const long long MOD = 1000000007; #define ll long long #define all(x) x.begin(), x.end() #define REP(i, a, b) for(int i = a; i < b; i++) #define rep(i, n) REP(i, 0, n) // typedef float double; // typedef priority_queue prique; typedef pair P; typedef vector vi; typedef vector vvi; typedef vector

vp; typedef vector vl; typedef vector matrix; int dx[4] = {0, -1, 0, 1}; int dy[4] = {1, 0, -1, 0}; int sign[2] = {1, -1}; template bool chmax(T &a, T b) { if(a < b) { a = b; return 1; } return 0; } template bool chmin(T &a, T b) { if(a > b) { a = b; return 1; } return 0; } ll modpow(ll a, ll b, ll m) { if(b == 0) return 1; ll t = modpow(a, b / 2, m); if(b & 1) { return (t * t % m) * a % m; } else { return t * t % m; } } struct edge { int to; ll cost; edge(int t, ll c) { to = t, cost = c; } }; typedef vector> graph; // using mint = modint998244353; int main(){ int h, w; cin >> h >> w; vector> a(h, vector(w)); rep(i, h) rep(j, w) cin >> a[i][j]; vector>> t(500000); rep(i, h){ rep(j, w){ if(a[i][j] > 0) t[a[i][j] - 1].push_back({i, j}); } } int ans = 0; for(int i = 499999; i >= 0; i--){ if(t[i].size() > 0) { mf_graph g(h + w + 2); rep(i, h) g.add_edge(h + w, i, 1); rep(i, w) g.add_edge(i + h, h + w + 1, 1); rep(j, t[i].size()){ g.add_edge(t[i][j].first, t[i][j].second + h, 1); } ans += g.flow(h + w, h + w + 1); } } cout << ans << endl; }