#include <bits/stdc++.h>
using namespace std;
void solve() {
  long long n, m;
  cin >> n >> m;
  long long arr[n][m];
  for (long long i = 0; i < n; i++) {
    for (long long j = 0; j < m; j++) {
      cin >> arr[i][j];
    }
  }
  vector<pair<long long, long long> > v;
  long long sr = 0, sc = 0;
  while (sr < n || sc < m) {
    long long oc = 0, zc = 0;
    long long i = min(sr, n - 1), j = min(sc, m - 1);
    while (i >= 0 && j < m) {
      if (arr[i][j] == 1)
        oc++;
      else
        zc++;
      i--;
      j++;
    }
    v.push_back({oc, zc});
    sr++;
    if (sr >= n) sc++;
  }
  long long ans = 0;
  long long a = v.size();
  for (long long i = 0; i < a / 2; i++) {
    long long oc = v[i].first + v[a - i - 1].first;
    long long zc = v[i].second + v[a - i - 1].second;
    ans += min(oc, zc);
  }
  cout << ans << endl;
  return;
}
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  long long t = 1;
  cin >> t;
  while (t--) {
    solve();
  }
}
