#include <bits/stdc++.h>
using namespace std;
template <class c>
struct rge {
  c b, e;
};
template <class c>
rge<c> range(c i, c j) {
  return rge<c>{i, j};
}
template <class c>
auto dud(c* x) -> decltype(cerr << *x, 0);
template <class c>
char dud(...);
struct debug {
  template <class c>
  debug& operator<<(const c&) {
    return *this;
  }
};
template <typename T>
void db(T x) {
  debug() << ">>> " << x;
}
template <typename T>
void db(T x, T y) {
  debug() << ">>> " << x << " , " << y;
}
template <typename T>
void db(T x, T y, T z) {
  debug() << ">>> " << x << " , " << y << " , " << z;
}
template <typename T>
void db(T x, T y, T z, T a) {
  debug() << ">>> " << x << " , " << y << " , " << z << " , " << a;
}
const long long xx = 2e5;
const long long Mod = 1e9 + 7;
const long long inf = 1e18;
const long long Maxn = 20010;
long long Cnt[100][2];
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  long long t;
  cin >> t;
  while (t--) {
    memset(Cnt, 0, sizeof Cnt);
    long long n, m;
    cin >> n >> m;
    long long mat[60][60];
    for (long long i = 1; i <= n; i++) {
      for (long long j = 1; j <= m; j++) {
        cin >> mat[i][j];
      }
    }
    for (long long i = 1; i <= m; i++) {
      long long r = 1, c = i;
      while (r <= n && c >= 1) {
        Cnt[i][0] += mat[r][c] == 0;
        Cnt[i][1] += mat[r][c] == 1;
        r++, c--;
      }
    }
    long long d = m + 1;
    for (long long i = 2; i <= n; i++) {
      long long r = i, c = m;
      while (r <= n && c >= 1) {
        Cnt[d][0] += mat[r][c] == 0;
        Cnt[d][1] += mat[r][c] == 1;
        r++, c--;
      }
      d++;
    }
    for (long long i = 1; i <= n + m - 1; i++) {
      debug() << "\033[94m ["
              << "i"
                 ": "
              << (i)
              << "]\033[0m"
                 "\033[94m ["
              << "Cnt[i][0]"
                 ": "
              << (Cnt[i][0])
              << "]\033[0m"
                 "\033[94m ["
              << "Cnt[i][1]"
                 ": "
              << (Cnt[i][1]) << "]\033[0m";
    }
    long long ans = 0;
    for (long long i = 1; i <= (n + m - 1) / 2; i++) {
      long long x =
          min(Cnt[i][0] + Cnt[n + m - i][0], Cnt[i][1] + Cnt[n + m - i][1]);
      debug() << "\033[94m ["
              << "i"
                 ": "
              << (i)
              << "]\033[0m"
                 "\033[94m ["
              << "x"
                 ": "
              << (x) << "]\033[0m";
      ans += x;
    }
    cout << ans << '\n';
  }
}
