#include <bits/stdc++.h>
using namespace std;
int main() {
  long int t;
  cin >> t;
  while (t--) {
    long int n, m, i, j, k, c = 0, l, count = 0, s = 0;
    cin >> n >> m;
    long int a[n][m];
    for (i = 0; i < n; i++) {
      for (j = 0; j < m; j++) cin >> a[i][j];
    }
    for (i = 0; i < (n + m - 1) / 2; i++) {
      s = 0;
      c = 0;
      for (l = 0; l < n; l++) {
        for (k = 0; k < m; k++) {
          if (l + k == i || l + k == n + m - 2 - i) {
            if (a[l][k] == 0) c++;
            s++;
          }
        }
      }
      count += min(c, s - c);
    }
    cout << count << "\n";
  }
  return 0;
}
