#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long i, j, k, r, c, c1, c2, cnt, ans, x, y, p, t;
  vector<vector<long long>> v, a, b;
  unordered_map<long long, set<pair<long long, long long>>> m;
  vector<long long> re, q;
  cin >> t;
  i = 0;
  while (i < t) {
    cin >> r >> c;
    j = 0;
    while (j < r) {
      p = 0;
      while (p < c) {
        cin >> k;
        q.push_back(0);
        re.push_back(k);
        p++;
      }
      a.push_back(q);
      b.push_back(q);
      q.clear();
      v.push_back(re);
      re.clear();
      j++;
    }
    j = 0;
    a[0][0] = 0;
    m[0].insert(make_pair(0, 0));
    while (j < r) {
      p = 0;
      while (p < c) {
        if (j + 1 < r) {
          a[j + 1][p] = 1 + a[j][p];
          m[1 + a[j][p]].insert(make_pair(j + 1, p));
        }
        if (p + 1 < c) {
          a[j][p + 1] = 1 + a[j][p];
          m[1 + a[j][p]].insert(make_pair(j, p + 1));
        }
        p++;
      }
      j++;
    }
    j = r - 1;
    b[r - 1][c - 1] = 0;
    m[0].insert(make_pair(r - 1, c - 1));
    while (j >= 0) {
      p = c - 1;
      while (p >= 0) {
        if (j - 1 >= 0) {
          b[j - 1][p] = 1 + b[j][p];
          m[1 + b[j][p]].insert(make_pair(j - 1, p));
        }
        if (p - 1 >= 0) {
          b[j][p - 1] = 1 + b[j][p];
          m[1 + b[j][p]].insert(make_pair(j, p - 1));
        }
        p--;
      }
      j--;
    }
    unordered_map<long long, set<pair<long long, long long>>>::iterator it;
    set<pair<long long, long long>>::iterator u;
    set<pair<long long, long long>> s;
    it = m.begin();
    cnt = 0;
    if ((r + c - 2) % 2 == 0) {
      k = (r + c - 2) / 2;
    }
    while (it != m.end()) {
      c1 = 0;
      c2 = 0;
      if (it->first != k) {
        s = it->second;
        u = s.begin();
        while (u != s.end()) {
          if (v[(*u).first][(*u).second] == 0) {
            c1++;
          } else {
            c2++;
          }
          u++;
        }
        if (c1 <= c2) {
          u = s.begin();
          while (u != s.end()) {
            if (v[(*u).first][(*u).second] == 0) {
              v[(*u).first][(*u).second] = 1;
            }
            u++;
          }
          cnt = cnt + c1;
        } else {
          u = s.begin();
          while (u != s.end()) {
            if (v[(*u).first][(*u).second] == 1) {
              v[(*u).first][(*u).second] = 0;
            }
            u++;
          }
          cnt = cnt + c2;
        }
      }
      it++;
    }
    cout << cnt << endl;
    v.clear();
    a.clear();
    b.clear();
    re.clear();
    m.clear();
    q.clear();
    i++;
  }
  return (0);
}
