#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int i, j, k, n, m, t;
  cin >> t;
  while (t--) {
    cin >> n >> m;
    vector<int> one(n + m), zero(n + m);
    for (i = 0; i < n; ++i) {
      for (j = 0; j < m; ++j) {
        bool a;
        cin >> a;
        if (a)
          one[i + j]++;
        else
          zero[i + j]++;
      }
    }
    int ans = 0;
    for (i = 0, j = n + m - 2; i < j; ++i, --j)
      ans += min(one[i] + one[j], zero[i] + zero[j]);
    cout << ans << "\n";
  }
  return 0;
}
