#include #include #include #include #include #include #include #include #include #include #include #include #define LL long long #define inf 0x7ffffff #define pa pair #define pi 3.1415926535897932384626433832795028841971 #define mx 10000000 using namespace std; inline LL read() { LL x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f; } inline void write(LL a) { if (a<0){printf("-");a=-a;} if (a>=10)write(a/10); putchar(a%10+'0'); } int T,n,m; LL a[1010][1010]; LL L[1010][1010]; LL R[1010][1010]; LL U[1010][1010]; LL D[1010][1010]; LL ans,md=4294967296ll; inline void calc(int x,int y) { LL l=y-L[x][y],r=R[x][y]-y,u=x-U[x][y],d=D[x][y]-x; ans+=(LL)a[x][y]*l*r*u*d*(l+r)*(u+d)/4; ans%=md; } inline void work() { int n=read(),m=read();ans=0; for(int i=1;i<=n;i++) for(int j=1;j<=m;j++)a[i][j]=read(); stackst; for(int i=1;i<=n;i++) { a[i][0]=a[i][m+1]=-inf; while(!st.empty())st.pop();st.push(0); for(int j=1;j<=m;j++) { while(!st.empty()&&a[i][st.top()]>a[i][j])st.pop(); L[i][j]=st.top(); st.push(j); } while(!st.empty())st.pop();st.push(m+1); for(int j=m;j>=1;j--) { while(!st.empty()&&a[i][st.top()]>a[i][j])st.pop(); R[i][j]=st.top(); st.push(j); } } for(int i=1;i<=m;i++) { a[0][i]=a[n+1][i]=inf; while(!st.empty())st.pop();st.push(0); for(int j=1;j<=n;j++) { while(!st.empty()&&a[st.top()][i]=1;j--) { while(!st.empty()&&a[st.top()][i]