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