#include <iostream>
using namespace std;

bool my_operation (bool a, bool b, bool* res) {
	if (!a and !b) return res[0];
	else if (!a and b) return res[1];
	else if (a and !b) return res[2];
	else return res[3];
}

int main() {
	int w, h;
	bool truth_t[4];
	cin >> w >> h;
	bool **first_t = new bool *[h];
	bool **second_t = new bool *[h];
	for (int i = 0; i < h; i++) {
		first_t[i] = new bool [w];
		second_t[i] = new bool [w];
	}
	char c;
	for (int i = 0; i < h; i++){
		for (int j = 0; j < w; j++) {
			cin >> c;
			first_t[i][j] = (c == '1' ? true : false);
		}
	}
    for (int i = 0; i < h; i++){ 
		for (int j = 0; j < w; j++) {
			cin >> c;
			second_t[i][j] = (c == '1' ? true : false);
		}
    }
    for (int i = 0; i < 4; i++) {
    	cin >> c;
    	truth_t[i] = (c == '1' ? true : false);
    }
    for (int i = 0; i < h; i++) {
    	for (int j = 0; j < w; j++){
    		cout << my_operation (first_t[i][j], second_t[i][j], truth_t);
    	}
    	if (i < h - 1) cout << endl;
    }
	return 0;
}