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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
#include <bits/stdc++.h>
#include <chrono>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
//using namespace chrono;
using namespace __gnu_pbds;
#pragma GCC optimize("Ofast")
//#pragma GCC optimize("Ofast,unroll-loops")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma,tune=native")
#pragma GCC target("sse,sse2,sse3,mmx,abm,tune=native") // for szkopul and sio only
typedef long long lld;
typedef double lf;
typedef long double llf;
typedef pair<int,int> pii;
typedef pair<lld,lld> pll;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> oset;
#define For(i,s,a) for(int i = s; i < a; ++i)
#define rpt(s, it) for(auto it = s.begin(); it != s.end(); ++it)
#define brpt(s, it) for(auto it = s.rend(); it != s.rbegin(); --it)
#define pb push_back
#define eb emplace_back
#define ff first
#define dd second
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define make_unique(x) (x).erase( unique(all(x)), (x).end())
#define popcnt(x) __builtin_popcount(x)
#define sz size()
#define time_since duration_cast< milliseconds >(system_clock::now().time_since_epoch())
 
template<typename Ta, typename Tb>
ostream & operator <<(ostream & os, pair<Ta, Tb> x){
	return os << x.ff << " " << x.dd;
}

const int N = 1e6 + 1;
const int H = 16;
vector<short> xlinkbot[H][840], xlinkup[H][840], ylinkbot[H][840], ylinkup[H][840];
bool banned[15009];
//int xd, xdd;
#define bigxd 15
#define smallxd 7

int getxr(int pos, int dest, int tim) {
	int res = 0;
	//cout << pos << " " << dest << " " << tim << " " << xlink[rot][0][tim][pos] << endl;
	while(xlinkup[0][tim][pos] < dest) {
	//	cout << pos << " " << dest << " " << tim << " " << xlink[rot][0][tim][pos] << endl;
	//	getchar();
		int step = xlinkup[smallxd][tim][pos] < dest ? (xlinkup[bigxd][tim][pos] < dest ? bigxd : smallxd) : 0;
		while(step < H - 1 && xlinkup[step + 1][tim][pos] < dest)
			++step;
		res += (1 << step) >> 1;
		bool change = 0;
		if(pos == xlinkup[step][tim][pos])
			change = 1;
		pos = xlinkup[step][tim][pos];
		if(change)	
			++tim, ++res;
		(tim += (1 << step) >> 1) %= 840;
	}
	return res;
}

int getxl(int pos, int dest, int tim) {
	int res = 0;
	//cout << pos << " " << dest << " " << tim << " " << xlink[rot][0][tim][pos] << endl;
	while(xlinkbot[0][tim][pos] > dest) {
	//	cout << pos << " " << dest << " " << tim << " " << xlink[rot][0][tim][pos] << endl;
	//	getchar();
		int step = xlinkbot[smallxd][tim][pos] > dest ? (xlinkbot[bigxd][tim][pos] > dest ? bigxd : smallxd) : 0;
		while(step < H - 1 && xlinkbot[step + 1][tim][pos] > dest)
			++step;
		res += (1 << step) >> 1;
		bool change = 0;
		if(pos == xlinkbot[step][tim][pos])
			change = 1;
		pos = xlinkbot[step][tim][pos];
		if(change)	
			++tim, ++res;
		(tim += (1 << step) >> 1) %= 840;
	}
	return res;
}

int getx(int pos, int dest, int tim) {
	if(dest == pos)
		return 0;
	bool rot = pos < dest;
	if(rot)
		return getxr(pos, dest, tim);
	else
		return getxl(pos, dest, tim);
	
	/*int res = 0;
	//cout << pos << " " << dest << " " << tim << " " << xlink[rot][0][tim][pos] << endl;
	while(rot ? xlink[rot][0][tim][pos] < dest : xlink[rot][0][tim][pos] > dest) {
	//	cout << pos << " " << dest << " " << tim << " " << xlink[rot][0][tim][pos] << endl;
	//	getchar();
		int step = 0;
		while(step < 17 && (rot ? xlink[rot][step + 1][tim][pos] < dest : xlink[rot][step + 1][tim][pos] > dest))
			++step;
		res += (1 << step) >> 1;
		bool change = 0;
		if(pos == xlink[rot][step][tim][pos])
			change = 1;
		pos = xlink[rot][step][tim][pos];
		if(change)	
			++tim, ++res;
		(tim += (1 << step) >> 1) %= 840;
	}
	return res;*/
}

int getyr(int pos, int dest, int tim) {
	int res = 0;
	//cout << pos << " " << dest << " " << tim << " " << xlink[rot][0][tim][pos] << endl;
	while(ylinkup[0][tim][pos] < dest) {
	//	cout << pos << " " << dest << " " << tim << " " << xlink[rot][0][tim][pos] << endl;
	//	getchar();
		int step = ylinkup[smallxd][tim][pos] < dest ? (ylinkup[bigxd][tim][pos] < dest ? bigxd : smallxd) : 0;
		while(step < H - 1 && ylinkup[step + 1][tim][pos] < dest)
			++step;
		res += (1 << step) >> 1;
		bool change = 0;
		if(pos == ylinkup[step][tim][pos])
			change = 1;
		pos = ylinkup[step][tim][pos];
		if(change)	
			++tim, ++res;
		(tim += (1 << step) >> 1) %= 840;
	}
	return res;
}

int getyl(int pos, int dest, int tim) {
	int res = 0;
	//cout << pos << " " << dest << " " << tim << " " << xlink[rot][0][tim][pos] << endl;
	while(ylinkbot[0][tim][pos] > dest) {
	//	cout << pos << " " << dest << " " << tim << " " << xlink[rot][0][tim][pos] << endl;
	//	getchar();
		int step = ylinkbot[smallxd][tim][pos] > dest ? (ylinkbot[bigxd][tim][pos] > dest ? bigxd : smallxd) : 0;
		while(step < H - 1 && ylinkbot[step + 1][tim][pos] > dest)
			++step;
		res += (1 << step) >> 1;
		bool change = 0;
		if(pos == ylinkbot[step][tim][pos])
			change = 1;
		pos = ylinkbot[step][tim][pos];
		if(change)	
			++tim, ++res;
		(tim += (1 << step) >> 1) %= 840;
	}
	return res;
}

int gety(int pos, int dest, int tim) {
	if(dest == pos)
		return 0;
	bool rot = pos < dest;
	if(rot)
		return getyr(pos, dest, tim);
	else
		return getyl(pos, dest, tim);
	/*int res = 0;
	//cout << pos << " " << dest << " " << tim << " " << xlink[rot][0][tim][pos] << endl;
	while(rot ? ylink[rot][0][tim][pos] < dest : ylink[rot][0][tim][pos] > dest) {
	///	cout << pos << " " << dest << " " << tim << " " << xlink[rot][0][tim][pos] << endl;
	//	getchar();
		int step = 0;
		while(step < 17 && (rot ? ylink[rot][step + 1][tim][pos] < dest : ylink[rot][step + 1][tim][pos] > dest))
			++step;
		res += (1 << step) >> 1;
		bool change = 0;
		if(pos == ylink[rot][step][tim][pos])
			change = 1;
		pos = ylink[rot][step][tim][pos];
		if(change)	
			++tim, ++res;
		(tim += (1 << step) >> 1) %= 840;
	}
	return res;*/
}

inline void scan(int *i) {
	int t = 0;
	char z = getchar_unlocked();
	while((z < '0') || (z > '9'))
		z = getchar_unlocked();
	while((z >= '0') && (z <= '9'))
		t = (t << 3) + (t << 1) + z - '0',
		z = getchar_unlocked();
	*i = t;
}

char mystos[11];
int _end, halp;

inline void print(int i) {
	if(i == 0)
		putchar('0');
	else {
		_end = 0;
		while(i) {
			halp = (i >> 1) / 5;
			mystos[++_end] = '0' + i - (halp << 3) - (halp << 1);
			i = halp;
		}
		while(_end)
			putchar_unlocked(mystos[_end--]);
	}
}

void solve(void) {
	int n, m, q;
	scanf("%d%d%d", &n, &m, &q);
	/*n = 15000;
	m = 67;
	q = 1000000;*/
	//cerr << n << " " << m << " "<< q << endl;
	For(h, 0, H)
	For(k, 0, 840)
		xlinkup[h][k].resize(m + 2),
		xlinkbot[h][k].resize(m + 2),
		ylinkup[h][k].resize(n + 2),
		ylinkbot[h][k].resize(n + 2);
		
	char lights[n + 1][m + 1][8];
	short lens[n + 1][m + 1];
	For(i, 1, n + 1)
	For(j, 1, m + 1) {
		scanf("%s", lights[i][j]);
		//For(k, 0, 8)
		//	lights[i][j][k] = (k % 8 == i % 8) + '0';
		lens[i][j] = strlen(lights[i][j]);
	}
	
	For(tim, 0, 840) {
		int last = 0;
		memset(banned, 0, sizeof(banned[0]) * (n + 2));
		For(i, 1, n + 1) {
			bool ok = 1;
			For(j, 1, m + 1) {
				ok &= (lights[i][j][tim % lens[i][j]] & 1);
				if(!ok)
					break;
			}
			if(ok) {
	//			if(tim < 8)
	//			cout << tim << " banning x " << i << endl;
				banned[i] = 1;
				last = i;
			}
			ylinkbot[0][tim][i] = last;
		}
		last = n;
		for(int i = n; i + 1; --i) {
			ylinkup[0][tim][i] = last;
			if(banned[i])
				last = i - 1;
		}
		
		last = 0;
		memset(banned, 0, sizeof(banned[0]) * (m + 2));
		For(j, 1, m + 1) {
			bool ok = 1;
			For(i, 1, n + 1) {
				ok &= !(lights[i][j][tim % lens[i][j]] & 1);
				if(!ok)
					break;
			}
			if(ok) {
	//			if(tim < 8)
	//			cout << tim << " wanning y " << j << endl;
				banned[j] = 1;
				last = j;
			}
			xlinkbot[0][tim][j] = last;
		}
		last = m;
		for(int j = m; j + 1; --j) {
			xlinkup[0][tim][j] = last;
			if(banned[j])
				last = j - 1;
		}
	}
	For(h, 1, H) {
		int nxt_tim = (1 << (h == 1 ? h - 1 : h - 2)) % 840;
		For(tim, 0, 840) {
			For(i, 0, m + 1) {
				xlinkbot[h][tim][i] = xlinkbot[h - 1][nxt_tim][xlinkbot[h - 1][tim][i]];
				xlinkup[h][tim][i] = xlinkup[h - 1][nxt_tim][xlinkup[h - 1][tim][i]];	
			}
			For(j, 0, n + 1) {
				ylinkbot[h][tim][j] = ylinkbot[h - 1][nxt_tim][ylinkbot[h - 1][tim][j]];
				ylinkup[h][tim][j] = ylinkup[h - 1][nxt_tim][ylinkup[h - 1][tim][j]];
			}
			++nxt_tim;
			if(nxt_tim == 840)
				nxt_tim = 0;
		}
	}
	
	/*For(i, 0, m + 1) {
		For(h, 0, 5) 	
			cout << ylinkbot[h][2][i] << " " << ylinkup[h][2][i] << "   ";
		puts("");
	}*/
	int wyn = 0;
	For(qq, 0, q) {
		int tim, x, y, dx, dy;
		scan(&tim); scan(&x); scan(&y); scan(&dx); scan(&dy);
		//scanf("%lld%d%d%d%d", &tim, &x, &y, &dx, &dy);
		/*tim = 1000000000 - qq;
		x = y = 0;
		dx = n;
		dy = m;*/
		int resx, resy;
		//resx = resy = 0;
		resx = getx(y, dy, tim % 840);
		resy = gety(x, dx, tim % 840);
		//wyn += resx + resy;
		//wyn = max(wyn, max(resx, resy));
		print(tim + max(resx, resy));
		putchar_unlocked('\n');
		//printf("%d\n", /*tim +*/ max(resx, resy));
	}
	//cout << wyn << endl;
	//printf("%lld_", wyn);
	//puts("kocham_arke_gdynie");
//	cout << xdd << endl;
}

int32_t main(void){
	int t = 1;
	//scanf("%d", &t);
	while(t--)
		solve();
}

/*
2 2 7
01 1100
001 10
1 0 1 2 1
*/

/*
6 8 4
00100110 110110 110110 00100110 110110 00100110 110110 110110
110110 00100110 00100110 00100110 110110 00100110 110110 110110
110110 00100110 110110 00100110 110110 110110 110110 110110
00100110 00100110 00100110 110110 110110 00100110 00100110 110110
110110 00100110 00100110 110110 00100110 00100110 00100110 00100110
110110 110110 110110 00100110 110110 00100110 00100110 110110
99999373 0 8 6 4
943222083 2 3 2 8
204958385 2 5 0 3
563152842 3 4 6 2
*/

/*
1545
1708
1997
2436
513
207
1803
1100
1315
1659
1330
1177
125
4065
1001
1638
3096
1553
571
2078
540
2116
547
*/