#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 5, M = 35;
int n, m, x;
int g[M][M], tot, v[1000][2];
int main() {
  int T;
  cin >> T;
  while (T--) {
    cin >> n >> m;
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= m; j++) {
        scanf("%d", &g[i][j]);
      }
    }
    int ans = 0;
    memset(v, 0, sizeof(v));
    tot = (n + m - 1) / 2;
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= m; j++) {
        int k = i + j - 1;
        if (k <= tot) {
          v[k][g[i][j]]++;
        } else if ((m + n - k) <= tot) {
          v[(m + n - k)][g[i][j]]++;
        }
      }
    }
    for (int i = 1; i <= tot; i++) {
      ans += min(v[i][0], v[i][1]);
    }
    cout << ans << endl;
  }
}
