1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast,unroll-loops")
using namespace std;
typedef long long lld;
typedef unsigned int uint;
typedef pair<lld, lld> pll;
#define ff first
#define ss second
#define mp make_pair
#define pb push_back

/// SPRAWDZIĆ CZY NIC NIE WYCHODZI POZA ZAKRESY PAMIĘCI
/// wszystko - czasy (T) też

constexpr uint TT = 840;
constexpr uint MNN = 15360;

uint MN = 15360;
uint T = 840;
uint K = 7, ROW;


// tablica[n][m]
// odwołujemy się przez (n) oraz (m + ROW)

uint lca[2][7][TT][MNN];
//vector<vector<vector<vector<uint>>>> lca;
bitset<MNN> green[TT];
vector<uint> scheme[MNN];
uint n, m, q, lcm = 1;
char s[16];

void read_schemes() {
	//cout << sizeof(lca) + sizeof(green) + sizeof(scheme) << endl;
	scanf("%u%u%u", &n, &m, &q);	
	ROW = n + 1; 
	
	/*
	MN = m + n + 4;
	T = 1;
	K = 0;
	while (1u << (K + K) <= m || 1u << (K + K) <= n) ++K;
	*/
	
	for (uint i = 0; i < n; ++i) {
		scheme[i].resize(m);
		for (uint j = 0; j < m; ++j) {
			scanf (" %s", s);
			uint k = 0, val = 0;
			while (s[k]) ++k;
			
			for (int l = k - 1; l >= 0; --l) {
				val <<= 1;
				val += s[l] - '0';
			}
			
			//T = (T * k) / __gcd(T, k);
			
			scheme[i][j] = (k << 16) + val;
		}
	}
}

void fill_green_lights() {
	for (uint t = 0; t < T; ++t) {
		for (uint i = 0; i < n; ++i) {
			for (uint j = 0; j < m; ++j) {
				uint k = scheme[i][j] >> 16, val = scheme[i][j];
				
				if (1 & (val >> (t % k))) {
					green[t][j + ROW] = 1;
				}
				else {
					green[t][i] = 1;
				}
				
				//green[t][i] = green[t][i] | ((1 & (val >> (t % k))) == 1);
				//green[t][j + ROW] = green[t][j + ROW] | ((1 & (val >> (t % k))) == 0);
			}
		}
	}
	
	/*
	for(int t = 0; t < 20; ++t) {
		printf("%d - :", t);
		for (uint i = 0; i < n; ++i) {
			printf(" %d", (int)green[t][i]);
		}
		printf("\n%d | :", t);
		for (uint i = ROW; i < ROW + m; ++i) {
			printf(" %d", (int)green[t][i]);
		}
		puts("");
	} 
	*/
}

void init_lca() {
	/*lca = vector<vector<vector<vector<uint>>>>(2, 
		vector<vector<vector<uint>>>(K, 
			vector<vector<uint>>(T, 
				vector<uint>(MN, 0u))));
	*/	
	/// ------- init lca na poziomie 0 -------
	
	for (uint t = 0; t < T; ++t) {
		for (uint i = 0; i < n; ++i) {
			int w = 0;
			while (!green[(t + w) % T][i]) ++w;
			lca[0][0][t][i + 1] = w;
			lca[1][0][t][i] = w;
		}
		
		for (uint i = ROW; i < ROW + m; ++i) {
			int w = 0;
			while (!green[(t + w) % T][i]) ++w;
			lca[0][0][t][i + 1] = w;
			lca[1][0][t][i] = w;
		}
	}
}

void rest_lca() {
	
	/// ------- 0 czyli chodzenie do tyłu -------
	
	for (uint k = 1, leap = 1; k < K; ++k, leap *= 4) {
		for (uint t = 0; t < T; ++t) {
			for (uint i = (leap * 4); i <= n; ++i) {
				int w = lca[0][k - 1][t][i]; 
				w += lca[0][k - 1][(t + w) % T][i - leap];
				w += lca[0][k - 1][(t + w) % T][i - leap - leap];
				w += lca[0][k - 1][(t + w) % T][i - leap - leap - leap];
				lca[0][k][t][i] = w;
			}
			
			for (uint i = ROW + (leap * 4); i <= ROW + m; ++i) {
				int w = lca[0][k - 1][t][i]; 
				w += lca[0][k - 1][(t + w) % T][i - leap];
				w += lca[0][k - 1][(t + w) % T][i - leap - leap];
				w += lca[0][k - 1][(t + w) % T][i - leap - leap - leap];
				lca[0][k][t][i] = w;
			}
		}
	}
	
	//puts("przeszedłem przez wypełnianie lca[0]");
	
	/// ------- 1 czyli chodzenie do przodu -------
	
	for (uint k = 1, leap = 1; k < K; ++k, leap *= 4) {
		for (uint t = 0; t < T; ++t) {
			for (uint i = 0; i + (leap * 4) <= n; ++i) {
				int w = lca[1][k - 1][t][i]; 
				w += lca[1][k - 1][(t + w) % T][i + leap];
				w += lca[1][k - 1][(t + w) % T][i + leap + leap];
				w += lca[1][k - 1][(t + w) % T][i + leap + leap + leap];
				lca[1][k][t][i] = w;
			}
			
			for (uint i = ROW; i + (leap * 4) <= ROW + m; ++i) {
				int w = lca[1][k - 1][t][i]; 
				w += lca[1][k - 1][(t + w) % T][i + leap];
				w += lca[1][k - 1][(t + w) % T][i + leap + leap];
				w += lca[1][k - 1][(t + w) % T][i + leap + leap + leap];
				lca[1][k][t][i] = w;
			}
		}
	}
	
	//puts("przeszedłem przez wypełnianie lca[1]");
	
	//printf("%u\n", lca[0][0][15][4]);
	
}

void fill_lca() {
	fill_green_lights();
	
	init_lca();
	
	rest_lca();
}

void query(uint t, uint x, uint y, uint xx, uint yy) {
	uint w = 0, res = 0;
	uint k = K - 1;
	y += ROW;
	yy += ROW;
	
	while (x < xx) {
		while(x + (1u << (k + k)) <= xx) {
			//printf("odległość od %u do %u wynosi: %u\n", x, x + (1u << (k + k)), lca[1][k][(t + w) % T][x]);
			w += lca[1][k][(t + w) % T][x];
			x += (1u << (k + k));
		}
		--k;
	}
	
	while (x > xx) {
		while(x >= (1u << (k + k)) + xx) {
			//printf("odległość od %u do %u wynosi: %u\n", x, x - (1u << (k + k)), lca[0][k][(t + w) % T][x]);
			w += lca[0][k][(t + w) % T][x];
			x -= (1u << (k + k));
		}
		--k;
	}
	
	res = w;
	w = 0;
	k = K - 1;
	
	while (y < yy) {
		while(y + (1u << (k + k)) <= yy) {
			//printf("odległość od %u do %u wynosi: %u\n", y, y + (1u << (k + k)), lca[1][k][(t + w) % T][y]);
			w += lca[1][k][(t + w) % T][y];
			y += (1u << (k + k));
		}
		--k;
	}
	
	while (y > yy) {
		while(y >= (1u << (k + k)) + yy) {
			//printf("odległość od %u do %u wynosi: %u\n", y, y - (1u << (k + k)), lca[0][k][(t + w) % T][y]);
			w += lca[0][k][(t + w) % T][y];
			y -= (1u << (k + k));
		}
		--k;
	}
	
	printf("%u\n", t + res + w);
}

void solve() {
	uint t, x, y, xx, yy;
	while (q--) {
		scanf("%u%u%u%u%u", &t, &x, &y, &xx, &yy);
		query(t, x, y, xx, yy);
	}
}

int main(){
	
	read_schemes();
	
	fill_lca();
	
	solve();
	
	return 0;
}
/*

2 2 7
01 1100
001 10
0 0 0 2 2
1 0 1 2 1
5 2 1 0 0
0 0 2 2 2
15 1 1 0 0
16 1 1 0 0
7 2 2 2 2

*/