#include <bits/stdc++.h>
using namespace std;
int dx[] = {0, 0, 1, -1, -1, -1, 1, 1};
int dy[] = {1, -1, 0, 0, -1, 1, 1, -1};
template <class T>
inline T biton(T n, T pos) {
  return n | ((T)1 << pos);
}
template <class T>
inline T bitoff(T n, T pos) {
  return n & ~((T)1 << pos);
}
template <class T>
inline T ison(T n, T pos) {
  return (bool)(n & ((T)1 << pos));
}
template <class T>
inline T gcd(T a, T b) {
  while (b) {
    a %= b;
    swap(a, b);
  }
  return a;
}
inline int nxt() {
  int aaa;
  scanf("%d", &aaa);
  return aaa;
}
inline long long lxt() {
  long long aaa;
  scanf("%lld", &aaa);
  return aaa;
}
inline double dxt() {
  double aaa;
  scanf("%lf", &aaa);
  return aaa;
}
template <class T>
inline T bigmod(T p, T e, T m) {
  T ret = 1;
  for (; e > 0; e >>= 1) {
    if (e & 1) ret = (ret * p) % m;
    p = (p * p) % m;
  }
  return (T)ret;
}
template <class T>
istream& operator>>(istream& is, vector<T>& input) {
  for (T& in : input) is >> in;
  return is;
}
vector<vector<int> > v;
int n, m;
bool isValid(int x, int y) {
  if (x >= 0 && x < n && y >= 0 && y < m) return true;
  return false;
}
vector<pair<int, int> > bfs() {
  queue<pair<int, int> > q;
  q.push({0, 0});
  vector<pair<int, int> > ans(n * m + 5, pair<int, int>(0, 0));
  vector<vector<int> > vis(n, vector<int>(m, -1));
  vis[0][0] = 0;
  while (!q.empty()) {
    auto out = q.front();
    q.pop();
    for (int i = 0; i <= 2; i += 2) {
      int x = out.first + dx[i];
      int y = out.second + dy[i];
      if (isValid(x, y) && vis[x][y] == -1) {
        vis[x][y] = vis[out.first][out.second] + 1;
        ans[vis[x][y]].first += (v[x][y] == 1);
        ans[vis[x][y]].second += (v[x][y] == 0);
        q.push({x, y});
      }
    }
  }
  return ans;
}
vector<pair<int, int> > bfs1() {
  queue<pair<int, int> > q;
  q.push({n - 1, m - 1});
  vector<pair<int, int> > ans(n * m + 5, pair<int, int>(0, 0));
  vector<vector<int> > vis(n, vector<int>(m, -1));
  vis[n - 1][m - 1] = 0;
  while (!q.empty()) {
    auto out = q.front();
    q.pop();
    for (int i = 1; i <= 3; i += 2) {
      int x = out.first + dx[i];
      int y = out.second + dy[i];
      if (isValid(x, y) && vis[x][y] == -1) {
        vis[x][y] = vis[out.first][out.second] + 1;
        ans[vis[x][y]].first += (v[x][y] == 1);
        ans[vis[x][y]].second += (v[x][y] == 0);
        q.push({x, y});
      }
    }
  }
  return ans;
}
void solve() {
  n = nxt(), m = nxt();
  v.assign(n, vector<int>(m));
  cin >> v;
  vector<pair<int, int> > a, b;
  a = bfs();
  b = bfs1();
  int ans = 0;
  for (int i = 1; i <= (n + m - 3) / 2; i++) {
    ans += min(a[i].first + b[i].first, a[i].second + b[i].second);
  }
  ans += (v[0][0] != v[n - 1][m - 1]);
  cout << ans << '\n';
}
int main() {
  cout.setf(ios::fixed);
  cout.precision(10);
  int tc = nxt();
  while (tc--) {
    solve();
  }
  return 0;
}
