#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization("unroll-loops")
using namespace std;
long long dx[] = {1, 0, -1, 0};
long long dy[] = {0, 1, 0, -1};
void debug_out(vector<string> __attribute__((unused)) args,
               __attribute__((unused)) long long idx,
               __attribute__((unused)) long long LINE_NUM) {
  cerr << "\n";
}
template <typename Head, typename... Tail>
void debug_out(vector<string> args, long long idx, long long LINE_NUM, Head H,
               Tail... T) {
  if (idx > 0)
    cerr << ", ";
  else
    cerr << "Line(" << LINE_NUM << ") ";
  stringstream ss;
  ss << H;
  cerr << args[idx] << " = " << ss.str();
  debug_out(args, idx + 1, LINE_NUM, T...);
}
void solve() {
  long long n, m;
  cin >> n >> m;
  long long a[n][m];
  long long x = n + m - 1;
  vector<vector<long long>> v(x);
  for (long long i = 0; i < n; i++)
    for (long long j = 0; j < m; j++) {
      cin >> a[i][j];
      v[i + j].push_back(a[i][j]);
    }
  long long ss = 0;
  for (long long i = 0; i < x / 2; i++) {
    vector<long long> a = v[i];
    vector<long long> b = v[x - i - 1];
    long long cost1 = 0, cost2 = 0;
    for (auto p : a) {
      if (p == 0) cost1++;
    }
    for (auto p : b) {
      if (p == 0) cost1++;
    }
    for (auto p : a) {
      if (p == 1) cost2++;
    }
    for (auto p : b) {
      if (p == 1) cost2++;
    }
    ss += min(cost1, cost2);
  }
  cout << ss << "\n";
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long t;
  cin >> t;
  while (t--) solve();
  return 0;
}
