#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n, m;
    cin >> n >> m;
    long long ar[n][m];
    long long abuff[n][m];
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        cin >> ar[i][j];
      }
    }
    long long buff;
    long long num1, num2;
    long long ans = -1;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        for (int ii = 0; ii < n; ii++) {
          for (int jj = 0; jj < m; jj++) {
            abuff[ii][jj] = -1;
          }
        }
        for (int ii = 0; ii <= i; ii++) {
          for (int jj = 0; jj <= j; jj++) {
            buff = ar[i][j] - (i - ii + j - jj);
            if (ar[ii][jj] >= buff) {
              if (ii == 0 && jj == 0) {
                abuff[ii][jj] = ar[ii][jj] - buff;
              } else if (ii == 0) {
                if (abuff[ii][jj - 1] == -1)
                  abuff[ii][jj] = -1;
                else {
                  abuff[ii][jj] = ar[ii][jj] - buff;
                  abuff[ii][jj] += abuff[ii][jj - 1];
                }
              } else if (jj == 0) {
                if (abuff[ii - 1][jj] == -1)
                  abuff[ii][jj] = -1;
                else {
                  abuff[ii][jj] = ar[ii][jj] - buff;
                  abuff[ii][jj] += abuff[ii - 1][jj];
                }
              } else {
                if (abuff[ii][jj - 1] == -1)
                  num1 = -1;
                else {
                  num1 = ar[ii][jj] - buff;
                  num1 += abuff[ii][jj - 1];
                }
                if (abuff[ii - 1][jj] == -1)
                  num2 = -1;
                else {
                  num2 = ar[ii][jj] - buff;
                  num2 += abuff[ii - 1][jj];
                }
                if (num1 == -1 && num2 == -1)
                  abuff[ii][jj] = -1;
                else if (num1 == -1)
                  abuff[ii][jj] = num2;
                else if (num2 == -1)
                  abuff[ii][jj] = num1;
                else
                  abuff[ii][jj] = min(num1, num2);
              }
            }
          }
        }
        if (abuff[i][j] != -1) {
          for (int ii = i; ii < n; ii++) {
            for (int jj = j; jj < m; jj++) {
              buff = ar[i][j] + ii - i + jj - j;
              if (ar[ii][jj] < buff) continue;
              if (ii == i && jj == j)
                continue;
              else if (ii == i) {
                if (abuff[ii][jj - 1] == -1)
                  abuff[ii][jj] = -1;
                else {
                  abuff[ii][jj] = ar[ii][jj] - buff;
                  abuff[ii][jj] += abuff[ii][jj - 1];
                }
              } else if (jj == j) {
                if (abuff[ii - 1][jj] == -1)
                  abuff[ii][jj] = -1;
                else {
                  abuff[ii][jj] = ar[ii][jj] - buff;
                  abuff[ii][jj] += abuff[ii - 1][jj];
                }
              } else {
                if (abuff[ii][jj - 1] == -1)
                  num1 = -1;
                else {
                  num1 = ar[ii][jj] - buff;
                  num1 += abuff[ii][jj - 1];
                }
                if (abuff[ii - 1][jj] == -1)
                  num2 = -1;
                else {
                  num2 = ar[ii][jj] - buff;
                  num2 += abuff[ii - 1][jj];
                }
                if (num1 == -1 && num2 == -1)
                  abuff[ii][jj] = -1;
                else if (num1 == -1)
                  abuff[ii][jj] = num2;
                else if (num2 == -1)
                  abuff[ii][jj] = num1;
                else
                  abuff[ii][jj] = min(num1, num2);
              }
            }
          }
          if (abuff[n - 1][m - 1] != -1) {
            if (ans == -1)
              ans = abuff[n - 1][m - 1];
            else
              ans = min(ans, abuff[n - 1][m - 1]);
          }
        } else
          continue;
      }
    }
    cout << ans << endl;
  }
}
