#include <stdio.h>
#include <stdlib.h>

int main(){
int T,N,M,zbir1=0,zbir2=0
;
printf("unesi broj test primera\n");
scanf("%d",&T);
for(int k=0;k<T;k++){
    scanf("%d %d",&N,&M);
int mat[N][M];
    for(int f=1;f<=N;f++){
        printf("unesi matricu\n");
        for(int u=1;u<=M;u++){
            scanf("%d",&mat[f][u]);
            }
        }
        for(int f=1;f<=N;f++){
                zbir1=0;
            for(int u=1;u<=M;u++){
            zbir1=zbir1+mat[f][u];
            }
        if(zbir1%N!=0 || zbir1==0){
            printf("-1");
            break;
        }
        else if(zbir1%N==0){
           for(int u=1;u<=M;u++){
                zbir2=0;
            for(int f=1;f<=N;f++){
            zbir2=zbir2+mat[f][u];
            }
            if(zbir2%M!=0 || zbir2==0){
                printf("-1");
                break;
            }




            else{
            for(int f=1;f<=N;f++){
                for(int u=1;u<=M;u++){
                    printf("%d",mat[f][u]);



                }

            }
        }
    }


}
}



}
return 0;
}