#include using namespace std; #define all(x) (x).begin(),(x).end() #define rep(i, n) for (int i = 0; i < (n); i++) #define endl "\n" typedef long long ll; typedef pair pii; typedef pair pll; template ostream &operator<<(ostream &os, const vector &vec) {os << "["; for (const auto &v : vec) {os << v << ","; } os << "]"; return os;} template ostream &operator<<(ostream &os, const pair &p) {os << "(" << p.first << ", " << p.second << ")"; return os;} void solve() { int H, W; cin >> H >> W; constexpr int size = 500001; vector> A(H, vector(W)); vector> X(size); for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { cin >> A[i][j]; X[A[i][j]].push_back({i, j}); } } int ans = 0; for (int a = 1; a < size; a++) { vector cntH(H), cntW(W); for (int k = 0; k < X[a].size(); k++) { int i = X[a][k].first, j = X[a][k].second; cntH[i]++; cntW[j]++; } int cH = 0, cW = 0; for (int i = 0; i < H; i++) { if (cntH[i]) cH++; } for (int j = 0; j < W; j++) { if (cntW[j]) cW++; } ans += min(cH, cW); } cout << ans << endl; } int main() { #ifdef LOCAL_ENV cin.exceptions(ios::failbit); #endif cin.tie(0); ios::sync_with_stdio(false); cout.setf(ios::fixed); cout.precision(16); solve(); }