#include <bits/stdc++.h>
using namespace std;
int a[50][50];
int cnt[110][2];
int main() {
  int T;
  scanf("%d", &T);
  while (T--) {
    int n, m;
    scanf("%d %d", &n, &m);
    memset(cnt, 0, sizeof(cnt));
    for (register int i = 0; i < n; i++) {
      for (register int j = 0; j < m; j++) {
        scanf("%d", &a[i][j]);
      }
    }
    int k;
    for (register int i = 0; i < n; i++) {
      for (register int j = 0; j < m; j++) {
        k = i + j;
        cnt[k][a[i][j]]++;
      }
    }
    int ans = 0;
    int x, y;
    for (register int i = 0; i < n + m - 1; i++) {
      x = i;
      y = n + m - i - 2;
      if (x >= y) {
        break;
      }
      ans += min(cnt[x][0] + cnt[y][0], cnt[x][1] + cnt[y][1]);
    }
    printf("%d\n", ans);
  }
  return 0;
}
