#include <bits/stdc++.h>
const long long int mod = 1e9 + 7;
using namespace std;
long long int gcd(long long int a, long long int b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
bool IsPowerOfTwo(int x) { return (x != 0) && ((x & (x - 1)) == 0); }
long long int powm(int a, int b) {
  long long int res = 1;
  while (b) {
    if (b & 1) res = (res * 1LL * a) % mod;
    b >>= 1;
    a = (a * 1LL * a) % mod;
  }
  return res;
}
long long int query(long long int i, long long int j) {
  cout << "? " << i << ' ' << j << endl;
  int x;
  cin >> x;
  return x;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int t;
  cin >> t;
  while (t--) {
    int n, m;
    cin >> n >> m;
    int a[n][m];
    int ans = 0;
    int k = m - 1 + n - 1;
    int tot = m + n;
    vector<pair<int, int>> v;
    v.resize(tot + 1);
    for (int i = 0; i < v.size(); i++) {
      v[i].first = 0;
      v[i].second = 0;
    }
    for (int(i) = 0; (i) < (n); ++(i))
      for (int(j) = 0; (j) < (m); ++(j)) {
        cin >> a[i][j];
        if (a[i][j] == 0) {
          v[i + j].first++;
        } else
          v[i + j].second++;
      }
    int d = v.size();
    int end = n + m - 3;
    for (int i = 1; i < end; i++) {
      int one = v[i].second;
      one += v[end].second;
      int zero = v[i].first;
      zero += v[end].first;
      end--;
      ans += min(zero, one);
    }
    if (a[0][0] != a[n - 1][m - 1]) ans++;
    cout << ans << endl;
  }
  return 0;
}
