#include <bits/stdc++.h>
using namespace std;
int main() {
  int test;
  cin >> test;
  while (test--) {
    int n, m;
    cin >> n >> m;
    int arr[n][m];
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        cin >> arr[i][j];
      }
    }
    vector<int> ans(n + m - 1, 0);
    vector<int> ans0(n + m - 1, 0);
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        int k = i + j;
        if (arr[i][j] == 1) {
          ans[k]++;
        } else {
          ans0[k]++;
        }
      }
    }
    int count = 0;
    for (int i = 0, j = n + m - 2; i < j; i++, j--) {
      count += min(ans[i] + ans[j], ans0[i] + ans0[j]);
    }
    cout << count << endl;
  }
}
