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 = [(sum(1<<(N*j) for j in range(N)))<<i for i in range(N)]

res = [[0 for i in range(N)] for j in range(N)]

for n in range(64):
  u = [(i>>n)&1 for i in U]
  v = [(i>>n)&1 for i in V]
  b = 0
  check = 0
  ###行のAND=1とOR=0を埋める###
  for i in range(N):
    if S[i] != u[i]:
      if u[i] == 1:
        b |= mask_gyo[i]
      check |= mask_gyo[i]
  ###列のAND=1とOR=0を埋める###
  for i in range(N):
    if T[i] != v[i]:
      if v[i] == 1:
        if b & mask_retu[i] != check & mask_retu[i]:
          print(-1)
          exit()
        b |= mask_retu[i]
      if v[i] == 0:
        if (b & mask_retu[i]) ^ (check & mask_retu[i]) != (check & mask_retu[i]):
          print(-1)
          exit()
      check |= mask_retu[i]
  ###行と列の値が一致するところを埋める###  
  for j in range(N):
    for i in range(N):
      if u[j] == v[i]:
        digit = (1<<i)<<(N*j)
        b |= digit*u[i]
        check |= digit
  ###行のOR=1を満たしていく###  
  for i in range(N):
    if S[i] == u[i] == 1:
      if b & mask_gyo[i] == 0: #まだ満たされてない行を発見した場合
        flag = 0 #未解決
        for j in range(N):
          digit = (1<<i)<<(N*j)
          if T[j]==1 and v[j]== 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:
      if b & mask_retu[i] == 0: #まだ満たされてない列を発見した場合
        flag = 0 #未解決
        for j in range(N):
          digit = (1<<j)<<(N*i)
          if S[j]==1 and u[j]== 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 j in range(N):
    for i in range(N):
      digit = (1<<j)<<(N*i)
      a = not (b & digit == 0)
      res[i][j] += a<<n

  
for item in res:
  print(*item)

### 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))