#include #include #include #include using namespace std; #define UI unsigned int UI l[1002][1002],r[1002][1002],u[1002][1002],d[1002][1002]; UI sum1[1002],sum2[1002],e[1002][1002],sta[1002]; int main() { // freopen("in.txt","r",stdin);freopen("out.txt","w",stdout); int T,i,j,n,m,top; for (i=1;i<=1000;i++) {sum2[i]=sum2[i-1]+i*i;sum1[i]=sum1[i-1]+i;} scanf("%d",&T); while (T--) { scanf("%d%d",&n,&m); for (i=1;i<=n;i++) for (j=1;j<=m;j++) scanf("%u",&e[i][j]); for (i=1;i<=n;i++) { top=0; for (j=1;j<=m;j++) { while (top&&e[i][sta[top]]>e[i][j]) {r[i][sta[top]]=j-1;top--;} l[i][j]=sta[top]+1; while (top&&e[i][sta[top]]==e[i][j]) {r[i][sta[top]]=j-1;top--;} sta[++top]=j; } for (j=1;j<=top;j++) r[i][sta[j]]=m; } for (j=1;j<=m;j++) { top=0; for (i=1;i<=n;i++) { while (top&&e[sta[top]][j]