#include <bits/stdc++.h>
using namespace std;
const long long int mod = 1e9 + 7;
int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long int t;
  cin >> t;
  while (t--) {
    long long int n, m;
    cin >> n >> m;
    long long int ans = 0;
    long long int arr[n][m];
    map<long long int, long long int> mp, mp1;
    for (long long int i = 0; i < n; i++) {
      for (long long int j = 0; j < m; j++) {
        cin >> arr[i][j];
        if (arr[i][j] == 0)
          mp[i + j]++;
        else
          mp1[i + j]++;
      }
    }
    long long int x = m + n - 2;
    long long int res = x / 2;
    long long int a = 0, b = 0;
    for (long long int i = 0; 2 * i <= x; i++) {
      if (i == res and (m + n) % 2 == 0) continue;
      a = mp[i] + mp[x - i], b = mp1[i] + mp1[x - i];
      ans += min(a, b);
    }
    cout << ans << '\n';
    mp.clear(), mp1.clear();
  }
  return 0;
}
