#include <bits/stdc++.h>
using namespace std;
int main() {
  int t, n, m, i, j, x, ans;
  cin >> t;
  while (t--) {
    cin >> n >> m;
    vector<int> dp[n + m - 1];
    for (i = 0; i < n; i++) {
      for (j = 0; j < m; j++) {
        cin >> x;
        int a = i + j;
        int b = (n - i - 1) + (m - j - 1);
        if (a != b) dp[min(a, b)].push_back(x);
      }
    }
    ans = 0;
    for (i = 0; i < n + m - 1; i++) {
      int a = 0;
      int b = 0;
      for (j = 0; j < dp[i].size(); j++) {
        if (dp[i][j] == 1)
          a++;
        else
          b++;
      }
      ans += min(a, b);
    }
    cout << ans << endl;
  }
  return 0;
}
