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
#include<vector>
#include<iostream>
//#include<algorithm>
//#include <stdio.h>
#include <set>
#include <map>
#include <string>

using namespace std;

#define MAX_SIDE 15005
#define MAX_CYCLE 840

struct query {
	int t;
	int a;
	int b;
	int c;
	int d;
};

struct fu {
	int leader;
	int rank;
};

//int s[MAX_SIDE][MAX_SIDE][MAX_CYCLE];




int fu_find(vector<fu> *v, int i) {
	if (v->at(i).leader != i) {
		v->at(i).leader = fu_find(v, v->at(i).leader);
	}
	return v->at(i).leader;
}

void fu_union(vector<fu> *v, int a, int b) {
	int i = fu_find(v, a); int j = fu_find(v, b);
	if (i == j) return;
	
	if (v->at(i).rank < v->at(j).rank) {
		v->at(i).leader = j;
		v->at(j).rank += v->at(i).rank;
	} else {
		v->at(j).leader = i;
		v->at(i).rank += v->at(j).rank;
	}
}

int to_leader(int i, int j, int m) {
	return i * (m+1) + j;
}

//int to_x_coord(int leader, int m) {
//	return leader / (m+1);
//}

//int to_y_coord(int leader, int m) {
//	return leader % (m+1);
//}


//void print_fu(vector< vector<fu> > leaders, int t) {
//		cout <<"============ " << t << " =============" << endl;
//		for (int i = 0; i <= n; ++i) {
//			for (int j = 0; j <= m; ++j) {
//				printf("%d ", fu_find(&leaders[t], to_leader(i, j)));
//			}
//			printf("\n");
//		}
//}

//void print_trans(vector< map<int, set<int> > > transitions, int t) {
//		cout << "TRANSITIONS " << t << endl;
//		map<int, set<int> > x = transitions[t];
//		for (map<int, set<int> >::iterator it = x.begin(); it != x.end(); it++) {
//			set<int> y = it->second;
//			printf("KEY %d: ", it->first);
//			for (set<int>::iterator j = y.begin(); j != y.end(); j++) {
//				printf("%d ", *j);
//			}
//			printf("\n");
//		}
//		cout << endl;
//}


int main()
{
	int n;
	int m;
	int q;
	cin >> n;
	cin >> m;
	cin >> q;
	
	int s[n+10][m+10][MAX_CYCLE];
	
	//cout << n << " " << m << " " << q << endl;
	
	vector< vector<fu> > leaders(MAX_CYCLE);
	vector< map<int, set<int> > > transitions(MAX_CYCLE);
	
	for (int i = 0; i < n; ++i) {
	    for (int j = 0; j < m; ++j) {
			string x;
			cin >> x;
			for (int pos = 0; pos < MAX_CYCLE; pos++) {
				s[i][j][pos] = x[pos % x.size()] - 48;
			}
		}
	}

	//printf("READ QUERIES\n");
	vector<query> queries;
	for (int i = 0; i < q; ++i) {
		int t, a, b, c, d;
		queries.push_back(query());
		cin >> t >> a >> b >> c >> d;
		//scanf("%d %d %d %d %d", &t, &a, &b, &c, &d);
		queries.back().t = t;
		queries.back().a = a;
		queries.back().b = b;
		queries.back().c = c;
		queries.back().d = d;
	}
	
	//printf("SET INITIAL LEADERS\n");
	
	for (int t = 0; t < MAX_CYCLE; ++t) {
		//printf("============ %d ==================\n", t);
		for (int i = 0; i <= n; ++i) {
			for (int j = 0; j <= m; ++j) {
				leaders[t].push_back(fu());
				leaders[t].back().leader = to_leader(i,j,m);
				leaders[t].back().rank = 1;
				//printf("ADDED LEADER %d %d %d\n", i, j, to_leader(i,j));
			}
		}
	}
	
	//printf("INITIAL LEADERS SET\n");

	
	////////////////////// POLICZ FIND UNION
	
	for (int t = 0; t < MAX_CYCLE; ++t) {	// FIXME
		for (int i = 0; i <= n; ++i) {
			//printf("========== ITERATION i=%d ==============\n", i);
			for (int j = 0; j <= m; ++j) {
				//printf("========== ITERATION i=%d j=%d ==============\n", i, j);
				// pole nad
				if (i - 1 >= 0) {
					//printf("POLE NAD ");
					// lewe przejście
					if (j - 1 >= 0) {
						//printf("LEWE PRZ ");
						int cross_i = i-1;
						int cross_j = j-1;
						if (s[cross_i][cross_j][t] == 0) { // "pozioma ulica" + "pionowe przejście"
							//printf("Jest przejście w górę lewym z (%d, %d) --> (%d, %d)\n", i, j, (i-1), j);
							fu_union(&leaders[t], to_leader(i,j,m), to_leader(i-1,j,m));
						}
					}
					// prawe przejście 
					if (j < m) {
						//printf("PRAWE PRZ ");
						int cross_i = i-1;
						int cross_j = j;
						if (s[cross_i][cross_j][t] == 0) { // "pozioma ulica" + "pionowe przejście"
							//printf("Jest przejście w górę prawym z (%d, %d) --> (%d, %d)\n", i, j, (i-1), j);
							fu_union(&leaders[t], to_leader(i,j,m), to_leader(i-1,j,m));
						}
					}
					//printf("\n");
				}
				// pole pod
				if (i + 1 <= n) {
					//printf("POLE POD ");
					// lewe przejście
					if (j - 1 >= 0) {
						//printf("LEWE PRZ ");
						int cross_i = i;
						int cross_j = j-1;
						if (s[cross_i][cross_j][t] == 0) { // "pozioma ulica" + "pionowe przejście"
							fu_union(&leaders[t], to_leader(i,j,m), to_leader(i+1,j,m));
							//printf("Jest przejście w dół lewym z (%d, %d) --> (%d, %d)\n", i, j, (i+1), j);
						}
					}
					// prawe przejście 
					if (j < m) {
						//printf("PRAWE PRZ ");
						int cross_i = i;
						int cross_j = j;
						if (s[cross_i][cross_j][t] == 0) { // "pozioma ulica" + "pionowe przejście"
							fu_union(&leaders[t], to_leader(i,j,m), to_leader(i+1,j,m));
							//printf("Jest przejście w dół prawym z (%d, %d) --> (%d, %d)\n", i, j, (i+1), j);
						}
					}
					//printf("\n");
				}
				// pole na lewo
				if (j - 1 >= 0) {
					//printf("POLE NA LEWO ");
					// górne przejście
					if (i - 1 >= 0) {
						//printf("GÓRNE PRZ ");
						int cross_i = i-1;
						int cross_j = j-1;
						if (s[cross_i][cross_j][t] == 1) { // "pionowa ulica" + "poziome przejście"
							fu_union(&leaders[t], to_leader(i,j,m), to_leader(i,j-1,m));
							//printf("Jest przejście na lewo górnym z (%d, %d) --> (%d, %d)\n", i, j, i, (j-1));
						}
					}
					// dolne przejście 
					if (i < n) {
						//printf("DOLNE PRZ ");
						int cross_i = i;
						int cross_j = j-1;
						if (s[cross_i][cross_j][t] == 1) { // "pionowa ulica" + "poziome przejście"
							fu_union(&leaders[t], to_leader(i,j,m), to_leader(i, j-1,m));
							//printf("Jest przejście na lewo dolnym z (%d, %d) --> (%d, %d)\n", i, j, i, (j-1));
						}
					}
					//printf("\n");
				}
				// pole na prawo
				if (j < n) {
					//printf("POLE NA PRAWO ");
					// górne przejście
					if (i - 1 >= 0) {
						//printf("GÓRNE PRZ ");
						int cross_i = i-1;
						int cross_j = j;
						if (s[cross_i][cross_j][t] == 1) { // "pionowa ulica" + "poziome przejście"
							fu_union(&leaders[t], to_leader(i,j,m), to_leader(i,j+1,m));
							//printf("Jest przejście na prawo górnym z (%d, %d) --> (%d, %d)\n", i, j, i, (j+1));
						}
					}
					// dolne przejście 
					if (i < n) {
						//printf("DOLNE PRZ ");
						int cross_i = i;
						int cross_j = j;
						if (s[cross_i][cross_j][t] == 1) { // "pionowa ulica" + "poziome przejście"
							fu_union(&leaders[t], to_leader(i,j,m), to_leader(i, j+1,m));
							//printf("Jest przejście na prawo dolnym z (%d, %d) --> (%d, %d)\n", i, j, i, (j+1));
						}
					}
					//printf("\n");
				}
			}
		}
	}
	////////////////////// KONIEC POLICZ FIND UNION
	
	//for (int t = 0; t < 0; ++t) {	// FIXME
	//	cout <<"============ " << t << " =============" << endl;
	//	for (int i = 0; i <= n; ++i) {
	//		for (int j = 0; j <= m; ++j) {
	//			printf("%d ", fu_find(&leaders[t], to_leader(i, j)));
	//		}
	//		printf("\n");
	//	}
	//}
	
	// CALCULATE TRANSITIONS
	for (int t = 0; t < MAX_CYCLE; ++t) {
		//cout << "TRANSITIONS " << t << endl;
		for (int i = 0; i <= n; ++i) {
			for (int j = 0; j <= m; ++j) {
				int leader = fu_find(&leaders[t], to_leader(i, j,m));
				int next_minute_leader = fu_find(&leaders[(t + 1) % MAX_CYCLE], to_leader(i, j,m));
				if (transitions[t].find(leader) == transitions[t].end()) {
					transitions[t].insert(pair<int, set<int> >(leader, set<int>()));
				}
				transitions[t].find(leader)->second.insert(next_minute_leader);
			}
		}
	}
	
	//for (int i = 0; i < 0; ++i) {
	//	cout << "TRANSITIONS " << i << endl;
	//	map<int, set<int> > x = transitions[i];
	//	for (map<int, set<int> >::iterator it = x.begin(); it != x.end(); it++) {
	//		set<int> y = it->second;
	//		printf("KEY %d: ", it->first);
	//		for (set<int>::iterator j = y.begin(); j != y.end(); j++) {
	//			printf("%d ", *j);
	//		}
	//		printf("\n");
	//	}
	//	cout << endl;
	//}	
	
	for (vector<query>::iterator it = queries.begin(); it != queries.end(); ++it) {
		int start_t = it->t;
		int start_t_modulo = start_t % MAX_CYCLE;
		int start_x = it->a;
		int start_y = it->b;
		int end_x = it->c;
		int end_y = it->d;
		
		//cout << "============== QUERY: " << start_t << " " << start_x << " " << start_y << " " << end_x << " " << end_y << endl; 
		
		//print_fu(leaders, start_t_modulo);
		//print_fu(leaders, (start_t_modulo + 1) % MAX_CYCLE);
		//print_trans(transitions, start_t_modulo);
		
		set<int> set1;
		set<int> set2;
	
		set<int> *prev = &set1;
		set<int> *next = &set2;
		
		int starting_leader = fu_find(&leaders[start_t_modulo], to_leader(start_x, start_y,m));
		int ending_leader = fu_find(&leaders[start_t_modulo], to_leader(end_x, end_y,m));
		
		if (starting_leader == ending_leader) {
			//cout << "STARTING_LEADER = ENDING_LEADER" << endl;
			cout << start_t << endl;
			continue;
		} else {
			prev->insert(starting_leader);
		}
		while (true) {
			int previous_start = start_t_modulo; 
			start_t++;
			start_t_modulo = start_t % MAX_CYCLE;
			//cout << "TESTING time=" << start_t << endl;
			//for (set<int>::iterator j = prev->begin(); j != prev->end(); j++) {
			//	cout << (*j) << " ";
			//}
			//cout << endl;
			
			int ending_leader = fu_find(&leaders[start_t_modulo], to_leader(end_x, end_y,m));
			
			
			for (set<int>::iterator j = prev->begin(); j != prev->end(); j++) {
				//cout << "ROZPATRUJEMY " << (*j) << endl;
				set<int> with_transition = transitions[previous_start].find(*j)->second;
				for(set<int>::iterator k = with_transition.begin(); k != with_transition.end(); k++) {
					//cout << "DODAJEMY " << (*k) << endl;
					next->insert(*k);
				}
			}
			
			//cout << "NEXT ";
			//for (set<int>::iterator j = next->begin(); j != next->end(); j++) {
			//	cout << (*j) << " ";
			//}
			//cout << endl;
			
			if (next->find(ending_leader) != next->end()) {
				cout << start_t << endl;
				break;
			}
			
			set<int> *tmp = prev;
			prev = next;
			next = tmp;
			next->clear();
		}
	}
	
}