#include <bits/stdc++.h>
using namespace std;
vector<long long> prime;
void simpleSieve(int limit) {
  vector<bool> mark(limit + 1, true);
  prime.push_back(1);
  for (int p = 2; p * p < limit; p++) {
    if (mark[p] == true) {
      for (int i = p * p; i < limit; i += p) mark[i] = false;
    }
  }
  for (int p = 2; p < limit; p++) {
    if (mark[p] == true) {
      prime.push_back(p);
    }
  }
}
long long binpow(long long a, long long b, long long m) {
  a %= m;
  long long res = 1;
  while (b > 0) {
    if (b & 1) res = res * a % m;
    a = a * a % m;
    b >>= 1;
  }
  return res;
}
long long m = 1e9 + 7;
long long MX = 1e5 + 5;
void solve(long long t) {
  long long N, M;
  cin >> N >> M;
  vector<vector<long long>> freq(N + M, vector<long long>(2, 0));
  for (long long i = 0; i < N; i++) {
    for (long long j = 0; j < M; j++) {
      long long a;
      cin >> a;
      if ((i + j) == (N + M - i - j - 2)) continue;
      long long r = min(i + j, N + M - i - j - 2);
      freq[r][a]++;
    }
  }
  long long ans = 0;
  for (long long i = 0; i < N + M; i++) {
    ans += min(freq[i][0], freq[i][1]);
  }
  cout << ans << endl;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long t = 1;
  cin >> t;
  for (long long i = 1; i <= t; i++) {
    solve(i);
  }
  return (0);
}
