#include <bits/stdc++.h>
using namespace std;
int main() {
  long long t;
  cin >> t;
  while (t--) {
    long long n, m;
    cin >> n >> m;
    vector<vector<long long>> a(n, vector<long long>(m, 0));
    for (int i = (0); i < (n); i++) {
      for (int j = (0); j < (m); j++) {
        cin >> a[i][j];
      }
    }
    vector<map<long long, long long>> len(n + m - 1);
    for (int i = (0); i < (n); i++) {
      for (int j = (0); j < (m); j++) {
        long long p1 = i + j;
        long long p2 = n - 1 - i + m - 1 - j;
        long long p = min(p1, p2);
        len[p][a[i][j]]++;
      }
    }
    long long tot = n + m - 2;
    long long ans = 0;
    for (int i = (0); i < (len.size()); i++) {
      long long mn = -1;
      if (tot % 2 == 0 && i == tot / 2) continue;
      if (len[i].empty()) continue;
      long long cnt = 0;
      for (auto el : len[i]) {
        cnt += el.second;
        mn = max(mn, el.second);
      }
      ans += cnt - mn;
    }
    cout << ans << endl;
  }
}
