#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int t;
  cin >> t;
  while (t--) {
    long long int n, i, x, ans = 0, m, mi, ma, j;
    cin >> n >> m;
    long long int a[n][m];
    long long int b[n][m];
    for (i = 0; i < n; i++) {
      for (j = 0; j < m; j++) {
        cin >> a[i][j];
      }
    }
    for (i = 0; i < n; i++) {
      for (j = 0; j < m; j++) {
        b[i][j] = i + j;
      }
    }
    x = n - 1 + m - 1;
    if (x % 2 == 1) {
      for (i = 0; i < n; i++) {
        for (j = 0; j < m; j++) {
          if (b[i][j] > x / 2) {
            b[i][j] = x - b[i][j];
          }
        }
      }
      map<long long int, int> one;
      map<long long int, int> zero;
      for (i = 0; i <= x / 2; i++) {
        one[i] = 0;
        zero[i] = 0;
      }
      for (i = 0; i < n; i++) {
        for (j = 0; j < m; j++) {
          if (a[i][j] == 1) {
            one[b[i][j]]++;
          } else {
            zero[b[i][j]]++;
          }
        }
      }
      for (i = 0; i <= x / 2; i++) {
        ans = ans + min(one[i], zero[i]);
      }
    } else {
      for (i = 0; i < n; i++) {
        for (j = 0; j < m; j++) {
          if (b[i][j] > x / 2) {
            b[i][j] = x - b[i][j];
          }
        }
      }
      map<long long int, int> one;
      map<long long int, int> zero;
      for (i = 0; i <= x / 2; i++) {
        one[i] = 0;
        zero[i] = 0;
      }
      for (i = 0; i < n; i++) {
        for (j = 0; j < m; j++) {
          if (b[i][j] != x / 2) {
            if (a[i][j] == 1) {
              one[b[i][j]]++;
            } else {
              zero[b[i][j]]++;
            }
          }
        }
      }
      for (i = 0; i <= x / 2; i++) {
        ans = ans + min(one[i], zero[i]);
      }
    }
    cout << ans << "\n";
  }
}
