#include <bits/stdc++.h>
using namespace std;
int main() {
  long long t;
  cin >> t;
  while (t--) {
    long long n, m;
    cin >> n >> m;
    vector<vector<long long> > v(n + 1, vector<long long>(m + 1));
    for (long long i = 1; i < n + 1; i++) {
      for (long long j = 1; j < m + 1; j++) {
        cin >> v[i][j];
      }
    }
    long long count = 0;
    long long l = 2;
    long long r = n + m;
    while (l < r) {
      long long zero = 0;
      long long one = 0;
      for (long long i = 1; i < n + 1; i++) {
        long long j1 = l - i;
        long long j2 = r - (n + 1 - i);
        if (j1 < 1 || j1 > m || j2 < 1 || j2 > m) {
          continue;
        } else {
          if (v[i][j1] == 0)
            zero++;
          else
            one++;
          if (v[n + 1 - i][j2] == 0)
            zero++;
          else
            one++;
        }
      }
      count += min(zero, one);
      l++;
      r--;
    }
    cout << count << endl;
  }
}
