### BUGGY CODE ###

#####BitCount#####
def count_bit(n):
  count = 0
  while n:
    n &= n -1
    count += 1
  return count

N = int(input())

S = list(map(int,input().split()))
T = list(map(int,input().split()))
U = list(map(int,input().split()))
V = list(map(int,input().split()))

mask_gyo = [((1<<N)-1)<<(N*i) for i in range(N)]
mask_retu = [(((1<<(N**2))-1)//((1<<N)-1))<<i for i in range(N)]

B = []

for n in range(64):
  u = sum(((U[i]>>n)&1)<<i for i in range(N))
  v = sum(((V[i]>>n)&1)<<i for i in range(N))
  b = 0
  check = 0
  ###行のAND=1とOR=0を埋める###
  for i in range(N):
    if S[i] !=((u>>i) & 1):
      if((u>>i) & 1) == 1:
        b |= mask_gyo[i]
      check |= mask_gyo[i]
  ###列のAND=1とOR=0を埋める###
  for i in range(N):
    if T[i] !=((v>>i) & 1):
      if((v>>i) & 1) == 1:
        if b & mask_retu[i] != check & mask_retu[i]:
          print(-1)
          exit()
        b |= mask_retu[i]
      if((v>>i) & 1) == 0:
        if (b & mask_retu[i]) ^ (check & mask_retu[i]) != (check & mask_retu[i]):
          print(-1)
          exit()
      check |= mask_retu[i]


  ###行と列の値が一致するところを埋める###  
  
  same = (1<<N**2)-1-sum( (v^((1<<N)-1)*((u>>i)&1))<<N*i for i in range(N))
  same_b = same & (v * mask_retu[0])
  b |= same_b
  check |= same

  ###行のOR=1を満たしていく###  
  for i in range(N):
    if S[i] ==((u>>i) & 1) == 1:
      if b & mask_gyo[i] == 0: #まだ満たされてない行を発見した場合
        flag = 0 #未解決
        for j in range(N):
          digit = (1<<j)<<(N*i)
          if T[j] ==((v>>j) & 1) == 0 and check & digit == 0 and count_bit(b & mask_retu[j])<N-1:
            b |= digit
            check |= digit
            flag = 1 #解決
            break
        if not flag:
          print(-1)
          exit()
  ###列のOR=1を満たしていく###  
  for i in range(N):
    if T[i] ==((v>>i) & 1) == 1:
      if b & mask_retu[i] == 0: #まだ満たされてない列を発見した場合
        flag = 0 #未解決
        for j in range(N):
          digit = (1<<i)<<(N*j)
          if S[j] ==((u>>j) & 1)== 0 and check & digit == 0 and count_bit(b & mask_gyo[j])<N-1:
            b |= digit
            check |= digit
            flag = 1 #解決
            break         
        if not flag:
          print(-1)
          exit()

  for i in range(N):
    if S[i] == 0 and((u>>i) & 1) == 0:
      if b & mask_gyo[i] == mask_gyo[i]:
        print(-1)
        exit()
    if S[i] == 0 and((u>>i) & 1) == 1:
      if b & mask_gyo[i] != mask_gyo[i]:
        print(-1)
        exit()
    if S[i] == 1 and((u>>i) & 1) == 0:
      if b & mask_gyo[i] != 0:
        print(-1)
        exit()
    if S[i] == 1 and((u>>i) & 1) == 1:
      if b & mask_gyo[i] == 0:
        print(-1)
        exit()
  
  for i in range(N):
    if T[i] == 0 and((v>>i) & 1) == 0:
      if b & mask_retu[i] == mask_retu[i]:
        print(-1)
        exit()
    if T[i] == 0 and((v>>i) & 1) == 1:
      if b & mask_retu[i] != mask_retu[i]:
        print(-1)
        exit()
    if T[i] == 1 and((v>>i) & 1) == 0:
      if b & mask_retu[i] != 0:
        print(-1)
        exit()
    if T[i] == 1 and((v>>i) & 1) == 1:
      if b & mask_retu[i] == 0:
        print(-1)
        exit()
  
  B.append(b)
"""
  for j in range(N):
    for i in range(N):
      digit = (1<<i)<<(N*j)
      a = not (b & digit == 0)
      res[j][i] += a<<n
"""

"""
for item in res:
  print(*item)
"""
for j in range(N):
  for i in range(N):
    ans = 0
    digit = (1<<i)<<(N*j)
    for n in range(64):
      b = B[n]
      a = not (b & digit == 0)
      ans += a<<n
    print(ans,end=" ")
  print()
   


### FIXED CODE ###

import numpy as np

N = int(input())

S = list(map(int,input().split()))
T = list(map(int,input().split()))
U = np.array(list(map(int,input().split())),dtype = np.uint64)
V = np.array(list(map(int,input().split())),dtype = np.uint64)

ans = np.zeros((N,N),dtype = np.uint64)

for n in range(64):
  u = (U>>n)&1
  v = (V>>n)&1
  su = [int(S[i]*2+u[i]) for i in range(N)]
  tv = [int(T[i]*2+v[i]) for i in range(N)]
  suzerozero = []
  suoneone = []
  tvzerozero = []
  tvoneone = []
  for i in range(N):
    if su[i] == 0:
      suzerozero.append(i)
    if su[i] == 3:
      suoneone.append(i)
  for i in range(N):
    if tv[i] == 0:
      tvzerozero.append(i)
    if tv[i] == 3:
      tvoneone.append(i)
  
  b = np.zeros((N,N),dtype = np.uint64)
  ###行のAND=1とOR=0を埋める###
  for i in range(N):
    if su[i] == 1:
      b[i] = np.ones(N,int)
  ###列のAND=1とOR=0を埋める###
  for i in range(N):
    if tv[i] == 1:
      b[:,i] = np.ones(N,int)
  ###行と列の値が一致するところを埋める###  
  b |= np.dot(np.array([u]).T,np.ones((1,N),dtype = np.uint64)) & np.dot(np.array([v]).T,np.ones((1,N),dtype = np.uint64)).T
  ###行のOR=1を満たしていく### 
  if 1 not in tv and 3 not in tv:
    m = len(tvzerozero)
    now = 0
    for i in suoneone:
      b[i][tvzerozero[now%m]] += 1
      now += 1
  ###列のOR=1を満たしていく### 
  if 1 not in su and 3 not in su:
    m = len(suzerozero)
    now = 0
    for i in tvoneone:
      b[suzerozero[now%m]][i] += 1
      now += 1
  ans += b<<n

def end():
  print(-1)
  exit()
for i in range(N):
  if S[i] == 0:
    now = ans[i][0]
    for j in range(1,N):
      now &= ans[i][j]
  if S[i] == 1:
    now = ans[i][0]
    for j in range(1,N):
      now |= ans[i][j]
  if now != U[i]:
    end()
for i in range(N):
  if T[i] == 0:
    now = ans[0][i]
    for j in range(1,N):
      now &= ans[j][i]
  if T[i] == 1:
    now = ans[0][i]
    for j in range(1,N):
      now |= ans[j][i]
  if now != V[i]:
    end()

for item in ans:
  print(*list(item))