#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;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKYm9vbCBteV9vcGVyYXRpb24gKGJvb2wgYSwgYm9vbCBiLCBib29sKiByZXMpIHsKCWlmICghYSBhbmQgIWIpIHJldHVybiByZXNbMF07CgllbHNlIGlmICghYSBhbmQgYikgcmV0dXJuIHJlc1sxXTsKCWVsc2UgaWYgKGEgYW5kICFiKSByZXR1cm4gcmVzWzJdOwoJZWxzZSByZXR1cm4gcmVzWzNdOwp9CgppbnQgbWFpbigpIHsKCWludCB3LCBoOwoJYm9vbCB0cnV0aF90WzRdOwoJY2luID4+IHcgPj4gaDsKCWJvb2wgKipmaXJzdF90ID0gbmV3IGJvb2wgKltoXTsKCWJvb2wgKipzZWNvbmRfdCA9IG5ldyBib29sICpbaF07Cglmb3IgKGludCBpID0gMDsgaSA8IGg7IGkrKykgewoJCWZpcnN0X3RbaV0gPSBuZXcgYm9vbCBbd107CgkJc2Vjb25kX3RbaV0gPSBuZXcgYm9vbCBbd107Cgl9CgljaGFyIGM7Cglmb3Ig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