#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int MAXN = 100;
const int dx[] = {0, 0, -1, 1};
const int dy[] = {-1, 1, 0, 0};
int T, N, M, a[MAXN][MAXN];
int main() {
scanf("%d", &T);
for (int kase = 1; kase <= T; kase++) {
scanf("%d %d", &N, &M);
memset(a, 0, sizeof(a));
int ans1 = 0, ans2 = 0;
for (int i = 1; i <= N; i++) {
for (int j = 1; j <= M; j++) {
scanf("%d", &a[i][j]);
if (a[i][j]) ans1++;
}
}
for (int i = 1; i <= N; i++) {
for (int j = 1; j <= M; j++) {
for (int k = 0; k < 4; k++) {
int x = i + dx[k], y = j + dy[k];
if (a[x][y] < a[i][j]) ans1 += a[i][j] - a[x][y];
}
}
}
printf("%d\n", ans1 + ans2);
}
return 0;
}