#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  scanf("%d", &t);
  while (t--) {
    int n, m;
    cin >> n >> m;
    vector<vector<int> > vec(n + 1);
    vector<pair<int, int> > arr(n + m + 1, make_pair(0, 0));
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        int x;
        scanf("%d", &x);
        vec[i].push_back(x);
      }
    }
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        int d = (i - 0) + (j - 0);
        if (vec[i][j])
          arr[d].first++;
        else
          arr[d].second++;
      }
    }
    int count = 0;
    int start = 0, end = n + m - 2;
    while (start < end) {
      int one = arr[start].first + arr[end].first;
      int zero = arr[start].second + arr[end].second;
      count += min(one, zero);
      start++;
      end--;
    }
    cout << count << endl;
  }
  return 0;
}
