#include <bits/stdc++.h>
using namespace std;
long long int power(long long int x, long long int y, long long int m) {
  if (y == 0) return 1;
  long long int a = power(x, y / 2, m);
  if (y % 2) {
    return (a * ((a * x) % m)) % m;
  } else {
    return (a * a) % m;
  }
}
long long int mod_inverse(long long int x, long long int m) {
  return power(x, m - 2, m);
}
long long int fact(long long int n, long long int m) {
  if (n <= 1) return 1;
  return (fact(n - 1, m) * n) % m;
}
long long int ncr(long long int n, long long int r, long long int m) {
  if (r > n) return 0;
  long long int n1 = 1, d1 = 1, d2 = 1;
  n1 = fact(n, m);
  d1 = fact(r, m);
  d2 = fact(n - r, m);
  long long int ans = mod_inverse((d1 * d2) % m, m);
  ans = (ans * n1) % m;
  return ans;
}
int ispal(string s) {
  int len = s.size();
  int flag = 1;
  for (int i = 0; i < len; ++i) {
    if (s[i] != s[len - i - 1]) {
      flag = 0;
      break;
    }
  }
  return flag;
}
long long int sroot(long long int n, long long int low = 1,
                    long long int high = 1e9 + 1) {
  if (low == high) return low;
  if (low == high - 1) {
    if (high * high <= n)
      return high;
    else
      return low;
  }
  long long int mid = (low + high) / 2;
  long long int a = mid * mid;
  if (a > n)
    return sroot(n, low, mid - 1);
  else
    return sroot(n, mid, high);
}
long long int croot(long long int n, long long int low = 1,
                    long long int high = 1e6 + 1) {
  if (low == high) return low;
  if (low == high - 1) {
    if (high * high * high <= n)
      return high;
    else
      return low;
  }
  long long int mid = (low + high) / 2;
  long long int a = mid * mid * mid;
  if (a > n)
    return croot(n, low, mid - 1);
  else
    return croot(n, mid, high);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int test = 1;
  cin >> test;
  while (test--) {
    int n, m;
    cin >> n >> m;
    long long int a[n + 1][m + 1];
    for (int i = 1; i <= n; ++i)
      for (int j = 1; j <= m; ++j) cin >> a[i][j];
    long long int ans = -1;
    for (int ti = 1; ti <= n; ++ti) {
      for (int tj = 1; tj <= m; ++tj) {
        long long int st = a[ti][tj] - (ti - 1) - (tj - 1);
        long long int g[n + 1][m + 1];
        for (int i = 1; i <= n; ++i) {
          for (int j = 1; j <= m; ++j) {
            g[i][j] = st + (i - 1) + (j - 1);
          }
        }
        for (int i = 1; i <= n; ++i) {
          for (int j = 1; j <= m; ++j) {
            if (g[i][j] > a[i][j])
              g[i][j] = -1;
            else
              g[i][j] = a[i][j] - g[i][j];
          }
        }
        long long int dp[n + 1][m + 1];
        memset(dp, -1, sizeof(dp));
        for (int i = n; i > 0; --i) {
          for (int j = m; j > 0; --j) {
            if (i == n && j == m) {
              dp[i][j] = g[i][j];
              continue;
            }
            if (g[i][j] == -1) continue;
            int x, y;
            long long int a1 = -1, a2 = -1;
            x = i + 1;
            y = j;
            if (x <= n && y <= m) {
              if (dp[x][y] != -1) {
                a1 = dp[x][y] + g[i][j];
              }
            }
            x = i;
            y = j + 1;
            if (x <= n && y <= m) {
              if (dp[x][y] != -1) {
                a2 = dp[x][y] + g[i][j];
              }
            }
            if (a1 != -1 && a2 != -1)
              dp[i][j] = min(a1, a2);
            else if (a1 != -1)
              dp[i][j] = a1;
            else
              dp[i][j] = a2;
          }
        }
        if (ans == -1)
          ans = dp[1][1];
        else if (dp[1][1] != -1)
          ans = min(ans, dp[1][1]);
      }
    }
    cout << ans << '\n';
  }
  return 0;
}
