#include <bits/stdc++.h>
using namespace std;
template <typename T>
ostream &operator<<(ostream &os, const vector<T> &V) {
  long long N = V.size();
  if (N == 0) {
    os << '.';
    return os;
  }
  for (long long i = 0; i < N - 1; i++) {
    os << V[i] << ' ';
  }
  os << V[N - 1];
  return os;
}
template <typename T>
ostream &operator<<(ostream &os, const vector<vector<T>> &V) {
  long long N = V.size();
  for (long long i = 0; i < N - 1; i++) os << V[i] << "\n";
  os << V[N - 1];
  return os;
}
template <typename T, typename S>
ostream &operator<<(ostream &os, pair<T, S> const &P) {
  os << P.first << ' ' << P.second;
  return os;
}
template <typename T>
void Vin(vector<T> &v) {
  long long n = v.size();
  for (long long i = 0; i < n; i++) cin >> v[i];
}
long long scomb(long long n, long long r) {
  if (r < 0 || r > n) return 0;
  if ((n - r) < r) r = n - r;
  long long a = 1;
  for (long long i = n; i > n - r; --i) {
    a = a * i;
  }
  for (long long i = 1; i < r + 1; ++i) {
    a = a / i;
  }
  return a;
}
long long digit_sum(long long n) {
  long long ret = 0;
  while (n > 0) {
    ret += n % 10;
    n /= 10;
  }
  return ret;
}
long long digit(long long k, long long i) {
  string s = to_string(k);
  return s[s.size() - i] - '0';
}
template <typename T>
void press(T &v) {
  v.erase(unique((v).begin(), (v).end()), v.end());
}
long long SMALLER(vector<long long> &a, long long x) {
  return lower_bound(a.begin(), a.end(), x) - a.begin();
}
long long orSMALLER(vector<long long> &a, long long x) {
  return upper_bound(a.begin(), a.end(), x) - a.begin();
}
long long BIGGER(vector<long long> &a, long long x) {
  return a.size() - orSMALLER(a, x);
}
long long orBIGGER(vector<long long> &a, long long x) {
  return a.size() - SMALLER(a, x);
}
long long COUNT(vector<long long> &a, long long x) {
  return upper_bound((a).begin(), (a).end(), x) -
         lower_bound((a).begin(), (a).end(), x);
}
long long maxind(vector<long long> &a) {
  return max_element((a).begin(), (a).end()) - a.begin();
}
long long minind(vector<long long> &a) {
  return min_element((a).begin(), (a).end()) - a.begin();
}
template <typename T>
bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T>
bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
vector<long long> vis(vector<long long> &v) {
  vector<long long> S(v.size() + 1);
  for (long long i = 1; i < S.size(); i++) S[i] += v[i - 1] + S[i - 1];
  return S;
}
const long long MOD = 1000000007;
const double EPS = 1e-10;
void init() {
  cin.tie(0);
  cout.tie(0);
  ios::sync_with_stdio(0);
  cout << fixed << setprecision(12);
}
using XX = long long;
XX INF_X() { return 3000000000000000000; }
XX ZERO_X() { return 0; }
struct edge {
  long long to;
  XX cost;
};
struct graph {
  long long N;
  vector<vector<edge>> G;
  vector<XX> dist;
  vector<long long> prever;
  graph(long long n) { init(n); }
  void init(long long n) {
    N = n;
    G.resize(N);
    dist.resize(N, INF_X());
  }
  void add_edge(long long s, long long t, XX cost) {
    edge e;
    e.to = t, e.cost = cost;
    G[s].push_back(e);
  }
  XX add(XX a, XX b) { return a + b; }
  void dijkstra(long long s) {
    for (long long i = 0; i < N; i++) dist[i] = INF_X();
    prever = vector<long long>(N, -1);
    dist[s] = ZERO_X();
    priority_queue<pair<XX, long long>, vector<pair<XX, long long>>,
                   greater<pair<XX, long long>>>
        q;
    q.push(make_pair(ZERO_X(), s));
    while (!q.empty()) {
      long long now;
      XX nowdist;
      tie(nowdist, now) = q.top();
      q.pop();
      if (dist[now] < nowdist) continue;
      for (auto e : G[now]) {
        if (dist[e.to] > add(dist[now], e.cost)) {
          prever[e.to] = now;
          dist[e.to] = add(dist[now], e.cost);
          q.push(make_pair(dist[e.to], e.to));
        }
      }
    }
  }
  bool bellman_ford(long long s) {
    for (long long i = 0; i < N; i++) {
      dist[i] = INF_X();
    }
    dist[s] = ZERO_X();
    for (long long i = 0; i < N; i++) {
      for (long long j = 0; j < N; j++) {
        for (auto e : G[j]) {
          if (dist[e.to] > add(dist[j], e.cost)) {
            dist[e.to] = add(dist[j], e.cost);
            if (i == N - 1) {
              return false;
            }
          }
        }
      }
    }
    return true;
  }
  vector<long long> get_path(long long t) {
    if (dist[t] >= 3000000000000000000 / 2) return {-1};
    vector<long long> path;
    for (; t != -1; t = prever[t]) {
      path.push_back(t);
    }
    reverse(path.begin(), path.end());
    return path;
  }
};
long long g(vector<vector<long long>> &a, long long K) {
  long long h = a.size(), w = a[0].size();
  vector<vector<long long>> b(h, vector<long long>(w));
  vector<vector<long long>> c = a;
  for (long long i = 0; i < h; i++)
    for (long long j = 0; j < w; j++) {
      if (a[i][j] < K)
        c[i][j] = 3000000000000000000;
      else
        c[i][j] = a[i][j] - K;
    }
  b[0][0] = c[0][0];
  for (long long i = 0; i < h; i++)
    for (long long j = 0; j < w; j++) {
      if (i == 0 && j == 0)
        continue;
      else if (i == 0)
        b[i][j] = b[i][j - 1] + c[i][j];
      else if (j == 0)
        b[i][j] = b[i - 1][j] + c[i][j];
      else
        b[i][j] = min(b[i][j - 1] + c[i][j], b[i - 1][j] + c[i][j]);
      if (b[i][j] >= 3000000000000000000) b[i][j] = 3000000000000000000;
    }
  return b[h - 1][w - 1];
}
long long f(vector<vector<long long>> &a, long long K, long long I,
            long long J) {
  long long h = a.size();
  long long w = a[0].size();
  vector<vector<long long>> A(I + 1, vector<long long>(J + 1)),
      B(h - I, vector<long long>(w - J));
  for (long long i = 0; i <= I; i++)
    for (long long j = 0; j <= J; j++) {
      A[i][j] = a[i][j];
    }
  for (long long i = I; i < h; i++)
    for (long long j = J; j < w; j++) {
      B[i - I][j - J] = a[i][j];
    }
  long long ans = g(A, K) + g(B, K);
  if (ans >= 3000000000000000000) ans = 3000000000000000000;
  return ans;
}
void sol() {
  long long h, w;
  cin >> h >> w;
  vector<vector<long long>> a(h, vector<long long>(w));
  for (long long i = 0; i < h; i++)
    for (long long j = 0; j < w; j++) cin >> a[i][j];
  long long tmp = a[0][0];
  for (long long i = 0; i < h; i++)
    for (long long j = 0; j < w; j++) {
      a[i][j] -= tmp;
      a[i][j] -= i + j;
    }
  tmp = 3000000000000000000;
  for (long long i = 0; i < h; i++)
    for (long long j = 0; j < w; j++) chmin(tmp, a[i][j]);
  for (long long i = 0; i < h; i++)
    for (long long j = 0; j < w; j++) a[i][j] -= tmp;
  long long ans = 3000000000000000000;
  for (long long i = 0; i < h; i++)
    for (long long j = 0; j < w; j++) {
      chmin(ans, f(a, a[i][j], i, j));
    }
  cout << ans << "\n";
}
signed main() {
  init();
  long long q = 1;
  cin >> q;
  while (q--) sol();
  return 0;
}
