#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:10000000000")
using namespace std;
const int INF = 1000000007;
const long long INF2 = 1000000000000000007;
const long long MOD = 1000000007;
const long long MOD1 = 1864463441;
const int SIZE = 200010;
const double eps = 1e-9;
const long double pi = acos(-1.0);
long long q, n, m, d, a, b, k, rs, mx, x, y, even1, even0, odd1, odd0;
long long ar[SIZE], mapa[35][35];
bool fl;
bool cmp(int a, int b) { return a > b; }
string s;
long long cnt[21];
vector<pair<int, int> > ans;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> q;
  while (q--) {
    cin >> n >> m;
    vector<int> cnt(n + m + 2), cnt1(n + m + 2);
    for (int i = 1; i < n + 1; ++i) {
      for (int j = 1; j < m + 1; ++j) {
        cin >> a;
        cnt[i + j] += (a % 2);
        cnt1[i + j]++;
      }
    }
    rs = 0;
    for (int i = 2; i < (n + m + 3) / 2; ++i) {
      rs += min(cnt1[i] - cnt[i] + cnt1[n + m + 2 - i] - cnt[n + m + 2 - i],
                cnt[i] + cnt[n + m + 2 - i]);
    }
    cout << rs << endl;
  }
  return 0;
}
