#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n, m;
    cin >> n >> m;
    vector<vector<int> > arr(n + m + 1, vector<int>(2, 0));
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= m; j++) {
        int a;
        cin >> a;
        arr[i + j][a]++;
      }
    }
    int ans = 0;
    if ((n + m) % 2 == 0) {
      int s = (n + m - 1) / 2;
      s++;
      for (int i = 2; i <= s; i++) {
        int z = arr[n + m + 2 - i][0] + arr[i][0];
        int o = arr[n + m + 2 - i][1] + arr[i][1];
        ans = ans + min(z, o);
      }
    } else {
      int s = (n + m - 1) / 2;
      s++;
      for (int i = 2; i <= s; i++) {
        int z = arr[n + m + 2 - i][0] + arr[i][0];
        int o = arr[n + m + 2 - i][1] + arr[i][1];
        ans = ans + min(z, o);
      }
    }
    cout << ans << endl;
  }
  return 0;
}
