#include <bits/stdc++.h>
namespace io {
template <typename T>
inline void read(T& _x) {
  int f = 0, ch;
  _x = 0;
  while (!isdigit(ch = getchar())) f |= ch == '-';
  while (isdigit(ch)) _x = _x * 10 + ch - '0', ch = getchar();
  if (f) _x = -_x;
}
template <typename T, typename... Args>
inline void read(T& _f, Args&... args) {
  read(_f), read(args...);
}
inline void _deal(char ch) { putchar(ch); }
template <typename T>
inline void _deal(T _x) {
  if (_x < 0) putchar('-'), _x = -_x;
  if (_x > 9) _deal(_x / 10);
  putchar(_x % 10 + '0');
}
inline void write() {}
template <typename T, typename... Args>
inline void write(T _f, Args... args) {
  _deal(_f), write(args...);
}
}  // namespace io
const int N = 30 + 5;
const int M = 30 + 5;
int n, m, a[N][M], sum[N + M][2];
int main() {
  int __tests;
  scanf("%d", &__tests);
  while (__tests--) {
    io::read(n, m);
    memset(sum, 0, sizeof(sum));
    for (int i = 1; i <= n; ++i) {
      for (int j = 1; j <= m; ++j) {
        io::read(a[i][j]);
        ++sum[i + j][a[i][j]];
      }
    }
    int ans = 0;
    for (int i = 2; i <= (n + m + 1) / 2; ++i) {
      int cnt0 = sum[i][0] + sum[n + m + 2 - i][0];
      int cnt1 = sum[i][1] + sum[n + m + 2 - i][1];
      ans += std::min(cnt0, cnt1);
    }
    io::write(ans, '\n');
  }
  return 0;
}
