#include #include #include #define rep(i, n) for(i = 0; i < n; i++) using namespace std; int h, w; int a[500][500]; set rows[500001]; set cols[500001]; signed main() { int i, j; cin >> h >> w; rep(i, h) { rep(j, w) { cin >> a[i][j]; rows[a[i][j]].insert(i); cols[a[i][j]].insert(j); } } int jyokai = 0; rep(i, 500001) { if (i == 0) continue; jyokai += min(rows[i].size(), cols[i].size()); } cout << jyokai << endl; return 0; }