#include <bits/stdc++.h>
using namespace std;
int manhattan(vector<vector<int>> &grid) {
  vector<int> ret_zero, ret_one;
  int n = grid.size();
  int m = grid[0].size();
  int mh_len = ((n + m + 1) / 2) - 1;
  ret_zero.resize(mh_len);
  ret_one.resize(mh_len);
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      int from_start = i + j;
      int from_goal = n - 1 - i + m - 1 - j;
      if (from_start < mh_len) {
        if (grid[i][j])
          ret_one[from_start]++;
        else
          ret_zero[from_start]++;
      } else if (from_goal < mh_len) {
        if (grid[i][j])
          ret_one[from_goal]++;
        else
          ret_zero[from_goal]++;
      }
    }
  }
  int ans = 0;
  for (int i = 0; i < mh_len; i++) {
    ans += min(ret_zero[i], ret_one[i]);
  }
  return ans;
}
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n, m;
    cin >> n >> m;
    vector<vector<int>> grid(n, vector<int>(m, 0));
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        cin >> grid[i][j];
      }
    }
    cout << manhattan(grid) << endl;
  }
  return 0;
}
