#include <bits/stdc++.h>
using namespace std;
const int mod = 1000000007;
const int nn = 3e5;
int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int t;
  cin >> t;
  while (t--) {
    int i, j, k, l, m, n, x = 0, y = 0, z = 0, r, b, c, d, e, f;
    string p, q, s;
    unordered_map<int, int> mp;
    vector<pair<int, int>> v;
    cin >> n >> m;
    int a[n][m];
    for (i = 0; i < n; i++) {
      for (j = 0; j < m; j++) {
        cin >> a[i][j];
      }
    }
    d = n + m - 1;
    d /= 2;
    for (r = 1; r <= d; r++) {
      i = 0;
      j = 0;
      v.clear();
      mp.clear();
      while ((i < n) && (i < r)) {
        k = r - i;
        j = r - i - 1;
        if ((j >= 0) && (j < m)) {
          v.push_back(make_pair(i, j));
          v.push_back(make_pair((n - i - 1), (m - 1 - j)));
        }
        i += 1;
      }
      for (auto w : v) {
        b = w.first;
        c = w.second;
        b = a[b][c];
        mp[b]++;
      }
      e = mp[0];
      f = mp[1];
      c = min(e, f);
      z += c;
    }
    cout << z << "\n";
  }
}
