#include <bits/stdc++.h>
using namespace std;
const double eps = 1e-9;
const int inf = 2000000000;
const long long infLL = 9000000000000000000;
template <typename first, typename second>
ostream& operator<<(ostream& os, const pair<first, second>& p) {
  return os << "(" << p.first << ", " << p.second << ")";
}
template <typename T>
ostream& operator<<(ostream& os, const vector<T>& v) {
  os << "{";
  for (auto it = v.begin(); it != v.end(); ++it) {
    if (it != v.begin()) os << ", ";
    os << *it;
  }
  return os << "}";
}
template <typename T>
ostream& operator<<(ostream& os, const set<T>& v) {
  os << "[";
  for (auto it = v.begin(); it != v.end(); ++it) {
    if (it != v.begin()) os << ",";
    os << *it;
  }
  return os << "]";
}
template <typename T>
ostream& operator<<(ostream& os, const multiset<T>& v) {
  os << "[";
  for (auto it = v.begin(); it != v.end(); ++it) {
    if (it != v.begin()) os << ", ";
    os << *it;
  }
  return os << "]";
}
template <typename first, typename second>
ostream& operator<<(ostream& os, const map<first, second>& v) {
  os << "[";
  for (auto it = v.begin(); it != v.end(); ++it) {
    if (it != v.begin()) os << ", ";
    os << it->first << " = " << it->second;
  }
  return os << "]";
}
void faltu() { cerr << '\n'; }
template <typename T>
void faltu(T a[], int n) {
  for (int i = 0; i < n; ++i) cerr << a[i] << ' ';
  cerr << '\n';
}
template <typename T, typename... hello>
void faltu(T arg, const hello&... rest) {
  cerr << arg << ' ';
  faltu(rest...);
}
int n, m;
int a[35][35];
int one1[100], one2[100];
int zero1[100], zero2[100];
int total1[100], total2[100];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  int t;
  cin >> t;
  while (t--) {
    memset(one1, 0, sizeof(one1));
    memset(one2, 0, sizeof(one2));
    memset(zero1, 0, sizeof(zero1));
    memset(zero2, 0, sizeof(zero2));
    memset(total1, 0, sizeof(total1));
    memset(total2, 0, sizeof(total2));
    cin >> n >> m;
    for (int i = 1; i <= n; ++i) {
      for (int j = 1; j <= m; ++j) {
        cin >> a[i][j];
        if (a[i][j]) {
          int temp = abs(1 - i) + abs(1 - j);
          ++one1[temp];
          ++total1[temp];
          temp = abs(n - i) + abs(m - j);
          ++one2[temp];
          ++total2[temp];
        } else {
          int temp = abs(1 - i) + abs(1 - j);
          ++zero1[temp];
          ++total1[temp];
          temp = abs(n - i) + abs(m - j);
          ++zero2[temp];
          ++total2[temp];
        }
      }
    }
    int ans = 0;
    int hmm = (n - 1) + (m - 1);
    int half = (hmm + 1) / 2;
    for (int i = 0; i < half; ++i) {
      int ul = total1[i] + total2[i];
      int temp1 = ul - one1[i] - one2[i];
      int temp2 = ul - zero1[i] - zero2[i];
      int temp = min(temp1, temp2);
      ans += temp;
    }
    cout << ans << '\n';
  }
}
