#include <bits/stdc++.h>
using namespace std;
long long int dia(vector<vector<long long int> > &a, long long int i1) {
  long long int n = a.size(), m = a[0].size();
  long long int ans = 0;
  if (m > n) {
    long long int i = 0, j = i1;
    while (i < n && j >= 0) {
      if (a[i][j] == 1) {
        ans++;
      }
      i++;
      j--;
    }
    i = n - 1;
    j = m - i1 - 1;
    while (i >= 0 && j < m) {
      if (a[i][j] == 1) {
        ans++;
      }
      i--;
      j++;
    }
  } else if (n >= m) {
    long long int j = 0, i = i1;
    while ((i >= 0 && j < m)) {
      if (a[i][j] == 1) {
        ans++;
      }
      i--;
      j++;
    }
    i = n - i1 - 1;
    j = m - 1;
    while ((i < n && j >= 0)) {
      if (a[i][j] == 1) {
        ans++;
      }
      i++;
      j--;
    }
  }
  long long int l = min((i1 + 1), m);
  l = min(l, n);
  if (ans > l) ans = 2 * l - ans;
  return ans;
}
int main() {
  ios_base::sync_with_stdio(0), cin.tie(0);
  long long int t;
  for (cin >> t; t > 0; t--) {
    long long int n, m;
    cin >> n >> m;
    vector<vector<long long int> > pal(n);
    for (long long int i = 0; i < n; i++) pal[i] = vector<long long int>(m);
    for (long long int i = 0; i < n; i++) {
      for (long long int j = 0; j < m; j++) {
        long long int c;
        cin >> c;
        pal[i][j] = c;
      }
    }
    long long int ans = 0;
    long long int k = (n + m - 1) / 2;
    for (long long int i = 0; i < k; i++) {
      ans += dia(pal, i);
    }
    cout << ans << "\n";
  }
}
