#include <bits/stdc++.h>
using namespace std;
istream& operator>>(istream& o, pair<long long, long long>& v) {
  o >> v.first >> v.second;
  return o;
}
ostream& operator<<(ostream& o, pair<long long, long long>& v) {
  o << "(" << v.first << "," << v.second << ")";
  return o;
}
template <typename T>
istream& operator>>(istream& o, vector<T>& v) {
  for (long long i = 0; i < v.size(); ++i) cin >> v[i];
  return o;
}
template <typename T>
ostream& operator<<(ostream& o, vector<T>& v) {
  for (long long i = 0; i < v.size(); ++i) cout << v[i] << " ";
  return o;
}
void solve() {
  long long n, m;
  cin >> n >> m;
  vector<vector<long long> > v(n, vector<long long>(m));
  cin >> v;
  long long len = n + m - 1;
  long long ans = 0;
  for (long long i = 0; i < len / 2; ++i) {
    long long zero = 0, one = 0;
    long long v1 = i;
    long long v2 = len - i - 1;
    for (long long r = 0; r < n; ++r) {
      for (long long c = 0; c < m; ++c) {
        if (r + c == v1 && v[r][c] == 0) zero++;
        if (r + c == v1 && v[r][c] == 1) one++;
        if (r + c == v2 && v[r][c] == 0) zero++;
        if (r + c == v2 && v[r][c] == 1) one++;
      }
    }
    ans += min(zero, one);
  }
  cout << ans << "\n";
}
int main() {
  time_t start, end;
  time(&start);
  time(&end);
  long long t;
  cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}
