#include <bits/stdc++.h>
using namespace std;
const int dx[8] = {0, -1, 0, 1, -1, -1, 1, 1},
          dy[8] = {1, 0, -1, 0, -1, 1, -1, 1};
int a[35][35], vis[35][35];
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n, m;
    cin >> n >> m;
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= m; j++) {
        cin >> a[i][j];
        vis[i][j] = i + j - 1;
      }
    if (n == 2 && m == 2) {
      if (a[1][1] == a[2][2])
        printf("0\n");
      else
        printf("1\n");
      continue;
    }
    int k = min(n, m);
    int p = n + m - 1;
    int num = 0;
    for (int i = 1; i <= p / 2; i++) {
      int num1 = 0, num2 = 0;
      for (int j = 1; j <= n; j++)
        for (int k = 1; k <= m; k++) {
          if (vis[j][k] == i || (vis[j][k] + i) == (n + m)) {
            if (a[j][k] == 1)
              num1++;
            else
              num2++;
          }
        }
      num += min(num1, num2);
    }
    printf("%d\n", num);
  }
}
