#include <bits/stdc++.h>
using namespace std;
inline long long axlb(long long a, long long b) {
  return (b % a == 0 ? (b / a) - 1 : b / a);
}
inline long long axgeb(long long a, long long b) {
  return (b % a == 0 ? b / a : (b / a) + 1);
}
inline long long axgb(long long a, long long b) { return (b / a) + 1; }
inline long long axleb(long long a, long long b) { return b / a; }
inline void printyn(bool c) {
  cout << (c ? "YES" : "NO") << "\n";
  return;
}
template <typename T>
inline void scanarr(T* arr, long long n) {
  for (long long i = 0; i < n; i++) {
    cin >> arr[i];
  }
}
template <typename T>
void scanvec(vector<T>* vec, long long n) {
  vec->reserve(n + 10);
  for (long long i = 0; i < n; i++) {
    T temp;
    cin >> temp;
    vec->push_back(temp);
  }
  return;
}
template <typename T>
inline void printarr(T* arr, long long n) {
  for (long long i = 0; i < n; i++) {
    cout << arr[i] << " ";
  }
  cout << "\n";
}
template <typename T>
inline void printvec(vector<T>* vec) {
  long long n = vec->size();
  for (long long i = 0; i < n; i++) {
    cout << vec->at(i) << " ";
  }
  cout << "\n";
}
long long solve(long long mat[][200], long long r, long long c) {
  long long i = 0, j = r - 1;
  long long ans = 0;
  while (i <= j) {
    long long x = 0, y = c - 1;
    while (x <= y) {
      if (i == j) {
        long long p = abs(mat[i][x] - mat[i][y]);
        ans += p;
      } else if (x == y) {
        long long p = abs(mat[i][x] - mat[j][x]);
        ans += p;
      } else {
        long long p = abs(mat[i][y] - mat[i][x]) + abs(mat[j][y] - mat[i][x]) +
                      abs(mat[j][x] - mat[i][x]);
        long long q = abs(mat[i][y] - mat[i][x]) + abs(mat[i][y] - mat[j][x]) +
                      abs(mat[i][y] - mat[j][y]);
        long long l = abs(mat[j][x] - mat[i][x]) + abs(mat[j][x] - mat[i][y]) +
                      abs(mat[j][x] - mat[j][y]);
        long long s = abs(mat[j][y] - mat[i][x]) + abs(mat[j][y] - mat[j][x]) +
                      abs(mat[j][y] - mat[i][y]);
        long long temp = ((p > q ? q : p) > l ? l : (p > q ? q : p));
        long long increment = (temp > s ? s : temp);
        ans += increment;
      }
      x++;
      y--;
    }
    i++;
    j--;
  }
  return ans;
}
int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long T;
  cin >> T;
  while (T--) {
    long long n, m;
    cin >> n >> m;
    long long mat[200][200];
    for (long long i = 0; i < n; i++) {
      for (long long j = 0; j < m; j++) {
        cin >> mat[i][j];
      }
    }
    cout << solve(mat, n, m) << "\n";
  }
}
