#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const long long inf = 0x3f3f3f3f3f3f3f3f;
const long long mod = 1e9 + 7;
const int N = 2e5 + 10;
int T;
int n, m;
int cnt[1000][2], mz[40][40];
int main() {
  scanf("%d", &T);
  while (T--) {
    scanf("%d%d", &n, &m);
    memset(cnt, 0, sizeof(cnt));
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= m; j++) {
        scanf("%d", &mz[i][j]);
        int a = abs(i - 1) + abs(j - 1);
        int b = abs(i - n) + abs(j - m);
        cnt[min(a, b)][mz[i][j]]++;
      }
    }
    int ans = 0;
    for (int i = 0; i <= (n - 1 + m - 1 - 1) / 2; i++) {
      ans += min(cnt[i][1], cnt[i][0]);
    }
    printf("%d\n", ans);
  }
  return 0;
}
