#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
const long long maxn = 1e5 + 1;
void swap(long long *xp, long long *yp) {
  long long temp = *xp;
  *xp = *yp;
  *yp = temp;
}
void print(vector<long long> v) {
  long long i, j, n = v.size();
  for (i = 0; i < n; i++) {
    cout << v[i] << " ";
  }
  cout << "\n";
}
void scan(vector<long long> &v, long long n) {
  for (long long i = 0; i < n; i++) cin >> v[i];
}
long long gcd(long long a, long long b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
long long lcm(long long a, long long b) { return (a * b) / gcd(a, b); }
int main() {
  long long int i, j, inc, dec, t, r;
  long long int c, d, e, temp, m, p, w, k, x, y, sum, pre, temp2, mid, n, low,
      high, ans;
  t = 1;
  n = 1;
  cin >> t;
  while (t--) {
    cin >> n >> m;
    vector<vector<long long>> a(n, vector<long long>(m));
    vector<vector<bool>> vis(n, vector<bool>(m, false));
    for (i = 0; i < n; i++) {
      for (j = 0; j < m; j++) {
        cin >> a[i][j];
      }
    }
    queue<pair<long long, long long>> q;
    q.push(make_pair(0, 0));
    vis[0][0] = true;
    vector<pair<long long, long long>> v;
    i = 0;
    while (!q.empty()) {
      y = q.size();
      temp = 0, temp2 = 0;
      while (y--) {
        pair<long long, long long> p = q.front();
        q.pop();
        if (a[p.first][p.second] == 0)
          temp++;
        else
          temp2++;
        if (p.first + 1 < n && !vis[p.first + 1][p.second]) {
          vis[p.first + 1][p.second] = true;
          q.push(make_pair(p.first + 1, p.second));
        }
        if (p.second + 1 < m && !vis[p.first][p.second + 1]) {
          vis[p.first][p.second + 1] = true;
          q.push(make_pair(p.first, p.second + 1));
        }
      }
      v.push_back(make_pair(temp, temp2));
    }
    i = 0, j = v.size() - 1;
    ans = 0;
    while (i < j) {
      ans += min(v[i].first + v[j].first, v[i].second + v[j].second);
      i++, j--;
    }
    cout << ans << "\n";
    ;
  }
  return 0;
}
