Why does this return WA? After trying some of my own test cases, i tried checking other solutions, and i couldn’t come up a case that fails. Also, if there’s any optimization possible, please do suggest.

Problem link: https://www.codechef.com/COOK96B/problems/ORMATRIX

My attempt:

```
def findans(m, n, grid):
same=0
for i in range(m):
if grid[i].count('0')==len(grid):
same+=1
if same==m:
grid=[['-1' for k in range(n)] for j in range(m)]
return grid
else:
row=[0 for i in range(m)]
col=[0 for k in range(n)]
for i in range(m):
for j in range(n):
if grid[i][j]=='1':
row[i]=1
col[j]=1
for i in range(m):
for j in range(n):
if grid[i][j]=='0':
if row[i]==1 or col[j]==1:
grid[i][j]='1'
else:
grid[i][j]='2'
else:
grid[i][j]='0'
return grid
tcases=int(raw_input())
for i in range(tcases):
m, n = map(int, raw_input().split(' '))
grid=[[0 for k in range(n)] for j in range(m)]
for i in range(m):
grid[i]=list(raw_input())
grid=findans(m, n, grid)
for i in range(m):
print " ".join(l for l in grid[i])
```