#include <bits/stdc++.h>
const long long INF = 2147483647;
const long long MOD = 1000000007;
const long long mod = 998244353;
const long double eps = 1e-12;
using namespace std;
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long n, m;
  long long arr[31][31];
  long long mid;
  long long cnt_0[61];
  long long cnt_1[61];
  long long res;
  long long T;
  cin >> T;
  while (T--) {
    cin >> n >> m;
    for (long long i = 0; i <= 60; i++) {
      cnt_0[i] = 0;
      cnt_1[i] = 0;
    }
    for (long long i = 0; i < n; i++) {
      for (long long j = 0; j < m; j++) {
        cin >> arr[i][j];
        if (arr[i][j] == 0) {
          cnt_0[i + j]++;
        } else {
          cnt_1[i + j]++;
        }
      }
    }
    mid = (n + m - 1) / 2;
    res = 0;
    for (long long i = 0; i < mid; i++) {
      res +=
          min(cnt_0[i] + cnt_0[n + m - 2 - i], cnt_1[i] + cnt_1[n + m - 2 - i]);
    }
    cout << res << endl;
  }
  return 0;
}
