#include <bits/stdc++.h>
using namespace std;
int main(int argc, char** argv) {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cout << setprecision(15);
  if (argc == 2 && atoi(argv[1]) == 123456789)
    freopen("d:\\code\\cpp\\contests\\stdin", "r", stdin);
  int T;
  cin >> T;
  for (int(t) = 0; (t) < (T); (t)++) {
    int n, m;
    cin >> n >> m;
    vector<vector<int>> a(n, vector<int>(m));
    for (int(i) = 0; (i) < (n); (i)++)
      for (int(j) = 0; (j) < (m); (j)++) cin >> a[i][j];
    vector<vector<int>> b(n, vector<int>(m, 1e9));
    b[0][0] = 0;
    for (int(i) = 0; (i) < (n); (i)++)
      for (int(j) = 0; (j) < (m); (j)++) {
        if (i > 0) b[i][j] = min(b[i][j], b[i - 1][j] + 1);
        if (j > 0) b[i][j] = min(b[i][j], b[i][j - 1] + 1);
      }
    b[n - 1][m - 1] = 0;
    for (int(i) = n - 1; (i) >= 0; (i)--)
      for (int(j) = m - 1; (j) >= 0; (j)--) {
        if (i + 1 < n) b[i][j] = min(b[i][j], b[i + 1][j] + 1);
        if (j + 1 < m) b[i][j] = min(b[i][j], b[i][j + 1] + 1);
      }
    vector<int> cnt0(n * m), cnt1(n * m);
    for (int(i) = 0; (i) < (n); (i)++)
      for (int(j) = 0; (j) < (m); (j)++) {
        if (a[i][j]) {
          cnt1[b[i][j]]++;
        } else {
          cnt0[b[i][j]]++;
        }
      }
    long long sol = 0;
    for (int(i) = 0; (i) < ((n + m - 1) / 2); (i)++) {
      sol += min(cnt1[i], cnt0[i]);
    }
    cout << sol << endl;
  }
  if (argc == 2 && atoi(argv[1]) == 123456789)
    cout << clock() * 1.0 / CLOCKS_PER_SEC << " sec\n";
  return 0;
}
