#include <bits/stdc++.h>
using namespace std;
int n, m;
int mat[31][31];
void solve() {
  int ans = 0;
  int ones[n + m - 1], zeros[n + m - 1];
  memset(ones, 0, sizeof(ones));
  memset(zeros, 0, sizeof(zeros));
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      cin >> mat[i][j];
      if (!mat[i][j])
        zeros[i + j]++;
      else
        ones[i + j]++;
    }
  }
  int v = (n + m - 2) / 2;
  for (int i = 0; i < v + 1; i++) {
    if (v != i || ((m + n) % 2 != 0)) {
      int a = ones[i] + ones[n + m - 2 - i];
      int b = zeros[i] + zeros[n + m - 2 - i];
      ans += min(a, b);
    }
  }
  cout << ans << "\n";
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int t;
  cin >> t;
  while (t--) {
    cin >> n >> m;
    solve();
  }
}
