#include <bits/stdc++.h>
using namespace std;
long long max(long long a, long long b) { return a > b ? a : b; }
long long min(long long a, long long b) { return a < b ? a : b; }
long long abso(long long a) { return a > 0 ? a : -a; }
bool cmp(const pair<int, int> &a, const pair<int, int> &b) {
  if (a.first == b.first) return a.second < b.second;
  return a.first < b.first;
}
int test(int i, int j, int n, int m) {
  if (i == n && j == m) return 1;
  if (i <= 0 || i > n || j <= 0 || j > m) return 0;
  return test(i + 1, j, n, m) + test(i, j + 1, n, m);
}
vector<vector<int> > dist, inp, vis;
queue<pair<int, int> > Q;
unordered_map<int, pair<int, int> > X;
int main(int argc, char const *argv[]) {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  int t;
  cin >> t;
  while (t--) {
    int a, b;
    cin >> a >> b;
    inp.resize(a);
    dist.resize(a);
    vis.resize(a);
    for (int i = 0; i < a; i++) {
      inp[i].resize(b);
      dist[i].resize(b);
      vis[i].resize(b);
      for (int j = 0; j < inp[i].size(); j++) {
        cin >> inp[i][j];
        dist[i][j] = 0;
        vis[i][j] = false;
      }
    }
    Q.push(pair<int, int>(0, 0));
    dist[0][0] = 0;
    while (!Q.empty()) {
      pair<int, int> f = Q.front();
      Q.pop();
      if (vis[f.first][f.second]) continue;
      vis[f.first][f.second] = 1;
      if (f.first + 1 < a) {
        Q.push(pair<int, int>(f.first + 1, f.second));
        dist[f.first + 1][f.second] = dist[f.first][f.second] + 1;
      }
      if (f.second + 1 < b) {
        Q.push(pair<int, int>(f.first, f.second + 1));
        dist[f.first][f.second + 1] = dist[f.first][f.second] + 1;
      }
    }
    X.clear();
    int lo = 0, hi = -1;
    for (int i = 0; i < dist.size(); i++) {
      for (int j = 0; j < dist[i].size(); j++) {
        if (X.find(dist[i][j]) == X.end()) {
          X[dist[i][j]] = pair<int, int>(0, 0);
        }
        if (inp[i][j] == 1)
          X[dist[i][j]].second++;
        else
          X[dist[i][j]].first++;
        hi = max(hi, dist[i][j]);
      }
    }
    int res = 0;
    while (lo < hi) {
      res += min(X[lo].first + X[hi].first, X[lo].second + X[hi].second);
      lo++;
      hi--;
    }
    cout << res << "\n";
  }
}
