#include #include #include #include #include #include #include #include #include #define inf (1<<30) #define INF (1ll<<62) #define prt(x) cout<<#x<<":"<void sc(T &x){ int f=1;x=0;char c; while(c=getchar(),c<48)if(c=='-')f=-1; do x=x*10+(c^48); while(c=getchar(),c>47); x*=f; } templatevoid nt(T x){ if(!x)return; nt(x/10);putchar('0'+x%10); } templatevoid pt(T x){ if(x<0)putchar('-'),x=-x; if(!x)putchar('0'); else nt(x); } int n,m; const int maxn=1005; int a[maxn][maxn]; int b[maxn][maxn]; int x[maxn][maxn]; int c[maxn][maxn]; int y[maxn][maxn]; ll dp[maxn][maxn]; ll sum[maxn<<1]; int stk[maxn]; int pos[maxn]; const ll mod=1ll<<32; void solve(){ sc(n);sc(m); for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) sc(a[i][j]);//b[i][j]=c[i][j]=x[i][j]=y[i][j]=0; for(int i=1;i<=n;i++){ int top=0; pos[top]=0; for(int j=1;j<=m;j++){ while(top&&stk[top]>a[i][j])top--; b[i][j]=j-pos[top]; stk[++top]=a[i][j]; pos[top]=j; } top=0; pos[top]=m+1; for(int j=m;j>=1;j--){ while(top&&stk[top]>a[i][j])top--; x[i][j]=pos[top]-j; stk[++top]=a[i][j]; pos[top]=j; } } for(int i=1;i<=m;i++){ int top=0; pos[top]=0; for(int j=1;j<=n;j++){ while(top&&stk[top]=1;j--){ while(top&&stk[top]