R,C=map(int,input().split())
L=[]
for _ in range(R):
L.append([int(x) for x in input().split()])
Ln=[]
for i in range(R):
Ln.append(L[i].copy())
for i in range(R):
for j in range(C):
if L[i][j]==0:
sume=0
b=4
try:
if L[i-1][j]!=0 and i-1>=0:sume+=L[i-1][j]
else:b-=1
except:b-=1
try:
if L[i+1][j]!=0:sume+=L[i+1][j]
else:b-=1
except:b-=1
try:
if L[i][j-1]!=0 and j-1>=0:sume+=L[i][j-1]
else:b-=1
except:b-=1
try:
if L[i][j+1]!=0:sume+=L[i][j+1]
else:b-=1
except:b-=1
if b!=0:
Ln[i][j]=sume//b
for i in Ln:
print(*i)
R,C=map(int,input().split())
L=[]
for _ in range(R):
L.append([int(x) for x in input().split()])
Ln=[]
for i in range(R):
Ln.append(L[i].copy())
for i in range(R):
for j in range(C):
if L[i][j]==0:
sume=0
b=4
try:
if L[i-1][j]!=0 and i-1>=0:sume+=L[i-1][j]
else:b-=1
except:b-=1
try:
if L[i+1][j]!=0:sume+=L[i+1][j]
else:b-=1
except:b-=1
try:
if L[i][j-1]!=0 and j-1>=0:sume+=L[i][j-1]
else:b-=1
except:b-=1
try:
if L[i][j+1]!=0:sume+=L[i][j+1]
else:b-=1
except:b-=1
if b!=0:
Ln[i][j]=sume//b
for i in Ln:
print(*i)
R, C=map(int,input().split())
L=[]
for _ in range(R):
L.append([int(x) for x in input().split()])
Ln = [row.copy() for row in L]
directions = [(-1, 0), (1, 0), (0, -1), (0, 1)] # 上,下,左,右
for i in range(R):
for j in range(C):
if L[i][j]==0:
sume, count = 0, 0
for dx, dy in directions:
nx, ny = i + dx, j + dy
if 0 <= nx < R and 0 <= ny < C and L[nx][ny] != 0:
sume += L[nx][ny]
count += 1
if count > 0:
Ln[i][j] = sume // count
for row in Ln:
print(*row)