#include <bits/stdc++.h>
using namespace std;
int n, m, a[50][50], ans[100][2];
void init() {
  scanf("%d %d", &n, &m);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) scanf("%d", &a[i][j]);
}
void work() {
  int T;
  scanf("%d", &T);
  while (T--) {
    init();
    memset(ans, 0, sizeof(ans));
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= m; j++) {
        int F = i + j - 1, G = (n - i) + (m - j) + 1;
        if (F <= (n + m - 1) >> 1) ans[F][a[i][j]]++;
        if (G <= (n + m - 1) >> 1) ans[G][a[i][j]]++;
      }
    int Ans = 0;
    for (int i = 1; i <= n + m - 1 >> 1; i++) Ans += min(ans[i][0], ans[i][1]);
    printf("%d\n", Ans);
  }
}
int main() {
  work();
  return 0;
}
