#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){ int tmp1; sort(all(t[i])); int tmp = t[i][0].first; t[i][0].first = 0; rep(j, t[i].size() - 1){ if(tmp < t[i][j + 1].first){ t[i][j + 1].first = t[i][j].first + 1; tmp++; }else{ t[i][j + 1].first = t[i][j].first; } } tmp1 = t[i][t[i].size()-1].first + 1; int tmp2; sort(all(t[i]), [](auto x, auto y){return x.second < y.second;}); tmp = t[i][0].second; t[i][0].second = 0; rep(j, t[i].size() - 1){ if(tmp < t[i][j + 1].second){ t[i][j + 1].second = t[i][j].second + 1; tmp++; }else{ t[i][j + 1].second = t[i][j].second; } } tmp2 = t[i][t[i].size()-1].second + 1; mf_graph g(tmp1 + tmp2 + 2); rep(j, tmp1) g.add_edge(tmp1 + tmp2, j, 1); rep(j, tmp2) g.add_edge(j + tmp1, tmp1 + tmp2 + 1, 1); rep(j, t[i].size()){ g.add_edge(t[i][j].first, t[i][j].second + tmp1, 1); } ans += g.flow(tmp1 + tmp2, tmp1 + tmp2 + 1); } } cout << ans << endl; }