time=0
while 1:
try:
time+=1
n=int(input())
x,y=map(int,input().split())
tree=[]
tree.append([0 for i in range(y+2)])
for i in range(x):
m='0 '+input()+' 0'
m=list(map(int,m.split()))
tree.append(m)
tree.append([0 for i in range(y+2)])
have=[1,tree[1].index(1)]
xx=[0,0,1,-1]
yy=[1,-1,0,0]
tree[1][tree[1].index(1)]='1'
while have!=[]:
if n==1:
newx=have.pop(0)
newy=have.pop(0)
for i in range(4):
if tree[newx+xx[i]][newy+yy[i]]==1:
tree[newx+xx[i]][newy+yy[i]]=int(tree[newx][newy])+1
have.append(newx+xx[i])
have.append(newy+yy[i])
elif n==2:
newx=have.pop(0)
newy=have.pop(0)
for i in range(3):
if tree[newx+xx[i]][newy+yy[i]]==1:
tree[newx+xx[i]][newy+yy[i]]=int(tree[newx][newy])+1
have.append(newx+xx[i])
have.append(newy+yy[i])
for i in range(1,x+1):
for j in range(1,y+1):
if tree[i][j]==1:
tree[i][j]=0
print('Case %d:'%(time))
for k in range(1,len(tree)-1):
print(*tree[k][1:-1],sep=' ')
except:
break