#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ;
  int q;
  cin >> q;
  while (q--) {
    int n, m;
    cin >> n >> m;
    int ve[n + m + 1], vec[n + m + 1];
    int a[n + 1][m + 1];
    for (int i = 1; i <= n + m; i++) {
      ve[i] = 0;
      vec[i] = 0;
    }
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= m; j++) {
        cin >> a[i][j];
        vec[i + j] += a[i][j];
        ve[i + j]++;
      }
    }
    int same = 0;
    for (int i = 2; i <= (n + m + 1) / 2; i++) {
      int x = 0, y = 0;
      x = 2 * ve[i];
      y = vec[i] + vec[m + n - i + 2];
      if (2 * y < x)
        same += y;
      else
        same += x - y;
    }
    cout << same << endl;
  }
}
