#include #include #include #include #include using namespace std; #define N 1020 #define M 200020 #define mod 1000000007 #define LL long long #define ls (i << 1) #define rs (ls | 1) #define md (ll + rr >> 1) #define lson ll, md, ls #define rson md + 1, rr, rs #define ui unsigned int int readint() { char c; while((c = getchar()) && !(c >= '0' && c <= '9')); int ret = c - 48; while((c = getchar()) && (c >= '0' && c <= '9')) ret = ret * 10 + c - 48; return ret; } int n, m, a[N][N]; int stk[N], top; int L[N][N], R[N][N], U[N][N], D[N][N]; ui calc(int l, int x, int r) { ui d1 = r - x + 1; ui d2 = x - l + 1; ui ret = d1 * (d1 + 1) / 2 * d2 + d2 * (d2 - 1) / 2 * d1; return ret; } void debug(int a[N][N]) { for(int i = 1; i <= n; ++i) { for(int j = 1; j <= m; ++j) { printf("%d%c", a[i][j], j == m ? '\n': ' '); } } } int main() { int cas; scanf("%d", &cas); while(cas--) { scanf("%d%d", &n, &m); for(int i = 1; i <= n; ++i) { for(int j = 1; j <= m; ++j) { a[i][j] = readint(); } } for(int i = 1; i <= n; ++i) { top = 0; for(int j = 1; j <= m; ++j) { while(top && a[i][stk[top-1]] > a[i][j]) -- top; if(top == 0) L[i][j] = 1; else L[i][j] = stk[top-1] + 1; stk[top++] = j; } } for(int i = 1; i <= n; ++i) { top = 0; for(int j = m; j >= 1; --j) { while(top && a[i][stk[top-1]] > a[i][j]) --top; if(top == 0) R[i][j] = m; else R[i][j] = stk[top-1] - 1; stk[top++] = j; } } for(int i = 1; i <= m; ++i) { top = 0; for(int j = 1; j <= n; ++j) { while(top && a[stk[top-1]][i] < a[j][i]) --top; if(top == 0) U[j][i] = 1; else U[j][i] = stk[top-1] + 1; stk[top++] = j; } top = 0; for(int j = n; j >= 1; --j) { while(top && a[stk[top-1]][i] < a[j][i]) --top; if(top == 0) D[j][i] = n; else D[j][i] = stk[top-1] - 1; stk[top++] = j; } } ui ans = 0; for(int i = 1; i <= n; ++i) { for(int j = 1; j <= m; ++j) { ans += (ui)a[i][j] * calc(L[i][j], j, R[i][j]) * calc(U[i][j], i, D[i][j]); } } printf("%u\n", ans); } return 0; }