#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <vector>
#include <queue>
#include <map>
using namespace std;
typedef long long LL;
const LL N = 505;

LL n,bu[N],bv[N],u[N],v[N],s[N],t[N];
LL pos[N][N],ans[N][N];

void solve(LL x){
	LL rs = n,cs = n;
	for(LL i = 1;i <= n;i ++){
		u[i] = (bu[i] >> x) & 1;
		v[i] = (bv[i] >> x) & 1;
//		cout << u[i] << ',' << v[i] << ' ' << s[i] << ' ' << t[i] << endl;
	}
	//
	memset(pos,-1,sizeof(pos));
	for(LL i = 1;i <= n;i ++){
		if(s[i] == 0 && u[i] == 1){
			rs --;
			for(LL j = 1;j <= n;j ++) pos[i][j] = 1;
		}
		if(s[i] == 1 && u[i] == 0){
			rs --;
			for(LL j = 1;j <= n;j ++) pos[i][j] = 0;
		}
	}
	for(LL j = 1;j <= n;j ++){
		if(t[j] == 0 && v[j] == 1){
			cs --;
			for(LL i = 1;i <= n;i ++) pos[i][j] = 1;
		}
		if(t[j] == 1 && v[j] == 0){
			cs --;
			for(LL i = 1;i <= n;i ++) pos[i][j] = 0;
		}
	}
	//*
	if(rs > 1 && cs > 1){
		for(LL i = 1;i <= n;i ++){
			LL fc = (i & 1);
			for(LL j = 1;j <= n;j ++){
				if(pos[i][j] != -1) continue;
				pos[i][j] = fc;
				fc = !fc;
			}
		}
	}
	else if(rs == 1 && cs >= 1){
		LL pl;
		for(LL i = 1;i <= n;i ++){
			for(LL j = 1;j <= n;j ++){
				if(pos[i][j] == -1) pl = i;
			}
		}
		for(LL j = 1;j <= n;j ++){
			LL fc = 0,fl = 0;
			for(LL i = 1;i <= n;i ++){
				if(pos[i][j] != -1) continue;
				fl = 1;
				if(t[j] == 0) fc &= pos[i][j];
				else fc |= pos[i][j];
			}
			if(!fl) continue;
			for(LL i = 0;i < 2;i ++){
				if(t[j] == 0 && (fc | i) == v[j]) pos[pl][j] = i;
				if(t[j] == 1 && (fc & 1) == v[j]) pos[pl][j] = i;
			}
		}
	}
	else if(cs == 1 && rs >= 1){
		LL pl;
		for(LL i = 1;i <= n;i ++){
			for(LL j = 1;j <= n;j ++){
				if(pos[i][j] == -1) pl = j;
			}
		}
		for(LL i = 1;i <= n;i ++){
			LL fc = 0,fl = 0;
			for(LL j = 1;j <= n;j ++){
				if(pos[i][j] != -1) continue;
				fl = 1;
				if(s[i] == 0) fc &= pos[i][j];
				else fc |= pos[i][j];
			}
			if(!fl) continue;
			for(LL j = 0;j < 2;j ++){
				if(s[i] == 0 && (fc | i) == u[j]) pos[i][pl] = j;
				if(s[i] == 1 && (fc & 1) == u[j]) pos[i][pl] = j;
			}
		}
	}
	//*/
	for(LL i = 1;i <= n;i ++){
		for(LL j = 1;j <= n;j ++){
//			cout << pos[i][j] << (j == n ? '\n' : ' ');
			ans[i][j] += (pos[i][j] << x);
		}
	}
}

LL cu[N],cv[N];
LL chk(){
	for(LL i = 1;i <= n;i ++) cu[i] = ans[i][1];
	for(LL j = 1;j <= n;j ++) cv[j] = ans[1][j];
	for(LL i = 1;i <= n;i ++){
		for(LL j = 1;j <= n;j ++){
//			cout << i << ' ' << j << ' ' << s[i] << ' ' << t[j] << endl;
			if(s[i] == 0) cu[i] &= ans[i][j];
			else cu[i] |= ans[i][j];
			if(t[j] == 0) cv[j] &= ans[i][j];
			else cv[j] |= ans[i][j];
		}
	}
//	for(LL i = 1;i <= n;i ++) cout << cu[i] << ',' << cv[i] << endl;
	for(LL i = 1;i <= n;i ++) if(cu[i] != bu[i] || cv[i] != bv[i]) return 1;
	return 0;
}

int main(){
	ios::sync_with_stdio(false);
	cin >> n;
	for(LL i = 1;i <= n;i ++) cin >> s[i];
	for(LL i = 1;i <= n;i ++) cin >> t[i];
	for(LL i = 1;i <= n;i ++) cin >> bu[i];
	for(LL i = 1;i <= n;i ++) cin >> bv[i];
	for(LL i = 0;i <= 62;i ++) solve(i);
	if(chk()){ cout << "-1\n"; return 0; }
	for(LL i = 1;i <= n;i ++){
		for(LL j = 1;j <= n;j ++){
			cout << ans[i][j] << (j == n ? '\n' : ' ');
		}
	}
	return 0;
}