#include <bits/stdc++.h>
long long fact[1000007] = {0};
long long exp(long long x, long long y, long long p) {
  long long res = 1;
  while (y) {
    if (y % 2) res = (res * x % p) % p;
    x = (x * x) % p;
    y /= 2;
  }
  return res;
}
long long expo(long long x, long long y) {
  long long res = 1;
  while (y) {
    if (y % 2) res = (res * x % ((long long)1e9 + 7)) % ((long long)1e9 + 7);
    x = (x * x) % ((long long)1e9 + 7);
    y /= 2;
  }
  return res;
}
long long sub(long long a, long long b) {
  return (a % ((long long)1e9 + 7) - b % ((long long)1e9 + 7) +
          ((long long)1e9 + 7)) %
         ((long long)1e9 + 7);
}
long long mul(long long a, long long b) {
  return ((a % ((long long)1e9 + 7)) * (b % ((long long)1e9 + 7)) +
          ((long long)1e9 + 7)) %
         ((long long)1e9 + 7);
}
long long inv(long long x) { return expo(x, ((long long)1e9 + 7) - 2); }
using namespace std;
bool comp(long long a, long long b) {
  if (a > b)
    return true;
  else
    return false;
}
void solve() {
  long long n, m;
  cin >> n >> m;
  long long a[n][m];
  vector<long long> v[n + m];
  for (long long i = 0; i < n; i++)
    for (long long j = 0; j < m; j++) {
      cin >> a[i][j];
      v[i + j].push_back(a[i][j]);
    }
  long long ans = 0;
  long long q = n + m - 2;
  for (long long i = 0; i < (q + 1) / 2; i++) {
    long long k = q - i;
    long long o1 = 0;
    long long z1 = 0;
    long long o2 = 0;
    long long z2 = 0;
    for (auto it : v[i]) {
      if (it)
        o1++;
      else
        z1++;
    }
    for (auto it : v[k]) {
      if (it)
        o2++;
      else
        z2++;
    }
    ans += min(o1 + o2, z1 + z2);
  }
  cout << ans << endl;
}
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
  long long testcases;
  cin >> testcases;
  while (testcases--) solve();
  return 0;
}
