#include <bits/stdc++.h>
using namespace std;
const int Max = 2e6 + 10;
const int Mod = 1e9 + 7;
const double PI = 3.141592653589793238463;
long long n, m;
int main() {
  (ios_base::sync_with_stdio(false), cin.tie(NULL));
  ;
  long long i, j, p, sum = 0, k, t, a, b, c, d, cnt = 0, q, l, r;
  string str;
  bool flag = true;
  cin >> t;
  while (t--) {
    cin >> n >> m;
    long long ara[n + 5][m + 2];
    map<pair<long long, long long>, long long> mp;
    for (i = 1; i <= n; i++) {
      for (j = 1; j <= m; j++) {
        cin >> ara[i][j];
        a = i - 1 + j - 1;
        mp[{a, ara[i][j]}]++;
      }
    }
    n = n + m - 2;
    long long ans = 0, one, zero;
    for (i = 0; i <= (n - 1) / 2; i++) {
      one = mp[{i, 1}] + mp[{n - i, 1}];
      zero = mp[{i, 0}] + mp[{n - i, 0}];
      ans += min(one, zero);
    }
    cout << ans << endl;
  }
}
