#include<iostream>
#include<vector>
#include<ctime>
//#include"BombsGame.h"
using namespace std;
vector<vector<int>> x(50, vector<int>(50, 0));
int Div[51];
int scnt=0,KK=0;
inline int solve() {
int q = 1, p = 0;
for (int i = 0; i < 50; i++) {
for (int j = 0; j < 50; j++) {
if (x[i][j] == 1)p += q;
q *= 2; q %= KK; p %= KK;
}
}
return p;
}
inline int Query(int i1, int i2, int i3, int i4) {
cout << "? " << i1 << ' ' << i2 << ' ' << i3 << ' ' << i4 << endl;
int Va; cin >> Va;
return Va;
}
inline void solve3(int row, int col, int p1) {
int i = row, j = col;
if(p1!=1)return;
if (p1 == 0) { x[i][j] = 0; x[i][j + 1] = 0; x[i + 1][j] = 0; x[i + 1][j + 1] = 0; return; }
else if (p1 == 4) { x[i][j] = 1; x[i][j + 1] = 1; x[i + 1][j] = 1; x[i + 1][j + 1] = 1; return; }
if (x[i][j] != -1) {
if (x[i][j] == 1) { scnt++; x[i][j + 1] = 0; x[i + 1][j] = 0; x[i + 1][j + 1] = 0; return; }
int p2 = Query(i, j, i, j + 1), p3 = p1 - p2;
if (p2 == 0) { x[i][j] = 0; x[i][j + 1] = 0; }
if (p3 == 0) { x[i + 1][j] = 0; x[i + 1][j + 1] = 0; x[i][j + 1] = 1; return; }
if (p3 == 1) {
if (j == 48) { x[i + 1][j] = Query(i + 1, j, i + 1, j); x[i + 1][j + 1] = 1 - x[i + 1][j]; }
else {
int v1 = Query(i + 1, j + 1, i + 1, j + 2);
if (v1 == 0) { x[i + 1][j] = 1; x[i + 1][j + 1] = 0; x[i + 1][j + 2] = 0; }
if (v1 == 2) { x[i + 1][j] = 0; x[i + 1][j + 1] = 1; x[i + 1][j + 2] = 1; }
if (v1 == 1) { x[i + 1][j + 1] = -2; }
}
}
}
if (x[i + 1][j] != -1) {
if (x[i + 1][j] == 1) { scnt++; x[i][j + 1] = 0; x[i][j] = 0; x[i + 1][j + 1] = 0; return; }
int p2 = Query(i, j, i, j + 1), p3 = p1 - p2;
if (p2 == 0) { x[i][j] = 0; x[i][j + 1] = 0; x[i + 1][j + 1] = 1; return; }
if (p3 == 0) { x[i + 1][j] = 0; x[i + 1][j + 1] = 0; }
if (p2 == 1) {
if (j == 48) { x[i][j] = Query(i, j, i, j); x[i][j + 1] = 1 - x[i][j]; }
else {
int v1 = Query(i, j + 1, i, j + 2);
if (v1 == 0) { x[i][j] = 1; x[i][j + 1] = 0; x[i][j + 2] = 0; }
if (v1 == 2) { x[i][j] = 0; x[i][j + 1] = 1; x[i][j + 2] = 1; }
if (v1 == 1) { x[i][j + 1] = -2; }
}
}
}
}
inline void solve2(int row, int col, int p1) {
int i = row, j = col;
if ((x[i][j] != -1 || x[i + 1][j] != -1) && p1 == 1) { solve3(i, j, p1); }
else {
if (p1 == 0) { x[i][j] = 0; x[i][j + 1] = 0; x[i + 1][j] = 0; x[i + 1][j + 1] = 0; }
else if (p1 == 4) { x[i][j] = 1; x[i][j + 1] = 1; x[i + 1][j] = 1; x[i + 1][j + 1] = 1; }
else {
int p2 = Query(i, j, i, j + 1), p3 = p1 - p2;
if (p2 == 0) { x[i][j] = 0; x[i][j + 1] = 0; }
if (p2 == 2) { x[i][j] = 1; x[i][j + 1] = 1; }
if (p3 == 0) { x[i + 1][j] = 0; x[i + 1][j + 1] = 0; }
if (p3 == 2) { x[i + 1][j] = 1; x[i + 1][j + 1] = 1; }
if (p2 == 1 && p3 == 1) {
int p4 = Query(i, j, i + 1, j);
if (p4 == 0) { x[i][j] = 0; x[i + 1][j] = 0; x[i][j + 1] = 1; x[i + 1][j + 1] = 1; }
else if (p4 == 2) { x[i][j] = 1; x[i + 1][j] = 1; x[i][j + 1] = 0; x[i + 1][j + 1] = 0; }
else {
int p5 = Query(i, j + 1, i, j + 1);
x[i][j + 1] = p5;
x[i][j] = p2 - p5;
x[i + 1][j] = p4 - x[i][j];
x[i + 1][j + 1] = p3 - x[i + 1][j];
}
}
else {
if (p2 == 1) {
if (j == 48) { x[i][j] = Query(i, j, i, j); x[i][j + 1] = 1 - x[i][j]; }
else {
int v1 = Query(i, j + 1, i, j + 2);
if (v1 == 0) { x[i][j] = 1; x[i][j + 1] = 0; x[i][j + 2] = 0; }
if (v1 == 2) { x[i][j] = 0; x[i][j + 1] = 1; x[i][j + 2] = 1; }
if (v1 == 1) { x[i][j + 1] = -2; }
}
}
if (p3 == 1) {
if (j == 48) { x[i + 1][j] = Query(i + 1, j, i + 1, j); x[i + 1][j + 1] = 1 - x[i + 1][j]; }
else {
int v1 = Query(i + 1, j + 1, i + 1, j + 2);
if (v1 == 0) { x[i + 1][j] = 1; x[i + 1][j + 1] = 0; x[i + 1][j + 2] = 0; }
if (v1 == 2) { x[i + 1][j] = 0; x[i + 1][j + 1] = 1; x[i + 1][j + 2] = 1; }
if (v1 == 1) { x[i + 1][j + 1] = -2; }
}
}
}
}
}
}
inline void solve4(int row, int col, int p1) {
int i = row, j = col;
if (p1 == 0)return;
int p2 = Query(i, j, i + 1, j + 3), p3 = p1 - p2;
if (p2 == 0) {}
if (p2 == 8) { for (int I = i + 0; I < i + 2; I++) { for (int J = j + 0; J < j + 4; J++) { x[I][J] = 1; } } }
if (p3 == 0) {}
if (p3 == 8) { for (int I = i + 2; I < i + 4; I++) { for (int J = j + 0; J < j + 4; J++) { x[I][J] = 1; } } }
if (p2 >= 1 && p2 <= 7 && p3 >= 1 && p3 <= 7) {
int p4 = Query(i, j, i + 3, j + 1), p5 = p1 - p4;
if (p4 != 0 && p5 != 0) {
int p10 = Query(i, j, i + 1, j + 1), p11 = p2 - p10;
int p12 = p4 - p10, p13 = p5 - p11;
solve2(i, j, p10); solve2(i, j + 2, p11);
solve2(i + 2, j, p12); solve2(i + 2, j + 2, p13);
}
if (p4 == 0) {
solve2(i, j, 0); solve2(i + 2, j, 0);
solve2(i, j + 2, p2); solve2(i + 2, j + 2, p3);
}
else if (p5 == 0) {
solve2(i, j, p2); solve2(i + 2, j, p3);
solve2(i, j + 2, 0); solve2(i + 2, j + 2, 0);
}
}
else {
if (p2 >= 1 && p2 <= 7) { int p6 = Query(i, j, i + 1, j + 1), p7 = p2 - p6; solve2(i, j, p6); solve2(i, j + 2, p7); }
if (p3 >= 1 && p3 <= 7) { int p8 = Query(i + 2, j, i + 3, j + 1), p9 = p3 - p8; solve2(i + 2, j, p8); solve2(i + 2, j + 2, p9); }
}
}
inline void saiki(int row, int col, int hs, int ws, int p1) {
if (row >= 50 || col >= 50)return;
if (hs == 2 && ws == 2) { solve2(row, col, p1); return; }
if (hs == 4 && ws == 4) { solve4(row, col, p1); return; }
if (hs > ws) {
int E1 = p1;
int EE1, EE2;
if (E1 == hs*ws) { EE1 = ws*Div[hs]; EE2 = ws*(hs - Div[hs]); }
else if (E1 == 0) { EE1 = 0; EE2 = 0; }
else { EE1 = Query(row, col, row + Div[hs] - 1, col + ws - 1); EE2 = E1 - EE1; }
saiki(row, col, Div[hs], ws, EE1);
saiki(row + Div[hs], col, hs - Div[hs], ws, EE2);
}
else {
int E1 = p1;
int EE1, EE2;
if (E1 == hs*ws) { EE1 = hs*Div[ws]; EE2 = hs*(ws - Div[ws]); }
else if (E1 == 0) { EE1 = 0; EE2 = 0; }
else { EE1 = Query(row, col, row + hs - 1, col + Div[ws] - 1); EE2 = E1 - EE1; }
saiki(row, col, hs, Div[ws], EE1);
saiki(row, col + Div[ws], hs, ws - Div[ws], EE2);
}
}
int main() {
//Init(1);
for (int i = 0; i < 2500; i++)x[i / 50][i % 50] = -1;
int H, W, N; cin >> H >> W >> N >> KK;
for (int i = 1; i <= 50; i++) {
if (i > 32)Div[i] = 32;
else if (i > 16)Div[i] = 16;
else if (i > 8)Div[i] = 8;
else if (i > 4)Div[i] = 4;
else if (i > 2)Div[i] = 2;
else if (i > 1)Div[i] = 1;
}
saiki(0, 0, 50, 50, 250);
for (int i = 0; i < 50; i++) { for (int j = 0; j < 50; j++) { if (x[i][j] == -1)x[i][j] = 0; } }
for (int i = 0; i < 50; i++) {
for (int j = 48; j >= 0; j--) {
if (x[i][j] == -2) {
x[i][j] = 1 - x[i][j + 1];
x[i][j - 1] = 1 - x[i][j];
}
}
}
int t=clock();
while(clock()-t<=(KK%100)*1000){}
//solve(x);
cout << "! " << solve() << endl;
return 0;
}
I2luY2x1ZGU8aW9zdHJlYW0+CiNpbmNsdWRlPHZlY3Rvcj4KI2luY2x1ZGU8Y3RpbWU+Ci8vI2luY2x1ZGUiQm9tYnNHYW1lLmgiCnVzaW5nIG5hbWVzcGFjZSBzdGQ7CnZlY3Rvcjx2ZWN0b3I8aW50Pj4geCg1MCwgdmVjdG9yPGludD4oNTAsIDApKTsKaW50IERpdls1MV07CmludCBzY250PTAsS0s9MDsKaW5saW5lIGludCBzb2x2ZSgpIHsKCWludCBxID0gMSwgcCA9IDA7Cglmb3IgKGludCBpID0gMDsgaSA8IDUwOyBpKyspIHsKCQlmb3IgKGludCBqID0gMDsgaiA8IDUwOyBqKyspIHsKCQkJaWYgKHhbaV1bal0gPT0gMSlwICs9IHE7CgkJCXEgKj0gMjsgcSAlPSBLSzsgcCAlPSBLSzsKCQl9Cgl9CglyZXR1cm4gcDsKfQppbmxpbmUgaW50IFF1ZXJ5KGludCBpMSwgaW50IGkyLCBpbnQgaTMsIGludCBpNCkgewoJY291dCA8PCAiPyAiIDw8IGkxIDw8ICcgJyA8PCBpMiA8PCAnICcgPDwgaTMgPDwgJyAnIDw8IGk0IDw8IGVuZGw7CglpbnQgVmE7IGNpbiA+PiBWYTsKCXJldHVybiBWYTsKfQppbmxpbmUgdm9pZCBzb2x2ZTMoaW50IHJvdywgaW50IGNvbCwgaW50IHAxKSB7CglpbnQgaSA9IHJvdywgaiA9IGNvbDsKCWlmKHAxIT0xKXJldHVybjsKCWlmIChwMSA9PSAwKSB7IHhbaV1bal0gPSAwOyB4W2ldW2ogKyAxXSA9IDA7IHhbaSArIDFdW2pdID0gMDsgeFtpICsgMV1baiArIDFdID0gMDsgcmV0dXJuOyB9CgllbHNlIGlmIChwMSA9PSA0KSB7IHhbaV1bal0gPSAxOyB4W2ldW2ogKyAxXSA9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