#include <bits/stdc++.h>
using namespace std;
int check_palindrome(vector<int> v) {
  int ans = 0;
  for (int i = 0; i < v.size() / 2; i++)
    ans += (v[i] == v[v.size() - i - 1]) ? 0 : 1;
  return ans;
}
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n, m;
    cin >> n >> m;
    vector<vector<int> > v(n, vector<int>(m));
    for (auto &x : v)
      for (auto &y : x) cin >> y;
    if (n == 1) {
      cout << check_palindrome(v[0]) << '\n';
    } else if (m == 1) {
      vector<int> c(n);
      for (int i = 0; i < n; i++) c[i] = v[i][0];
      cout << check_palindrome(c) << '\n';
    } else {
      vector<vector<int> > v1(n, vector<int>(m));
      for (int i = 0; i < n; i++) v1[i][0] = i + 1;
      for (int i = 0; i < m; i++) v1[0][i] = i + 1;
      for (int i = 1; i < n; i++) {
        for (int j = 1; j < m; j++)
          v1[i][j] = min(v1[i][j - 1], v1[i - 1][j]) + 1;
      }
      map<int, pair<int, int> > mm;
      int mod = (n + m) / 2;
      int ans = 0;
      for (int i = 0; i < n; i++)
        for (int j = 0; j < m; j++) {
          if (v[i][j])
            mm[(v1[i][j] > mod) ? ((n + m) - v1[i][j]) : v1[i][j]].first++;
          else
            mm[(v1[i][j] > mod) ? ((n + m) - v1[i][j]) : v1[i][j]].second++;
        }
      if (((m + n) & 1)) {
        for (auto c : mm) ans += min(c.second.first, c.second.second);
      } else {
        mm.erase(mod);
        for (auto c : mm) ans += min(c.second.first, c.second.second);
      }
      cout << ans << '\n';
    }
  }
}
