#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ;
  int t;
  cin >> t;
  while (t--) {
    int n, m;
    cin >> n >> m;
    int arr[n + 5][m + 5], brr[n + 5][m + 5];
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= m; j++) {
        cin >> arr[i][j];
        brr[i][j] = 0;
      }
    }
    map<pair<int, int>, int> mp;
    int ans = 0, start = 0;
    for (int i = 1; i <= n; i++) {
      start++;
      for (int ii = i, j = 1; ii >= 1 and j <= m; ii--, j++) {
        brr[ii][j] = start;
        mp[{ii, j}] = 1;
      }
    }
    for (int i = 2; i <= m; i++) {
      if (mp[{n, i}]) continue;
      start++;
      for (int ii = n, j = i; ii >= 1 and j <= m; ii--, j++) {
        mp[{ii, j}] = 1;
        brr[ii][j] = start;
      }
    }
    map<int, int> mp1, mp2;
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= m; j++) {
        int x = brr[i][j], y = start - x + 1;
        if (x > y) swap(x, y);
        mp1[x] += arr[i][j];
        mp2[x]++;
      }
    }
    start /= 2;
    for (int i = 1; i <= start; i++) {
      int one = mp1[i], total = mp2[i], zero = total - one;
      ans += min(one, zero);
    }
    cout << ans << "\n";
  }
  return 0;
}
