#include <bits/stdc++.h>
using namespace std;
int a[31][31], used[31][31];
long long n, m;
vector<pair<int, int> > zz = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
int check(long long x, long long y) {
  if (x >= 1 && x <= n && y >= 1 && y <= m && used[x][y] == 0) return 1;
  return 0;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int t;
  cin >> t;
  for (; t > 0; t--) {
    long long ans = 0;
    cin >> n >> m;
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= m; j++) {
        cin >> a[i][j];
        used[i][j] = 0;
      }
    if (n == 1 && m == 1) {
      cout << 0 << "\n";
      continue;
    }
    queue<int> x, y, dep;
    int k = 0;
    x.push(1);
    y.push(1);
    dep.push(1);
    x.push(n);
    y.push(m);
    dep.push(1);
    used[1][1] = 1;
    used[n][m] = 1;
    long long z = n + m - 1;
    z /= 2;
    while (k + 1 <= z) {
      k++;
      long long ed = 0;
      long long nul = 0;
      while (!dep.empty() && dep.front() == k) {
        long long xx = x.front();
        x.pop();
        long long yy = y.front();
        y.pop();
        long long d = dep.front();
        dep.pop();
        if (a[xx][yy] == 1)
          ed++;
        else
          nul++;
        for (int i = 0; i < 4; i++) {
          xx += zz[i].first;
          yy += zz[i].second;
          if (check(xx, yy)) {
            x.push(xx);
            y.push(yy);
            dep.push(d + 1);
            used[xx][yy] = 1;
          }
          xx -= zz[i].first;
          yy -= zz[i].second;
        }
      }
      ans += min(ed, nul);
    }
    cout << ans << "\n";
  }
}
