#include <bits/stdc++.h>
using namespace std;
long long a, b, c, d, f, g, n, m, i, j, k, l, o, p, t, y, mx, mn = 1e15, x;
bool A[50][50];
long long B[205];
long long C[205];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> t;
  for (y = 0; y < t; y++) {
    g = 0;
    for (i = 0; i < 200; i++) {
      B[i] = 0;
      C[i] = 0;
    }
    cin >> n >> m;
    for (i = 0; i < n; i++) {
      for (j = 0; j < m; j++) {
        cin >> A[i][j];
      }
    }
    d = 0;
    for (i = 0; i < m; i++) {
      a = 0;
      b = i;
      c = 0;
      f = 0;
      while (b >= 0 && a < n) {
        if (A[a][b] == 1)
          c++;
        else
          f++;
        b--;
        a++;
      }
      d++;
      B[d] = c;
      C[d] = f;
    }
    for (i = 1; i < n; i++) {
      a = i;
      b = m - 1;
      c = 0;
      f = 0;
      while (a < n && b >= 0) {
        if (A[a][b] == 1)
          c++;
        else
          f++;
        b--;
        a++;
      }
      d++;
      B[d] = c;
      C[d] = f;
    }
    for (i = 1, j = d; i < j; i++, j--) {
      g += min(B[i] + B[j], C[i] + C[j]);
    }
    cout << g << "\n";
  }
}
