#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n, m;
    cin >> n >> m;
    int a[n][m], one[n + m], zero[n + m];
    memset(one, 0, sizeof(one));
    memset(zero, 0, sizeof(zero));
    for (int i = 0; i < n; i++)
      for (int j = 0; j < m; j++) {
        cin >> a[i][j];
        if (a[i][j])
          one[i + j]++;
        else
          zero[i + j]++;
      }
    int im = (n - 1 + m - 1);
    int ans = 0;
    for (int i = 0; i <= im / 2; i++) {
      if (im - i == i) continue;
      ans += min(one[i] + one[im - i], zero[i] + zero[im - i]);
    }
    cout << ans << endl;
  }
  return 0;
}
