#include <bits/stdc++.h>
using namespace std;
long long int x[1000001], y[1000001], n, mx, a, b;
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long int t;
  cin >> t;
  while (t--) {
    long long int n, m, ans = 0;
    cin >> n >> m;
    long long int a[100] = {}, b[100] = {};
    for (long long int i = 0; i < n; i++) {
      for (long long int j = 0; j < m; j++) {
        long long int x;
        cin >> x;
        if (x == 0) {
          a[i + j]++;
        } else {
          b[i + j]++;
        }
      }
    }
    long long int k = n + m - 2;
    for (long long int i = 0; i < k; i++, k--) {
      ans += min(a[i] + a[k], b[i] + b[k]);
    }
    cout << ans << "\n";
    ans = 0;
  }
}
