#include <bits/stdc++.h>
using namespace std;
void solve() {
  int n, m;
  cin >> n >> m;
  int arr[n][m];
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) cin >> arr[i][j];
  int sz = n + m - 1;
  sz /= 2;
  int edit = 0;
  if (m >= n) {
    for (int i = 0; i < sz; i++) {
      int r = 0, c = i;
      int rr = n - 1, cc = m - 1 - i;
      int one = 0, to = 0;
      while (r < n && c >= 0) {
        if (arr[r][c]) one++;
        to++;
        r++;
        c--;
      }
      while (rr >= 0 && cc < m) {
        if (arr[rr][cc]) one++;
        to++;
        rr--;
        cc++;
      }
      if (one >= to - one) {
        edit += to - one;
      } else {
        edit += one;
      }
    }
  } else {
    for (int i = 0; i < sz; i++) {
      int r = i, c = 0;
      int rr = n - 1 - i, cc = m - 1;
      int one = 0, to = 0;
      while (r >= 0 && c < m) {
        if (arr[r][c]) one++;
        to++;
        r--;
        c++;
      }
      while (rr < n && cc >= 0) {
        if (arr[rr][cc]) one++;
        to++;
        rr++;
        cc--;
      }
      if (one >= to - one) {
        edit += to - one;
      } else {
        edit += one;
      }
    }
  }
  cout << edit << endl;
  return;
}
int main() {
  int t;
  cin >> t;
  while (t--) solve();
  return 0;
}
