a = [int(s) for s in input().split()]
b=[]
for i in range(a[0]):
b.append([])
b[i]= [int(s) for s in input().split()]
c = [int(s) for s in input().split()]
################################
def u(b):
tem=[]
for i in range(len(b)):
tem.append([])
tem[i]=b[(i+1)*-1]
return tem
################################
def n(b):
tem=[]
for i in range(len(b[0])):
tem.append([])
for j in range(len(b)):
tem[i].append(b[j][(i+1)*-1])
tem=u(tem)
return tem
################################
for i in range(len(c)):
if c[i]==1:
b=u(b)
if c[i]==0:
b=n(b)
for j in range(len(b)):
b[j].reverse()
################################
print(len(b),len(b[0]))
for i in range(len(b)):
for j in range(len(b[i])):
print(b[i][j],end=" ")
print()