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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
#define e1 first
#define e2 second
#define pb push_back
#define OUT(x) {cout << x << "\n"; exit(0); }
#define TCOUT(x) {cout << x << "\n"; return; }
#define FOR(i, l, r) for(int i = (l); i <= (r); ++i)
#define rep(i, l, r) for(int i = (l); i < (r); ++i)
#define boost {ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); }
#define sz(x) int(x.size())
#define trav(a, x) for(auto& a : x)
#define all(x) begin(x), end(x)
typedef long long ll;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vll;
/*#include <atcoder/modint>
using namespace atcoder;
using mint = modint998244353;
vector <mint> fac, inv;
mint binom(int n, int k) {
	if (n < k || n < 0) return 0;
	return fac[n] * inv[k] * inv[n-k];
}

void prep(int N) {
	fac.resize(N+1, 1); inv.resize(N+1, 1);
	for (int i=1; i<=N; ++i) fac[i] = fac[i-1] * i;
	inv[N] = fac[N].inv();
	for (int i=N-1; i>0; --i) inv[i] = inv[i+1] * (i + 1);
}*/

mt19937_64 rng(2137);
int random(int l, int r) {
	return uniform_int_distribution<int>(l, r)(rng);
}
#ifdef DEBUG
template<class T> int size(T &&x) {
	return int(x.size());
}
template<class A, class B> ostream& operator<<(ostream &out, const pair<A, B> &p) {
	return out << '(' << p.first << ", " << p.second << ')';
}
template<class T> auto operator<<(ostream &out, T &&x) -> decltype(x.begin(), out) {
	out << '{';
	for(auto it = x.begin(); it != x.end(); ++it)
		out << *it << (it == prev(x.end()) ? "" : ", ");
	return out << '}';
}
void dump() {}
template<class T, class... Args> void dump(T &&x, Args... args) {
	cerr << x << ";  ";
	dump(args...);
}
#endif
#ifdef DEBUG
  struct Nl{~Nl(){cerr << '\n';}};
# define debug(x...) cerr << (strcmp(#x, "") ? #x ":  " : ""), dump(x), Nl(), cerr << ""
#else
# define debug(...) 0 && cerr
#endif

// credits to nor, nor orz
namespace fastio {

    static constexpr int SZ = 1 << 17;
    char ibuf[SZ], obuf[SZ];
    int pil = 0, pir = 0, por = 0;

    inline void load() {
        memmove(ibuf, ibuf + pil, pir - pil);
        pir = pir - pil +
              (int)fread_unlocked(ibuf + pir - pil, 1, SZ - pir + pil, stdin);
        pil = 0;
    }

    inline void rd(char& c) {
        if (pil + 32 > pir) load();
        c = ibuf[pil++];
    }
    template <typename T>
    inline void rd(T& x) {
        if (pil + 32 > pir) load();
        char c;
        do c = ibuf[pil++];
        while (c < '-');
        [[maybe_unused]] bool minus = false;
        if constexpr (std::is_signed<T>::value == true) {
            if (c == '-') minus = true, c = ibuf[pil++];
        }
        x = 0;
        while (c >= '0') {
            x = x * 10 + (c & 15);
            c = ibuf[pil++];
        }
        if constexpr (std::is_signed<T>::value == true) {
            if (minus) x = -x;
        }
    }
    
    void readString(string&ret) {
		if (pil + 32 > pir) load();
		char c = ibuf[pil++];
		while (c == '0' || c == '1') {
			ret += c;
			c = ibuf[pil++];
		}
	}
	
    inline void rd() {}
    template <typename Head, typename... Tail>
    inline void rd(Head& head, Tail&... tail) {
        rd(head);
        rd(tail...);
    }

    inline void skip_space() {
        if (pil + 32 > pir) load();
        while (ibuf[pil] <= ' ') pil++;
    }

    inline void flush() {
        fwrite_unlocked(obuf, 1, por, stdout);
        por = 0;
    }

    struct Pre {
        char num[40000];
        constexpr Pre() : num() {
            for (int i = 0; i < 10000; i++) {
                int n = i;
                for (int j = 3; j >= 0; j--) {
                    num[i * 4 + j] = n % 10 + '0';
                    n /= 10;
                }
            }
        }
    } constexpr pre;

    struct Post {
        Post() { std::atexit(flush); }
    } post;

    inline void wt(char c) {
        if (por > SZ - 32) flush();
        obuf[por++] = c;
    }
    inline void wt(bool b) {
        if (por > SZ - 32) flush();
        obuf[por++] = b ? '1' : '0';
    }
    template <typename T>
    inline void wt(T x) {
        if (por > SZ - 32) flush();
        if (!x) {
            obuf[por++] = '0';
            return;
        }
        if constexpr (std::is_signed<T>::value == true) {
            if (x < 0) obuf[por++] = '-', x = -x;
        }
        int i = 12;
        char buf[16];
        while (x >= 10000) {
            memcpy(buf + i, pre.num + (x % 10000) * 4, 4);
            x /= 10000;
            i -= 4;
        }
        if (x < 100) {
            if (x < 10) {
                obuf[por] = char('0' + x);
                ++por;
            } else {
                uint32_t q = (uint32_t(x) * 205) >> 11;
                uint32_t r = uint32_t(x) - q * 10;
                obuf[por] = char('0' + q);
                obuf[por + 1] = char('0' + r);
                por += 2;
            }
        } else {
            if (x < 1000) {
                memcpy(obuf + por, pre.num + (x << 2) + 1, 3);
                por += 3;
            } else {
                memcpy(obuf + por, pre.num + (x << 2), 4);
                por += 4;
            }
        }
        memcpy(obuf + por, buf + i + 4, 12 - i);
        por += 12 - i;
    }

    inline void wt() {}
    template <typename Head, typename... Tail>
    inline void wt(Head&& head, Tail&&... tail) {
        wt(head);
        wt(std::forward<Tail>(tail)...);
    }
    template <typename... Args>
    inline void wtn(Args&&... x) {
        wt(std::forward<Args>(x)...);
        wt('\n');
    }

}  // namespace fastio
using fastio::rd;
using fastio::wtn;

//Did you REAALLY consider sample tests?

const int maxn = 15010;
const int maxq = 1000100;
int A[maxq], B[maxq], C[maxq], D[maxq], T[maxq], WROW[maxq], WCOL[maxq];
const int LCM = 840;
int n, m, q;
vector <vector<string> > input;
int bad_row[maxn][LCM]; //row is bad if it only has zeroes
int bad_col[maxn][LCM]; //columns is bad if it only has ones
int pos_row[maxq], pos_col[maxq];

const int LOG = 14;

int skok_row_left[2][LCM][maxn]; //how much time it takes to jump this much from this dude
int skok_row_right[2][LCM][maxn];
int skok_col_up[2][LCM][maxn];
int skok_col_down[2][LCM][maxn];

inline int ustal(int x, int mod) {
	while (x >= mod) x -= mod;
	return x;
}

void wzo() {
	// calculate bad rows and columns
	rep(i, 0, n) {
		rep(j, 0, m) {
			int k = sz(input[i][j]);
			for (int znak = 0; znak < k; ++znak) {
				if (input[i][j][znak] == '1') { //this column not bad
					for (int time = znak; time < LCM; time += k) bad_col[j][time] = 0;
				}
				else {
					for (int time = znak; time < LCM; time += k) bad_row[i][time] = 0;
				}
			}
		}
	}
	
	// calculate transition graph and answer queries
	FOR(i, 1, q) pos_row[i] = A[i], pos_col[i] = B[i];
	
	// initial calculation
	rep(time, 0, LCM) {
		rep(i, 0, n) {
			int delta = 0;
			while (bad_row[i][ustal(time + delta, LCM)]) ++delta;
			skok_row_right[0][time][i] = delta;
			skok_row_left[0][time][i + 1] = delta;
		}
		
		rep(i, 0, m) {
			int delta = 0;
			while (bad_col[i][ustal(time + delta, LCM)]) ++delta;
			skok_col_down[0][time][i] = delta;
			skok_col_up[0][time][i + 1] = delta;
		}
	}
	
	for (int iter = 0; iter < LOG; ++iter) {
		int bit = (iter % 2); int other = 1 - bit;
		int ile_poprzedni = (1 << (iter - 1));
		int ten_skok = (1 << iter);
		if (iter > 0) { //transition step
			for (int time = 0; time < LCM; ++time) {
				rep(i, 0, n) {
					if (i + ten_skok > n) break;
					int first_jump_time = skok_row_right[other][time][i];
					int second_jump_time = skok_row_right[other][ustal(time + first_jump_time, LCM)][i + ile_poprzedni];
					skok_row_right[bit][time][i] = first_jump_time + second_jump_time;
				}
				
				for (int i=n; i>0; --i) {
					if (i - ten_skok < 0) break;
					int first_jump_time = skok_row_left[other][time][i];
					int second_jump_time = skok_row_left[other][ustal(time + first_jump_time, LCM)][i - ile_poprzedni];
					skok_row_left[bit][time][i] = first_jump_time + second_jump_time;
				}
				
				rep(i, 0, m) {
					if (i + ten_skok > m) break;
					int first_jump_time = skok_col_down[other][time][i];
					int second_jump_time = skok_col_down[other][ustal(time + first_jump_time, LCM)][i + ile_poprzedni];
					skok_col_down[bit][time][i] = first_jump_time + second_jump_time;
				}
				
				for (int i=m; i>0; --i) {
					if (i - ten_skok < 0) break;
					int first_jump_time = skok_col_up[other][time][i];
					int second_jump_time = skok_col_up[other][ustal(time + first_jump_time, LCM)][i - ile_poprzedni];
					skok_col_up[bit][time][i] = first_jump_time + second_jump_time;
				}
			}
		}
		
		// query move step
		for (int zap = 1; zap <= q; ++zap) {
			if (pos_row[zap] < C[zap]) { //prawo
				int len = abs(pos_row[zap] - C[zap]);
				if (len & ten_skok) {
					int curr_time = (T[zap] + WROW[zap]) % LCM;
					WROW[zap] += skok_row_right[bit][curr_time][pos_row[zap]];
					pos_row[zap] += ten_skok;
				}
			}
			if (pos_row[zap] > C[zap]) { //w lewo
				int len = abs(pos_row[zap] - C[zap]);
				if (len & ten_skok) {
					int curr_time = (T[zap] + WROW[zap]) % LCM;
					WROW[zap] += skok_row_left[bit][curr_time][pos_row[zap]];
					pos_row[zap] -= ten_skok;
				}
			}
			if (pos_col[zap] < D[zap]) { //w dol ekranu
				int len = abs(pos_col[zap] - D[zap]);
				if (len & ten_skok) {
					int curr_time = (T[zap] + WCOL[zap]) % LCM;
					WCOL[zap] += skok_col_down[bit][curr_time][pos_col[zap]];
					pos_col[zap] += ten_skok;
				}
			}
			if (pos_col[zap] > D[zap]) { //w gore ekranu
				int len = abs(pos_col[zap] - D[zap]);
				if (len & ten_skok) {
					int curr_time = (T[zap] + WCOL[zap]) % LCM;
					WCOL[zap] += skok_col_up[bit][curr_time][pos_col[zap]];
					pos_col[zap] -= ten_skok;
				}
			}
		}
	}
	
	FOR(zap, 1, q) {
		assert(WROW[zap] == 0 || WCOL[zap] == 0);
		wtn(WROW[zap] + WCOL[zap] + T[zap]);
	}
	
}


bool TEST = 0;

int main() {
	cin.tie(nullptr)->sync_with_stdio(false);
	if (!TEST) {
		rd(n, m, q);
		input.resize(n, vector<string>(m, ""));
		rep(time, 0, LCM) {
			FOR(i, 0, n) bad_row[i][time] = 1;
			FOR(i, 0, m) bad_col[i][time] = 1;
		}
		
		rep(i, 0, n) {
			rep(j, 0, m) {
				string s = "";
				fastio::readString(s);
				input[i][j] = s;
			}
		}
		
		FOR(zap, 1, q) {
			rd(T[zap], A[zap], B[zap], C[zap], D[zap]);
			WROW[zap] = WCOL[zap] = 0;
		}
		
		wzo();
	}
	else {
		n = 15000, m = 100, q = 1000000;
		input.resize(n, vector<string>(m, ""));
		rep(time, 0, LCM) {
			FOR(i, 0, n) bad_row[i][time] = 1;
			FOR(i, 0, m) bad_col[i][time] = 1;
		}
		
		//cout << n << ' ' << m << ' ' << zap << "\n";
		rep(i, 0, n) {
			rep(j, 0, m) {
				int r = random(2, 8);
				string output = "";
				int suma = 0;
				FOR(step, 1, r) {
					int rnd = random(0, 1);
					suma += rnd;
					output += (char)(rnd + '0');
				}
				if (suma == 0) {
					output.pop_back();
					output += '1';
					++suma;
				}
				if (suma == r) {
					output.pop_back();
					output += '0';
					--suma;
				}
				assert(suma != 0);
				assert(suma = r);
				
				input[i][j] = output;
			}
		}
		FOR(zap, 1, q) {
			int t = random(0, (int)1e9);
			//cout << t << ' ' << random(0, n) << ' ' << random(0, m) << ' ' << random(0, n) << ' ' << random(0, m) << "\n";
			T[zap] = t;
			A[zap] = random(0, n);
			B[zap] = random(0, m);
			C[zap] = random(0, n);
			D[zap] = random(0, m);
			WROW[zap] = WCOL[zap] = 0;
		}
		
		wzo();
	}
}