#include #include #include #include #include #include #include #include #include #include #include #include using namespace std; typedef long long LL; typedef pairpi; const int Maxn=1020; int Left[Maxn][Maxn],Right[Maxn][Maxn]; int up[Maxn][Maxn],down[Maxn][Maxn]; int a[Maxn][Maxn]; int n,m; unsigned cal(int a,int d,int n){ unsigned an=a+(n-1)*d; if((a+(int)an)&1){ if(n&1)puts("wa"); return (unsigned)n/2*((unsigned)a+an); } unsigned ret=((unsigned)a+an)/2; return ret*(unsigned)n; } int main(){ int _;scanf("%d",&_); while(_--){ scanf("%d%d",&n,&m); for(int i=1;i<=n;i++)for(int j=1;j<=m;j++)scanf("%d",&a[i][j]); for(int i=1;i<=n;i++){ Left[i][1]=1; for(int j=2;j<=m;j++){ int t=j; while(t!=1&&a[i][j]=1;j--){ int t=j; while(t!=m&&a[i][j]a[t-1][i])t=up[t-1][i]; up[j][i]=t; } down[n][i]=n; for(int j=n-1;j>=1;j--){ int t=j; while(t!=n&&a[j][i]>a[t+1][i])t=down[t+1][i]; down[j][i]=t; } } /* for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++)printf("%d ",up[i][j]); puts(""); } */ unsigned ans=0; for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++){ int l1=j-Left[i][j]+1,r1=Right[i][j]-j+1; int l2=i-up[i][j]+1,r2=down[i][j]-i+1; ans+=cal(cal(1,1,r1),r1,l1)*cal(cal(1,1,r2),r2,l2)*a[i][j]; // printf("ans=%u\n",ans); // printf("%d %d %d %d %d %d\n",i,j,l1,r1,l2,r2); } } printf("%u\n",ans); } return 0; }