#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, n;
bool truth_t[4];
cin >> w >> h;
n = w * h;
bool *first_t = new bool [n];
bool *second_t = new bool [n];
char c;
for (int i = 0; i < n; i++) {
cin >> c;
first_t[i] = (c == '1' ? true : false);
}
for (int i = 0; i < n; i++) {
cin >> c;
second_t[i] = (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 < n; i++) {
if (i != 0 && i % w == 0)
cout << endl;
cout << my_operation (first_t[i], second_t[i], truth_t);
}
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKYm9vbCBteV9vcGVyYXRpb24gKGJvb2wgYSwgYm9vbCBiLCBib29sKiByZXMpIHsKCWlmICghYSBhbmQgIWIpIHJldHVybiByZXNbMF07CgllbHNlIGlmICghYSBhbmQgYikgcmV0dXJuIHJlc1sxXTsKCWVsc2UgaWYgKGEgYW5kICFiKSByZXR1cm4gcmVzWzJdOwoJZWxzZSByZXR1cm4gcmVzWzNdOwp9CgppbnQgbWFpbigpIHsKCWludCB3LCBoLCBuOwoJYm9vbCB0cnV0aF90WzRdOwoJY2luID4+IHcgPj4gaDsKCW4gPSB3ICogaDsKCWJvb2wgKmZpcnN0X3QgPSBuZXcgYm9vbCBbbl07Cglib29sICpzZWNvbmRfdCA9IG5ldyBib29sIFtuXTsKCWNoYXIgYzsKCWZvciAoaW50IGkgPSAwOyBpIDwgbjsgaSsrKSB7CiAgICAJY2luID4+IGM7CiAgICAJZmlyc3RfdFtpXSA9IChjID09ICcxJyA/IHRydWUgOiBmYWxzZSk7Cgl9Cglmb3IgKGludCBpID0gMDsgaSA8IG47IGkrKykgewogICAgCWNpbiA+PiBjOwogICAgCXNlY29uZF90W2ldID0gKGMgPT0gJzEnID8gdHJ1ZSA6IGZhbHNlKTsKCX0KCWZvciAoaW50IGkgPSAwOyBpIDwgNDsgaSsrKSB7CiAgICAJY2luID4+IGM7CiAgICAJdHJ1dGhfdFtpXSA9IChjID09ICcxJyA/IHRydWUgOiBmYWxzZSk7Cgl9Cglmb3IgKGludCBpID0gMDsgaSA8IG47IGkrKykgewogICAgCWlmIChpICE9IDAgJiYgaSAlIHcgPT0gMCkKICAgIAljb3V0IDw8IGVuZGw7CiAgICAJY291dCA8PCBteV9vcGVyYXRpb24gKGZpcnN0X3RbaV0sIHNlY29uZF90W2ldLCB0cnV0aF90KTsKCX0KCXJldHVybiAwOwp9