#include <bits/stdc++.h>
using namespace std;
int n, m, tam;
int get(int i, int j) {
  int pos = i + j + 1;
  if (pos <= tam / 2) return pos;
  return tam - pos + 1;
}
void solvetask() {
  cin >> n >> m;
  tam = n + m - 1;
  int a[n][m];
  vector<int> ida(n + m), volta(n + m), cnt(n + m);
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      cin >> a[i][j];
      int pos = get(i, j);
      if ((tam & 1) && pos == (tam + 1) / 2) continue;
      if (i + j + 1 <= tam / 2) {
        ida[pos] += a[i][j];
        cnt[pos]++;
      } else {
        volta[pos] += a[i][j];
        cnt[pos]++;
      }
    }
  }
  int ans = 0;
  for (int i = 1; i <= tam / 2; i++) {
    ans += min(ida[i] + volta[i], cnt[i] - ida[i] - volta[i]);
  }
  cout << ans << '\n';
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int t = 1;
  cin >> t;
  while (t--) solvetask();
}
