#include <bits/stdc++.h>   using namespace std;   const long long mod = 1e9 + 7;   long long dx[] = {-1, 0, 1, 0};   long long dy[] = {0, -1, 0, 1};   vector<long long> p(10000005, -1);   void f(long long n = 10000005) {     p[2] = 2;     for (long long i = 4; i < n; i += 2) {       p[i] = 2;     }     for (long long i = 3; i < n; i += 2) {       if (p[i] == -1) {         p[i] = i;         for (long long j = i * i; j < n; j += i) {           if (p[j] == -1) p[j] = i;         }       }     }   }   void find(long long x, vector<long long>& v) {     while (x != 1) {       v.push_back(p[x]);       x /= p[x];     }   }   void solve() {     long long n, m;     cin >> n >> m;     vector<vector<long long>> v(n, vector<long long>(m));     map<long long, vector<long long>> z;     for (long long i = 0; i < n; i++) {       for (long long j = 0; j < m; j++) {         cin >> v[i][j];         z[i + j].push_back(v[i][j]);       }     }     long long f = 0, r = n + m - 2;     long long ans = 0;     while (f < r) {       map<long long, long long> p;       for (auto x : z[f]) p[x]++;       for (auto x : z[r]) p[x]++;       long long t = 0, mx = 0;       for (auto x : p) {         t += x.second;         mx = max(mx, x.second);       }       ans += t - mx;       f++;       r--;     }     cout << ans <<   n ;   }   int32_t main() {     ios::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     long long t;     cin >> t;     while (t--) {       solve();     }   }   