/**
 * code generated by JHelper
 * More info: https://github.com/AlexeyDmitriev/JHelper
 * @author aajisaka
 */

#include<bits/stdc++.h>

using namespace std;

void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}
#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif

#define SPEED ios_base::sync_with_stdio(false);cin.tie(nullptr)
#define rep(i,n) for(int i=0; i<(int)(n); i++)
#define all(v) v.begin(), v.end()
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }

using ll = long long;
using P = pair<ll, ll>;

constexpr long double PI = 3.14159265358979323846264338327950288L;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());

#define ull unsigned long long

class FIHateMatrixConstruction {
public:
    void solve(istream& cin, ostream& cout) {
      SPEED;
      int n; cin >> n;
      vector<int> s(n), t(n);
      vector<ull> u(n), v(n);

      rep(i, n) cin >> s[i];
      rep(i, n) cin >> t[i];
      rep(i, n) cin >> u[i];
      rep(i, n) cin >> v[i];

      vector<vector<ull>> ans(n, vector<ull>(n));

      if (n==1) {
        if (u[0] != v[0]) {
          cout << -1 << endl;
          return;
        } else {
          cout << u[0] << endl;
          return;
        }
      }

      // split by bit
      ull b = 1;
      rep(k, 64) {
        vector<vector<int>> a(n, vector<int>(n, -1));
        rep(i, n) {
          if (s[i] == 0 && (u[i]&b) != 0) {
            rep(j, n) a[i][j] = 1;
          }
          if (s[i] == 1 && (u[i]&b) == 0) {
            rep(j, n) a[i][j] = 0;
          }
        }
        rep(j, n) {
          if (t[j] == 0 && (v[j]&b) != 0) {
            rep(i, n) {
              if (a[i][j] == 0) {
                cout << -1 << endl;
                return;
              } else {
                a[i][j] = 1;
              }
            }
          }
          if (t[j] == 1 && (v[j]&b) == 0) {
            rep(i, n) {
              if (a[i][j] == 1) {
                cout << -1 << endl;
                return;
              } else {
                a[i][j] = 0;
              }
            }
          }
        }
        rep(i, n) {
          if (s[i] == 0 && (u[i]&b) != 0) {
            rep(j, n) {
              if (a[i][j] == 0) {
                cout << -1 << endl;
                return;
              } else {
                a[i][j] = 1;
              }
            }
          }
          if (s[i] == 1 && (u[i]&b) == 0) {
            rep(j, n) {
              if (a[i][j] == 1) {
                cout << -1 << endl;
                return;
              } else {
                a[i][j] = 0;
              }
            }
          }
        }




        rep(i, n) {
          rep(j, n) {
            chmax(a[i][j], 0);
          }
        }

        rep(i, n) {
          if (s[i] == 1 && (u[i] & b) != 0) {
            bool flag = false;
            rep(j, n) {
              if (t[j] == 1 && (v[j] & b) != 0) {
                a[i][j] = 1;
              }
              if (a[i][j] == 1) flag = true;
            }
            if (flag) continue;
            rep(j, n) {
              if (t[j] == 0 && (v[j] & b) == 0) {
                int cnt = 0;
                rep(l, n) {
                  if (a[l][j] == 0) cnt++;
                }
                if (cnt >= 2) {
                  a[i][j] = 1;
                  flag = true;
                  break;
                }
              }
            }
            if (!flag) {
              cout << -1 << endl;
              return;
            }
          }
        }

        rep(j, n) {
          if (t[j]==1 && (v[j]&b) != 0) {
            bool flag = false;
            rep(i, n) {
              if (a[i][j] == 1) flag = true;
            }
            if (flag) continue;
            rep(i, n) {
              if (s[i] == 0 && (u[i] & b) == 0) {
                int cnt = 0;
                rep(l, n) {
                  if (a[i][l] == 0) cnt++;
                }
                if (cnt >= 2) {
                  a[i][j] = 1;
                  flag = true;
                  break;
                }
              }
            }
            if (!flag) {
              cout << -1 << endl;
              return;
            }
          }
        }
        rep(i, n) {
          rep(j, n) {
            if (k<5) debug(k, i, j, a[i][j]);
            ans[i][j] += b*a[i][j];
          }
        }
        b *= 2;
      }





      rep(i, n) {
        rep(j, n) {
          cout << ans[i][j] << ' ';
        }
        cout << '\n';
      }


    }
};

signed main() {
  FIHateMatrixConstruction solver;
  std::istream& in(std::cin);
  std::ostream& out(std::cout);
  solver.solve(in, out);
  return 0;
}

Measured memory_bytes: 7644