#include #include using namespace std; typedef long long ll; typedef long double ld; // read template long long readInt(long long l,long long r,char endd){ long long x=0; int cnt=0; int fi=-1; bool is_neg=false; while(true){ char g=getchar(); if(g=='-'){ assert(fi==-1); is_neg=true; continue; } if('0'<=g && g<='9'){ x*=10; x+=g-'0'; if(cnt==0){ fi=g-'0'; } cnt++; assert(fi!=0 || cnt==1); assert(fi!=0 || is_neg==false); assert(!(cnt>19 || ( cnt==19 && fi>1) )); } else if(g==endd){ assert(cnt>0); if(is_neg){ x= -x; } assert(l<=x && x<=r); return x; } else { assert(false); } } } string readString(int l,int r,char endd){ string ret=""; int cnt=0; while(true){ char g=getchar(); assert(g!=-1); if(g==endd){ break; } cnt++; ret+=g; } assert(l<=cnt && cnt<=r); return ret; } long long readIntSp(long long l,long long r){ return readInt(l,r,' '); } long long readIntLn(long long l,long long r){ return readInt(l,r,'\n'); } string readStringLn(int l,int r){ return readString(l,r,'\n'); } string readStringSp(int l,int r){ return readString(l,r,' '); } // end const int M = 1e3 + 239; ll sum; bool a[M], b[M]; int n, m; string s[M]; void solve() { n = readIntSp(1, 1000); m = readIntLn(1, 1000); sum += (ll)n * (ll)m; for (int i = 0; i < n; i++) s[i] = readStringLn(m, m); bool ch = true; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) ch &= (s[i][j] == '0' || s[i][j] == '1'); assert(ch); for (int i = 0; i < n; i++) { a[i] = 0; for (int j = 0; j < m; j++) a[i] |= (s[i][j] - '0'); //is any 1 in i-th row } for (int j = 0; j < m; j++) { b[j] = 0; for (int i = 0; i < n; i++) b[j] |= (s[i][j] - '0'); //is any 1 in j-th column } ch = false; for (int i = 0; i < n; i++) ch |= a[i]; for (int i = 0; i < n; i++, cout << "\n") for (int j = 0; j < m; j++, cout << " ") { if (!ch) cout << -1; // ch = 0 ==> all s[i][j] = 0 ==> all answers = -1 else if (s[i][j] == '1') cout << 0; // answer is 0, because s[i][j] = 1 else if (a[i] || b[j]) cout << 1; // it can be done in 1 operation else cout << 2; // it can be done in 2 operations } } int main() { int T = readIntLn(1, 100); sum = 0; while (T--) solve(); assert(getchar() == -1); assert(sum <= (ll)(1e6)); return 0; }