#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  while (t--) {
    int r, c;
    cin >> r >> c;
    int dp[r][c];
    map<int, map<int, int>> mp;
    for (int i = 0; i < r; i++) {
      for (int j = 0; j < c; j++) {
        int a;
        cin >> a;
        int i1 = r - 1 - i;
        int j1 = c - 1 - j;
        if (i + j == j1 + i1) continue;
        mp[min(i + j, i1 + j1)][a]++;
      }
    }
    int ans = 0;
    for (auto x : mp) {
      ans += min(x.second[1], x.second[0]);
    }
    cout << ans << endl;
  }
  return 0;
}
