o=input()
lis=[]
for i in range(int(o[0])):
c=input()
c=c.split(" ")
lis.append(c)
y=input()
y=y.split(" ")
y=y[::-1]
for z in y:
output=[]
if z=="0":
for i in range(len(lis[0])):
output.append([])
for ele in lis:
for i in range(len(ele)-1,-1,-1):
output[len(ele)-i-1].append(ele[i])
o=o[2::-1]
lis=output
else:
lis=lis[::-1]
print(o)
for i in lis:
out=''
for k in i:
out=out+k+' '
print(out)
######################help##############################