#include #include #include #include #include #include #include #include using namespace std; typedef long long LL; int T,n,m,a[1005][1005]; int num,q[1005]; LL mod=1; int l[1005][1005],r[1005][1005]; int u[1005][1005],d[1005][1005]; int main() { scanf("%d",&T); for (int i=1;i<=32;++i) mod*=2; while (T--) { 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) { num=0;q[0]=0; for (int j=1;j<=m;++j) { while (num>0&&a[i][q[num]]>a[i][j]) --num; l[i][j]=q[num]+1; while (num>0&&a[i][q[num]]>=a[i][j]) --num; ++num; q[num]=j; } num=0;q[0]=m+1; for (int j=m;j>=1;--j) { while (num>0&&a[i][q[num]]>a[i][j]) --num; r[i][j]=q[num]-1; while (num>0&&a[i][q[num]]>=a[i][j]) --num; ++num; q[num]=j; } } for (int j=1;j<=m;++j) { num=0;q[0]=0; for (int i=1;i<=n;++i) { while (num>0&&a[q[num]][j]0&&a[q[num]][j]<=a[i][j]) --num; ++num; q[num]=i; } num=0;q[0]=n+1; for (int i=n;i>=1;--i) { while (num>0&&a[q[num]][j]0&&a[q[num]][j]<=a[i][j]) --num; ++num; q[num]=i; } } unsigned ans=0; for (int i=1;i<=n;++i) for (int j=1;j<=m;++j) { LL aa,bb,now1,now2; aa=j-l[i][j]; bb=r[i][j]-j; if (aa%2==1) { LL dd=(aa+1)*(bb+1)%mod; LL cc=(aa+1)/2*(aa+bb); cc%=mod; cc=cc*(bb+1)%mod; LL ee=cc+dd; ee%=mod; now1=ee; } else { LL dd=(aa+1)*(bb+1)%mod; LL cc=(bb+1)*(aa+bb)/2; cc%=mod; cc=cc*(aa+1)%mod; LL ee=cc+dd; ee%=mod; now1=ee; } aa=i-u[i][j]; bb=d[i][j]-i; if (aa%2==1) { LL dd=(aa+1)*(bb+1)%mod; LL cc=(aa+1)/2*(aa+bb); cc%=mod; cc=cc*(bb+1)%mod; LL ee=cc+dd; ee%=mod; now2=ee; } else { LL dd=(aa+1)*(bb+1)%mod; LL cc=(bb+1)*(aa+bb)/2; cc%=mod; cc=cc*(aa+1)%mod; LL ee=cc+dd; ee%=mod; now2=ee; } unsigned xx=now1,yy=now2; unsigned now=xx*yy; unsigned dq=a[i][j]; now=now*dq; ans=ans+now; } printf("%u\n",ans); } return 0; }