#include <bits/stdc++.h>
using namespace std;
const int N = 105;
int a[N], b[N];
int main() {
  ios::sync_with_stdio(false);
  int t;
  cin >> t;
  while (t--) {
    int n, m;
    cin >> n >> m;
    for (int i = 1; i <= n + m; i++) a[i] = b[i] = 0;
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= m; j++) {
        int c;
        cin >> c;
        if ((i + j - 1) > (n + m) / 2) {
          int k = n + m - i - j + 1;
          if (c == 1)
            a[k]++;
          else
            b[k]++;
        } else {
          int k = i + j - 1;
          if (c == 1)
            a[k]++;
          else
            b[k]++;
        }
      }
    int ans = 0;
    for (int i = 1; i <= (n + m - 1) / 2; i++) {
      ans += min(a[i], b[i]);
    }
    cout << ans << endl;
  }
}
