#include <bits/stdc++.h>
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
using namespace std;
void err(istream_iterator<string> it) { cout << '\n'; }
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
  cerr << "[" << *it << " = " << a << "] ";
  err(++it, args...);
}
pair<int, int> dx[] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
const int inf = 2e9 + 7;
const long long INF = 3e18 + 7;
const long double pi = 3.1415926535897932384626433;
const long double EPS = 1e-9;
const int mod = 1e9 + 7;
long long MOD = 1e15 + 7;
int gcd(int a, int b) {
  if (a < b) swap(a, b);
  while (b) {
    a %= b;
    swap(a, b);
  }
  return a;
}
const bool multiple_tests = 1;
const long long N = 2e5 + 7;
void solve() {
  long long n, m;
  cin >> n >> m;
  long long cp[n + 1][m + 1], a[max(n, m) + 1][max(n, m) + 1];
  bool used[max(n, m) + 1][max(n, m) + 1];
  for (long long i = 1; i <= n; i++) {
    for (long long j = 1; j <= m; j++) {
      used[i][j] = 0;
      cin >> cp[i][j];
    }
  }
  if (n < m) {
    for (long long i = 1; i <= n; i++) {
      for (long long j = 1; j <= m; j++) {
        a[j][i] = cp[i][j];
        used[j][i] = 0;
      }
    }
    swap(n, m);
  } else {
    for (long long i = 1; i <= n; i++) {
      for (long long j = 1; j <= m; j++) {
        used[i][j] = 0;
        a[i][j] = cp[i][j];
      }
    }
  }
  long long ans = 0;
  for (long long q = 1; q <= n; q++) {
    long long i = q, j = 1;
    long long one = 0, zero = 0;
    while (i >= 1 && j <= m) {
      if (a[i][j] == 1)
        one++;
      else
        zero++;
      used[i][j] = 1;
      i--;
      j++;
    }
    i = n - q + 1;
    j = m;
    bool zabei = 0;
    while (i <= n && j >= 1) {
      if (a[i][j] == 1)
        one++;
      else
        zero++;
      if (used[i][j]) {
        zabei = 1;
      }
      used[i][j] = 1;
      i++;
      j--;
    }
    if (zabei) {
      continue;
    }
    long long set = 0;
    if (one > zero) {
      set = 1;
    }
    i = q, j = 1;
    while (i >= 1 && j <= m) {
      if (a[i][j] != set) ans++;
      a[i][j] = set;
      i--;
      j++;
    }
    i = n - q + 1;
    j = m;
    while (i <= n && j >= 1) {
      if (a[i][j] != set) ans++;
      a[i][j] = set;
      i++;
      j--;
    }
  }
  cout << ans;
}
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long TT = 1;
  if (multiple_tests) cin >> TT;
  for (long long T = 1; T <= TT; T++) {
    solve();
    cout << '\n';
  }
  cerr << "\nTime elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
  return 0;
}
