#include <bits/stdc++.h>
const int maxn = 1e6 + 20;
const int maxm = 1e5 + 10;
const long long int mod = 998244353;
const long long int INF = 1e18 + 100;
const int inf = 0x3f3f3f3f;
const double pi = acos(-1.0);
const double eps = 1e-8;
using namespace std;
int a[50][50];
int num[200], c[200];
int main() {
  int _;
  for (scanf("%d", &_); _; _--) {
    memset(num, 0, sizeof num);
    memset(c, 0, sizeof c);
    memset(a, 0, sizeof a);
    int n, m;
    scanf("%d %d", &n, &m);
    for (int i = 0; i < n; i++)
      for (int j = 0; j < m; j++) scanf("%d", &a[i][j]);
    if (n == 2 && m == 2) {
      if (a[0][0] != a[1][1])
        puts("1");
      else
        puts("0");
    } else {
      n--;
      m--;
      for (int i = 0; i <= n; i++) {
        for (int j = 0; j <= m; j++) {
          c[i + j]++;
          num[i + j] += a[i][j];
        }
      }
      int ans = 0;
      if ((n + m) % 2 == 1) {
        for (int i = 0; i < (n + m) / 2; i++) {
          ans += min(num[i] + num[n + m - i],
                     (c[i] + c[n + m - i]) - (num[i] + num[n + m - i]));
        }
        int i = (n + m) / 2;
        ans += min(num[i] + num[n + m - i],
                   (c[i] + c[n + m - i]) - (num[i] + num[n + m - i]));
      } else {
        for (int i = 0; i < (n + m) / 2; i++) {
          ans += min(num[i] + num[n + m - i],
                     (c[i] + c[n + m - i]) - (num[i] + num[n + m - i]));
        }
      }
      printf("%d\n", ans);
    }
  }
}
