direction = [[1,0],[0,1],[-1,0],[0,-1]]
def dfs(graph,x,y):
graph[x][y] = 2
for i ,j in direction:
curx = x+i
cury = y+j
if 0<=curx<len(graph) and 0<=cury<len(graph[0]):
if graph[curx][cury] == 1:
dfs(graph,curx,cury)
n ,m = map(int,input().split())
graph = []
for i in range(n):
graph.append(list(map(int,input().split())))
for i in range(n):
if graph[i][0] == 1:
dfs(graph,i,0)
if graph[i][m-1] == 1:
dfs(graph,i,m-1)
for i in range(m):
if graph[0][i] == 1:
dfs(graph,0,i)
if graph[n-1][i] == 1:
dfs(graph,n-1,i)
for i in range(n):
for j in range(m):
if graph[i][j] == 2:
graph[i][j] = 1
elif graph[i][j] == 1:
graph[i][j] = 0
print(graph[i][j],end = " ")
print()