using System;
using System.Collections.Generic;
using System.Linq;
using CompLib.Util;

public class Program
{
    int N;
    int[] S;
    int[] T;
    ulong[] U;
    ulong[] V;
    public void Solve()
    {
        var sc = new Scanner();
        N = sc.NextInt();
        S = sc.IntArray();
        T = sc.IntArray();
        U = sc.Array().Select(s => ulong.Parse(s)).ToArray();
        V = sc.Array().Select(s => ulong.Parse(s)).ToArray();
        ulong[][] ans = new ulong[N][];
        for (int i = 0; i < N; i++)
        {
            ans[i] = new ulong[N];
        }
        for (int t = 0; t < 64; t++)
        {
            ulong bit = 1UL << t;
            bool[] flagU = new bool[N];
            bool[] flagV = new bool[N];
            // 0かつbitが立ってるなら全部1
            for (int i = 0; i < N; i++)
            {
                bool uu = (U[i] & bit) == bit;
                bool vv = (V[i] & bit) == bit;
                if (S[i] == 0 && uu)
                {
                    flagU[i] = true;
                    for (int j = 0; j < N; j++)
                    {
                        ans[i][j] |= bit;
                        flagV[j] = true;
                    }
                }
                if (T[i] == 0 && vv)
                {
                    flagV[i] = true;
                    for (int j = 0; j < N; j++)
                    {
                        ans[j][i] |= bit;
                        flagU[j] = true;
                    }
                }
            }
            // U,V両方立ってる　立てる
            for (int i = 0; i < N; i++)
            {
                bool uu = (U[i] & bit) == bit;
                for (int j = 0; j < N; j++)
                {

                    bool vv = (V[j] & bit) == bit;
                    if (uu && vv)
                    {
                        ans[i][j] |= bit;
                        flagU[i] = true;
                        flagV[j] = true;
                    }
                }
            }
            // 1かつbitが立ってないなら全部0 矛盾するなら -1
            for (int i = 0; i < N; i++)
            {
                bool uu = (U[i] & bit) == bit;
                bool vv = (V[i] & bit) == bit;

                if (S[i] == 1 && !uu && flagU[i])
                {
                    Console.WriteLine("-1");
                    return;
                }
                if (T[i] == 1 && !vv && flagV[i])
                {
                    Console.WriteLine("-1");
                    return;
                }
            }
            // のこり
            // 1かつbitが立ってる まだ立ててない

            // 行
            // 列 どの列も 立ってる列がない
            // 1かつ立ってる行がある 0かつ立ってないがない or
            // 全部1かつ立ってる 列 0かつ立ってないが1以下 
            // -1
            List<int> row = new List<int>();
            List<int> col = new List<int>();
            bool andR = true;
            bool andC = true;
            bool orR = false;
            bool orC = false;
            for (int i = 0; i < N; i++)
            {
                bool uu = (U[i] & bit) == bit;
                bool vv = (V[i] & bit) == bit;
                if (S[i] == 0 && !uu) row.Add(i);
                andR &= S[i] == 1 && uu && !flagU[i];
                orR |= S[i] == 1 && uu && !flagU[i];

                if (T[i] == 0 && !vv) col.Add(i);
                andC &= T[i] == 1 && vv && !flagV[i];
                orC |= T[i] == 1 && vv && !flagV[i];
            }

            if ((orR && col.Count == 0) || (orC && row.Count == 0) || (andR && col.Count <= 1) || (andC && row.Count <= 1))
            {
                Console.WriteLine("-1");
                return;
            }

            if (orR)
            {
                int cnt = 0;
                for (int i = 0; i < N; i++)
                {
                    bool uu = (U[i] & bit) == bit;
                    if (S[i] == 1 && uu && !flagU[i])
                    {
                        ans[i][col[(cnt++) % col.Count]] |= bit;
                    }
                }
            }
            else if (orC)
            {
                int cnt = 0;
                for (int i = 0; i < N; i++)
                {
                    bool vv = (V[i] & bit) == bit;
                    if (T[i] == 1 && vv && !flagV[i])
                    {
                        ans[row[(cnt++) % row.Count]][i] |= bit;
                    }
                }
            }
        }

        for (int i = 0; i < N; i++)
        {
            if (S[i] == 0)
            {
                ulong u = ulong.MaxValue;
                for (int j = 0; j < N; j++)
                {
                    u &= ans[i][j];
                }
                if (U[i] != u)
                {
                    Console.WriteLine("-1");
                    return;
                }
            }
            else
            {
                ulong u = 0;
                for (int j = 0; j < N; j++)
                {
                    u |= ans[i][j];
                }
                if (U[i] != u)
                {
                    Console.WriteLine("-1");
                    return;
                }
            }
            if (T[i] == 0)
            {
                ulong v = ulong.MaxValue;
                for (int j = 0; j < N; j++)
                {
                    v &= ans[j][i];
                }
                if (V[i] != v)
                {
                    Console.WriteLine("-1");
                    return;
                }
            }
            else
            {
                ulong v = 0;
                for (int j = 0; j < N; j++)
                {
                    v |= ans[j][i];
                }
                if (V[i] != v)
                {
                    Console.WriteLine("-1");
                    return;
                }
            }
        }


        Console.WriteLine(string.Join("\n", ans.Select(ln => string.Join(" ", ln))));
    }

    public static void Main(string[] args) => new Program().Solve();
}

namespace CompLib.Util
{
    using System;
    using System.Linq;

    class Scanner
    {
        private string[] _line;
        private int _index;
        private const char Separator = ' ';

        public Scanner()
        {
            _line = new string[0];
            _index = 0;
        }

        public string Next()
        {
            while (_index >= _line.Length)
            {
                _line = Console.ReadLine().Split(Separator);
                _index = 0;
            }

            return _line[_index++];
        }

        public int NextInt() => int.Parse(Next());
        public long NextLong() => long.Parse(Next());
        public double NextDouble() => double.Parse(Next());
        public decimal NextDecimal() => decimal.Parse(Next());
        public char NextChar() => Next()[0];
        public char[] NextCharArray() => Next().ToCharArray();

        public string[] Array()
        {
            _line = Console.ReadLine().Split(Separator);
            _index = _line.Length;
            return _line;
        }

        public int[] IntArray() => Array().Select(int.Parse).ToArray();
        public long[] LongArray() => Array().Select(long.Parse).ToArray();
        public double[] DoubleArray() => Array().Select(double.Parse).ToArray();
        public decimal[] DecimalArray() => Array().Select(decimal.Parse).ToArray();
    }
}


Measured memory_bytes: 60980