#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma warning(disable : 4996)
using namespace std;
clock_t start_time, end_time;
void open() {
  ((void)0);
  ((void)0);
  ((void)0);
  ((void)0);
}
void close() {
  ((void)0);
  ((void)0);
}
const int MAX = 1e5 + 1;
const int MOD = 1e9 + 7;
const int INF = 0x3f3f3f3f;
const long long LL_INF = 0x3f3f3f3f3f3f3f3fLL;
const double PI = acos(-1);
const long double ERR = 1e-10;
const int move_r[] = {-1, 0, 1, 0, -1, -1, 1, 1};
const int move_c[] = {0, -1, 0, 1, -1, 1, -1, 1};
int t, n, m;
long long a[101][101];
vector<pair<long long, long long> > dp[101][101];
pair<long long, long long> process(long long& height,
                                   pair<long long, long long> prv, int depth) {
  long long diff = height + 1 - prv.first;
  if (diff >= 0)
    return {prv.first - 1, prv.second + diff};
  else
    return {height, prv.second - diff * depth};
}
int main(int argc, char* argv[]) {
  open();
  scanf("%d", &t);
  while (t--) {
    scanf("%d %d", &n, &m);
    for (int i = n; i > 0; --i)
      for (int j = m; j > 0; --j) {
        scanf("%lld", a[i] + j);
        dp[i][j].clear();
      }
    dp[1][1].push_back({a[1][1], 0});
    for (int j = 2; j <= m; ++j)
      dp[1][j].push_back(process(a[1][j], dp[1][j - 1].back(), j - 1));
    for (int i = 2; i <= n; ++i)
      dp[i][1].push_back(process(a[i][1], dp[i - 1][1].back(), i - 1));
    for (int i = 2; i <= n; ++i) {
      for (int j = 2; j <= m; ++j) {
        auto it1 = dp[i][j - 1].begin();
        auto it2 = dp[i - 1][j].begin();
        pair<long long, long long> res;
        while (it1 != dp[i][j - 1].end() && it2 != dp[i - 1][j].end()) {
          if (it1->first <= it2->first) {
            res = process(a[i][j], *it1, i + j - 2);
            ++it1;
          } else {
            res = process(a[i][j], *it2, i + j - 2);
            ++it2;
          }
          if (!dp[i][j].empty() && dp[i][j].back().first == res.first)
            dp[i][j].back().second = min(dp[i][j].back().second, res.second);
          else
            dp[i][j].push_back(res);
        }
        while (it1 != dp[i][j - 1].end()) {
          res = process(a[i][j], *it1, i + j - 2);
          ++it1;
          if (!dp[i][j].empty() && dp[i][j].back().first == res.first)
            dp[i][j].back().second = min(dp[i][j].back().second, res.second);
          else
            dp[i][j].push_back(res);
        }
        while (it2 != dp[i - 1][j].end()) {
          res = process(a[i][j], *it2, i + j - 2);
          ++it2;
          if (!dp[i][j].empty() && dp[i][j].back().first == res.first)
            dp[i][j].back().second = min(dp[i][j].back().second, res.second);
          else
            dp[i][j].push_back(res);
        }
      }
    }
    sort(dp[n][m].begin(), dp[n][m].end(),
         [](const pair<long long, long long>& a,
            const pair<long long, long long>& b) {
           return a.second < b.second;
         });
    printf("%lld\n", dp[n][m][0].second);
  }
  close();
  return 0;
}
