#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <vector>

using namespace std;

int a[812][812], b[812][812];
int c[810][810];

int S[810][810];

int row[810][810];
int col[810][810];

int get(vector<int> Q)
{
	int res = 0;
	for (int i = 0; i < 6; i++) {
		res = res * 3 + Q[i];
	}
	return res;
}

int main()
{
	ios::sync_with_stdio(false);
	// 3 ^ 6
	for (int i = 0; i < 729; i++) {
		for (int j = 0; j < 729; j++) {
			int ans = 0;
			int x = i, y = j;
			for (int k = 0; k < 6; k++) {
				ans += (x % 3) * (y % 3);
				x /= 3;
				y /= 3;
			}
			S[i][j] = ans % 3;
		}
	}

	int n;
	while (cin >> n) {
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < n; j++) {
				cin >> a[i][j];
				a[i][j] %= 3;
			}
		}
		
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < n; j++) {
				cin >> b[i][j];
				b[i][j] %= 3;
			}
		}	
		memset(row, 0, sizeof(row));
		memset(col, 0, sizeof(col));
		vector<int> Q;
		for (int i = 0; i < n; i++) {
			int c0 = 0;
			for (int j = 0; j < n; j++) {
				if (Q.size() == 6){
					row[i][c0++] = get(Q);
					Q.clear();
				} 
				Q.push_back(a[i][j]);
			}
			while (Q.size() < 6) Q.push_back(0);
			row[i][c0++] = get(Q);
			Q.clear();
		}

		for (int j = 0; j < n; j++) {
			int c1 = 0;
			for (int i = 0; i < n; i++) {
				if (Q.size() == 6) {
					col[c1++][j] = get(Q);
					Q.clear();
				}
				Q.push_back(b[i][j]);
			}
			while (Q.size() < 6) Q.push_back(0);
			col[c1++][j] = get(Q);
			Q.clear();
		}
		/*
		cout << row[0][0] << endl;
		cout << row[1][0] << endl;
		cout << col[0][0] << endl;
		cout << col[0][1] << endl;
		*/
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < n; j++) {
				c[i][j] = 0;
				for (int k = 0; k <= (n - 1) / 6; k++) {
					c[i][j] += S[row[i][k]][col[k][j]];
				}
				c[i][j] %= 3;
				if (j > 0) printf(" ");
				printf("%d", c[i][j]);
			}
			puts("");
		}
	}
	return 0;
}
