#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
const int INF = 0x3f3f3f3f;
int a[110][110];
int dp[100][2];
const int M = 1e5 + 10;
int vis[M], prime[M];
void prim() {
  memset(vis, 0, sizeof(vis));
  memset(prime, 0, sizeof(prime));
  int cun = 1;
  for (int i = 2; i <= M; i++) {
    if (vis[i] == 0) {
      prime[cun++] = i;
    }
    for (int j = 1; j <= cun && prime[j] * i < M; j++) {
      vis[i * prime[j]] = 1;
      if (i % prime[j] == 0) {
        break;
      }
    }
  }
}
int main() {
  int t;
  cin >> t;
  int sum = 0;
  prim();
  while (t--) {
    long long n, m;
    cin >> n >> m;
    int temp = n + m - 2;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) scanf("%d", &a[i][j]);
    }
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) a[i][j] += i - i + j - j;
    }
    for (int i = 0; i <= (temp / 2); i++) {
      dp[i][0] = 0;
      dp[i][1] = 0;
    }
    if (temp % 2) {
      for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
          int fl = i + j;
          if (fl <= temp / 2)
            dp[fl][a[i][j]]++;
          else
            dp[temp - fl][a[i][j]]++;
        }
      }
      int ans = 0;
      for (int i = 0; i <= temp / 2; i++) {
        ans += min(dp[i][0], dp[i][1]);
      }
      cout << ans << endl;
    } else {
      int ans = 0;
      for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
          int fl = i + j;
          if (fl == temp / 2) continue;
          if (fl < temp / 2)
            dp[fl][a[i][j]]++;
          else {
            dp[temp - fl][a[i][j]]++;
          }
        }
      }
      for (int i = 0; i <= (temp / 2 - 1); i++) {
        ans += min(dp[i][0], dp[i][1]);
      }
      cout << ans << endl;
    }
  }
  return 0;
}
