#include <bits/stdc++.h>
using namespace std;
int minu(int a, int b) { return (a < b) ? a : b; }
int min(int a, int b, int c) { return minu(minu(a, b), c); }
int max(int a, int b) { return (a > b) ? a : b; }
int diagonalOrder(int arr[35][35], int ROW, int COL,
                  vector<pair<int, int>> ans) {
  int m = COL, n = ROW;
  for (int line = 1; line <= (ROW + COL - 1); line++) {
    int start_col = max(0, line - ROW);
    int count = min(line, (COL - start_col), ROW);
    int count0 = 0, count1 = 0;
    for (int j = 0; j < count; j++) {
      if (arr[minu(ROW, line) - j - 1][start_col + j])
        count1++;
      else
        count0++;
    }
    ans.push_back({count0, count1});
  }
  int count = 0;
  for (int i = 0; i < (m + n) / 2; i++) {
    int s1 = 0, s0 = 0;
    if (m + n - i - 2 != i) {
      s1 = ans[i].second + ans[m + n - i - 2].second;
      s0 = ans[i].first + ans[m + n - i - 2].first;
    }
    count += min(s1, s0);
  }
  return count;
}
int main() {
  int t;
  cin >> t;
  while (t--) {
    long long int m, n;
    cin >> m >> n;
    int arr[35][35];
    for (int i = 0; i < m; i++) {
      for (int j = 0; j < n; j++) {
        cin >> arr[i][j];
      }
    }
    vector<pair<int, int>> ans;
    int final = diagonalOrder(arr, m, n, ans);
    cout << final << endl;
  }
  return 0;
}
