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
#pragma GCC optimize ("O3")
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef double K;
constexpr int INF = 0x3f3f3f3f;

#define FOR(i, b, e) for(int i = (b); i < (e); i++)
#define TRAV(x, a) for(auto &x: (a))
#define SZ(x) ((int)(x).size())
#define PB push_back
#define X first
#define Y second

constexpr int N = 15'003;
constexpr int LC = 840;
constexpr int LOG = 16;

vector<vector<string>> s;

int typ[LC]; // 0 - poziome, 1 - pionowe
int podz[LC][N];
short dod[LOG][LC + 1][N], uje[LOG][LC + 1][N];
bool poz[N][9][8], pio[N][9][8];
int n, m;

void licz() {
	FOR(i, 0, n) FOR(j, 0, m) {
		int dl = SZ(s[i][j]);
		FOR(k, 0, dl) poz[j][dl][k] |= s[i][j][k] == '1', pio[i][dl][k] |= s[i][j][k] == '0';
	}
}

void prep(int r) {
	ii akt = {0, 0};
	while(akt.X < n && akt.Y < m) {
		if(s[akt.X][akt.Y][r % SZ(s[akt.X][akt.Y])] == '1') akt.Y++;
		else akt.X++;
	}
	typ[r] = akt.X == n;
	int spoj = 0;
	if(typ[r]) {
		FOR(i, 0, m) {
			podz[r][i] = spoj;
			bool ok = false;
			FOR(j, 2, 9) ok |= poz[i][j][r % j];
			spoj += !ok;
		}
		podz[r][m] = spoj;
	}
	else {
		FOR(i, 0, n) {
			podz[r][i] = spoj;
			bool ok = false;
			FOR(j, 2, 9) ok |= pio[i][j][r % j];
			spoj += !ok;
		}
		podz[r][n] = spoj;
	}
}

void skoki(int alc) {
	// ZEROWY
	FOR(r, 0, alc) {
		uje[0][r][0] = 0;
		FOR(i, 1, (typ[r] ? m : n) + 1) {
			uje[0][r][i] = uje[0][r][i - 1];
			if(podz[r][i] != podz[r][i - 1]) uje[0][r][i] = i;
		}
		dod[0][r][(typ[r] ? m : n)] = (typ[r] ? m : n);
		for(int i = (typ[r] ? m : n) - 1; i >= 0; i--) {
			dod[0][r][i] = dod[0][r][i + 1];
			if(podz[r][i] != podz[r][i + 1]) dod[0][r][i] = i;
		}
	}
	// PIERWSZY
	FOR(i, 0, (typ[0] ? m : n) + 1) uje[0][alc][i] = uje[0][0][i];
	FOR(i, 0, (typ[0] ? m : n) + 1) dod[0][alc][i] = dod[0][0][i];
	FOR(r, 0, alc) {
		FOR(i, 0, (typ[r] ? m : n) + 1) {
			if(typ[r] ^ typ[(r + 1) % alc]) {
				uje[0][r][i] = 0;
				dod[0][r][i] = N - 1;
			}
			else {
				uje[0][r][i] = uje[0][r + 1][uje[0][r][i]];
				dod[0][r][i] = dod[0][r + 1][dod[0][r][i]];
			}
		}
	}
	FOR(k, 0, LOG) FOR(r, 0, alc) dod[k][r][N - 1] = N - 1;
	FOR(k, 1, LOG) FOR(r, 0, alc) {
		int elaps = (r + (1 << (k - 1))) % alc;
		FOR(i, 0, (typ[r] ? m : n) + 1) {
			uje[k][r][i] = uje[k - 1][elaps][uje[k - 1][r][i]];
			dod[k][r][i] = dod[k - 1][elaps][dod[k - 1][r][i]];
		}
	}
}

void read(int &num) {
	num = 0;
	char c = getchar_unlocked();
	while(c < 33) c = getchar_unlocked();
	while(c > 47) {
		num = num * 10 + c - '0';
		c = getchar_unlocked();
	}
}

void readS(string &ss) {
	ss = "";
	char c = getchar_unlocked();
	while(c < 33) c = getchar_unlocked();
	while(c > 47) {
		ss += c;
		c = getchar_unlocked();
	}
}

void print(int &x) {
	string digs;
	do {
		digs += x % 10 + '0';
		x /= 10;
	}while(x);
	for(int i = SZ(digs) - 1; i >= 0; i--) putchar_unlocked(digs[i]);
	putchar_unlocked('\n');
}

void solve() {
	int q, alc = 1;
	read(n), read(m), read(q);
	s.resize(n, vector<string>(m));
	FOR(i, 0, n) FOR(j, 0, m) readS(s[i][j]);
	TRAV(x, s) TRAV(y, x) alc = lcm(alc, SZ(y));
	licz();
	FOR(i, 0, alc) prep(i);
	skoki(alc);
	FOR(i, 0, q) {
		int t, a, b, c, d;
		read(t), read(a), read(b), read(c), read(d);
		if(typ[t % alc]) swap(a, b), swap(c, d);
		if(podz[t % alc][a] != podz[t % alc][c]) {
			if(a < c) {
				for(int k = LOG - 1; k >= 0; k--) {
					while(dod[k][t % alc][a] < c) a = dod[k][t % alc][a], t += 1 << k;
				}
			}
			else {
				for(int k = LOG - 1; k >= 0; k--) {
					while(uje[k][t % alc][a] > c) a = uje[k][t % alc][a], t += 1 << k;
				}
			}
			t++;
		}
		print(t);
	}
}

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	// int tt; cin >> tt;
	// FOR(te, 0, tt) {
	// 	// cout << "Case #" << te + 1 << ": ";
	// 	solve();
	// }
	solve();
	return 0;
}