#include <bits/stdc++.h>
using namespace std;
const long long INF = (long long)(1e9 + 7);
bool check(pair<int, int> a, pair<int, int> b) { return (a.first < b.first); }
int main() {
  int t;
  cin >> t;
  while (t--) {
    int m, n;
    cin >> m >> n;
    int a[31][31];
    pair<int, int> x[65];
    for (int i = 0; i < 65; i++) {
      x[i] = {0, 0};
    }
    for (int i = 1; i <= m; i++) {
      for (int j = 1; j <= n; j++) {
        cin >> a[i][j];
      }
    }
    for (int i = 1; i <= m; i++) {
      for (int j = 1; j <= n; j++) {
        x[i + j].first += a[i][j];
        x[i + j].second++;
      }
    }
    int sum = 0;
    for (int i = 2; 2 * i <= m + n + 1; i++) {
      sum += min(x[i].first + x[m + n + 2 - i].first,
                 x[i].second + x[m + n + 2 - i].second -
                     (x[i].first + x[m + n + 2 - i].first));
    }
    cout << sum << endl;
  }
}
