#include <bits/stdc++.h>
using namespace std;
const long long maxn = 510;
const long long mod = 1e9 + 7;
const long long inf = 1e18 + 10;
long long n, m;
long long a[55][55];
long long dis(long long i, long long j, long long x, long long y) {
  return (abs(i - x) + abs(j - y));
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long q;
  cin >> q;
  while (q--) {
    cin >> n >> m;
    vector<long long> ve[110];
    for (long long i = 1; i <= n; i++)
      for (long long j = 1; j <= m; j++) {
        cin >> a[i][j];
        long long x = dis(i, j, 1, 1), y = dis(i, j, n, m);
        if (x == y) continue;
        ve[min(x, y)].push_back(a[i][j]);
      }
    long long ans = 0;
    for (long long i = 0; i <= 105; i++) {
      long long s0 = 0, s1 = 0;
      for (auto x : ve[i])
        if (x == 1)
          s1++;
        else
          s0++;
      ans += min(s0, s1);
    }
    cout << ans << "\n";
  }
}
