#include <bits/stdc++.h>
using namespace std;
long long int bigmod(long long int a, long long int p, long long int m) {
  if (p == 1) {
    return a % m;
  }
  if (p == 0) {
    return 1;
  }
  if (p % 2 == 0) {
    long long int ans = bigmod(a, p / 2, m);
    ans = (ans % m) * (ans % m);
    return ans % m;
  } else {
    long long int ans = bigmod(a, (p - 1), m);
    ans = (a % m) * (ans % m);
    return ans % m;
  }
}
int arr[35][35], n, m, ans, arr1[35][35];
int main() {
  int t;
  scanf("%d", &t);
  for (int tt = 1; tt <= t; tt++) {
    ans = 0;
    scanf("%d%d", &n, &m);
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= m; j++) {
        scanf("%d", &arr[i][j]);
      }
    }
    int manhattan_dist[100][2];
    memset(manhattan_dist, 0, sizeof(manhattan_dist));
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= m; j++) {
        if (i + j - 2 != m + n - i - j) {
          manhattan_dist[min(i + j - 2, n + m - i - j)][arr[i][j]]++;
        }
      }
    }
    for (int i = 0; i <= n + m - 2; i++) {
      ans += min(manhattan_dist[i][0], manhattan_dist[i][1]);
    }
    cout << ans << endl;
  }
  return 0;
}
