#include <bits/stdc++.h>
using namespace std;
int tmp;
int row, col;
int ans1[60];
int ans0[60];
int t, n, m;
long long ans;
int M[31][31];
int main() {
  int count0, count1;
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.precision(10);
  cout << fixed;
  cin >> t;
  while (t--) {
    ans = 0;
    cin >> n >> m;
    for (int i = (int)(0); i < (int)(n); ++i) {
      for (int j = (int)(0); j < (int)(m); ++j) {
        cin >> M[i][j];
      }
    }
    for (int i = (int)(0); i < (int)(m + n - 1); ++i) {
      count0 = 0, count1 = 0;
      if (i >= m) {
        row = i - m + 1;
        col = i - row;
      } else {
        row = 0;
        col = i - row;
      }
      while (row < n && col >= 0) {
        if (M[row][col] == 1) {
          count1++;
        } else
          count0++;
        row += 1;
        col -= 1;
      }
      ans1[i] = count1;
      ans0[i] = count0;
    }
    for (int i = (int)(0); i < (int)((m + n - 1) / 2); ++i) {
      tmp = m + n - i - 2;
      ans += min(ans1[i] + ans1[tmp], ans0[i] + ans0[tmp]);
    }
    cout << ans << '\n';
  }
  return 0;
}
