#include <bits/stdc++.h>
using namespace std;
template <typename T>
T modpow(T base, T exp, T modulus) {
  base %= modulus;
  T result = 1;
  while (exp > 0) {
    if (exp & 1) result = (result * base) % modulus;
    base = (base * base) % modulus;
    exp >>= 1;
  }
  return result;
}
struct cmp {
  bool operator()(const pair<int, int> &a, const pair<int, int> &b) const {
    int lena = a.second - a.first + 1;
    int lenb = b.second - b.first + 1;
    if (lena == lenb) return a.first < b.first;
    return lena > lenb;
  }
};
long long calcpow(long long x, long long y) {
  long long temp;
  if (y == 0) return 1;
  temp = calcpow(x, y / 2);
  if (y % 2 == 0)
    return temp * temp;
  else {
    if (y > 0)
      return x * temp * temp;
    else
      return (temp * temp) / x;
  }
}
long long gcd(long long a, long long b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
long long egcd(long long a, long long b, long long &x, long long &y) {
  if (b == 0) {
    x = 1;
    y = 0;
    return a;
  }
  long long x1;
  long long y1;
  long long g = egcd(b, a % b, x1, y1);
  x = y1;
  y = x1 - y1 * (a / b);
  return g;
}
bool isprime(long long a) {
  bool flag = 1;
  for (long long i = 2; i * i <= a; i++) {
    if (a % i == 0) {
      flag = 0;
      break;
    }
  }
  return flag;
}
long long N = 1e7 + 6;
int main() {
  long long t;
  cin >> t;
  while (t--) {
    long long n, m;
    cin >> n >> m;
    vector<vector<long long> > vec(n, vector<long long>(m));
    for (long long i = 0; i < n; i++)
      for (long long j = 0; j < m; j++) cin >> vec[i][j];
    long long ans = 0;
    pair<long long, long long> st = {0, 0};
    pair<long long, long long> en = {n - 1, m - 1};
    while (st.first + st.second < en.first + en.second) {
      vector<long long> vec1, vec2;
      pair<long long, long long> p1 = st;
      pair<long long, long long> p2 = en;
      long long i = 0;
      while (p1.first + i < n && p1.second - i >= 0 && p2.first + i < n &&
             p2.second - i >= 0) {
        vec1.push_back({vec[p1.first + i][p1.second - i]});
        vec2.push_back({vec[p2.first + i][p2.second - i]});
        i++;
      }
      long long o = 0;
      long long one = 0;
      long long o1 = 0;
      long long one1 = 0;
      for (auto x : vec1) {
        if (x == 0) {
          o++;
        } else {
          one++;
        }
      }
      for (auto x : vec2) {
        if (x == 0) {
          o++;
        } else {
          one++;
        }
      }
      ans += min(o, one);
      st.second++;
      en.first--;
      if (en.first == -1) {
        en.first = 0;
        en.second--;
      }
      if (st.second == m) {
        st.second = m - 1;
        st.first++;
      }
    }
    cout << ans << endl;
  }
  return 0;
}
