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
#include <bits/stdc++.h>
#define PB push_back
#define ST first
#define ND second

//#pragma GCC optimize ("O3")
//#pragma GCC target("tune=native")

//mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//using namespace __gnu_pbds;
//typedef tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;

using namespace std;
using ll = long long;
using pi = pair<int, int>;
using vi = vector<int>;

const int nax = 15 * 1000 + 10, c_lcm = 850, INF = 1e9, qax = 1e6 + 10;
int n, m, q, t;
vector<vector<string>> lights;
int valid[10][10];
vector<pi> hor_lines[c_lcm];
vector<pi> ver_lines[c_lcm];
int nxt = 1;
int d[12600000 + 10];
vi child[12600000 + 10];
vector<pi>parq[12600000 + 10];


struct Query {
	int ti, ai, bi, ci, di;
};

Query qrs[qax];
int res[qax];

int ogr_ver(int x, int ti) {
	int low = 0, high = (int)ver_lines[ti].size() - 1, mid;
	while(low <= high) {
		mid = (low + high) / 2;
		if(ver_lines[ti][mid].ST >= x) {
			high = mid - 1;
		} else {
			low = mid + 1;
		}
	}
	high++;
	if(high == (int)ver_lines[ti].size()) {
		return 0;
	} else {
		return ver_lines[ti][high].ND;
	}
}

int ogr_hor(int x, int ti) {
	int low = 0, high = (int)hor_lines[ti].size() - 1, mid;
	while(low <= high) {
		mid = (low + high) / 2;
		if(hor_lines[ti][mid].ST >= x) {
			high = mid - 1;
		} else {
			low = mid + 1;
		}
	}
	high++;
	if(high == (int)hor_lines[ti].size()) {
		return 0;
	} else {
		return hor_lines[ti][high].ND;
	}
}

vi S;

void dfs(int x) {
	S.PB(x);
	for(auto qr : parq[x]) {
		int low = 0, high = (int)S.size() - 1, mid;
		while(low <= high) {
			mid = (low + high) / 2;
			if(d[S[mid]] >= qr.ND) {
				low = mid + 1;
			} else {
				high = mid - 1;
			}
		}
		res[qr.ST] = (int)S.size() - low;
	}
	for(int y : child[x]) {
		dfs(y);
	}
	S.pop_back();
}

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cin >> n >> m >> q;
	//n = 15'000; m = 66; q = 1'000'000;
	lights.resize(n);
	t = 1;
	for(int i = 0; i < n; ++i) {
		lights[i].resize(m);
		for(int j = 0; j < m; ++j) {
			cin >> lights[i][j];
			//for(int k = 0; k < ((i+j) % 7) + 2; ++k) {
				//lights[i][j] += ((k&1)?"1":"0");
			//}
			t = t * (int)lights[i][j].size() / __gcd(t, (int)lights[i][j].size());
		}
	}
	// case 1) horizontal lines
	for(int i = 0; i < n; ++i) {
		for(int len = 1; len <= 8; ++len) {
			for(int r = 0; r < len; ++r) {
				valid[len][r] = true;
			}
		}
		for(int j = 0; j < m; ++j) {
			for(int r = 0; r < (int)lights[i][j].size(); ++r) {
				if(lights[i][j][r] == '0') {
					valid[(int)lights[i][j].size()][r] = false;
				}
			}
		}
		for(int ti = 0; ti < t; ++ti) {
			bool good = true;
			for(int len = 1; len <= 8; ++len) {
				if(!valid[len][ti % len]) {
					good = false;
					break;
				}
			}
			if(good) {
				d[nxt] = i;
				hor_lines[ti].PB({i, nxt++});
			}
		}
	}
	// case 2) vertical lines
	for(int j = 0; j < m; ++j) {
		for(int len = 1; len <= 8; ++len) {
			for(int r = 0; r < len; ++r) {
				valid[len][r] = true;
			}
		}
		for(int i = 0; i < n; ++i) {
			for(int r = 0; r < (int)lights[i][j].size(); ++r) {
				if(lights[i][j][r] == '1') {
					valid[(int)lights[i][j].size()][r] = false;
				}
			}
		}
		for(int ti = 0; ti < t; ++ti) {
			bool good = true;
			for(int len = 1; len <= 8; ++len) {
				if(!valid[len][ti % len]) {
					good = false;
					break;
				}
			}
			if(good) {
				d[nxt] = j;
				ver_lines[ti].PB({j,nxt++});
			}
		}
	}
	for(int i = 0; i < q; ++i) {
		int ti, ai, bi, ci, di;
		cin >> ti >> ai >> bi >> ci >> di;
		//ti = i; ai = 0, bi = 0, ci = n, di = m;
		qrs[i] = {ti, ai, bi, ci, di};
	}
	d[0] = INF;
	for(int rep = 0; rep < 2; ++rep) {
		for(int i = 0; i < t; ++i) {
			if((int)hor_lines[i].size() == 0) continue;
			int ip = (i + 1) % t;
			int ptr = 0;
			for(auto x : hor_lines[i]) {
				while(ptr < (int)hor_lines[ip].size() && hor_lines[ip][ptr].ST < x.ST) {
					ptr++;
				}
				if(ptr == (int)hor_lines[ip].size()) {
					child[0].PB(x.ND);
				} else {
					child[hor_lines[ip][ptr].ND].PB(x.ND);
				}
			}
		}
		for(int i = 0; i < t; ++i) {
			if((int)ver_lines[i].size() == 0) continue;
			int ip = (i + 1) % t;
			int ptr = 0;
			for(auto x : ver_lines[i]) {
				while(ptr < (int)ver_lines[ip].size() && ver_lines[ip][ptr].ST < x.ST) {
					ptr++;
				}
				if(ptr == (int)ver_lines[ip].size()) {
					child[0].PB(x.ND);
				} else {
					child[ver_lines[ip][ptr].ND].PB(x.ND);
				}
			}
		}
		for(int i = 0; i < q; ++i) {
			int ti, ai, bi, ci, di;
			ti = qrs[i].ti; ai = qrs[i].ai; bi = qrs[i].bi; ci = qrs[i].ci; di = qrs[i].di;
			ti %= t;
			int ida = ogr_hor(ai, ti), idc = ogr_hor(ci, ti);
			int idb = ogr_ver(bi, ti), idd = ogr_ver(di, ti);
			if(ida == idc && idb == idd) {
				res[i] = 0;
				continue;
			}
			if(ida != idc && d[ida] < d[idc]) {
				parq[ida].emplace_back(i, ci);
			} else if(idb != idd && d[idb] < d[idd]) {
				parq[idb].emplace_back(i, di);
			}
		}
		dfs(0);
		for(int i = 0; i < t; ++i) {
			for(auto &x : ver_lines[i]) x.ST = -x.ST-1;
			for(auto &x : hor_lines[i]) x.ST = -x.ST-1;
			reverse(ver_lines[i].begin(), ver_lines[i].end());
			reverse(hor_lines[i].begin(), hor_lines[i].end());
		}
		for(int i = 1; i < nxt; ++i) {
			d[i] = -d[i]-1;
			parq[i].clear();
			child[i].clear();
		}
		parq[0].clear();
		child[0].clear();
		for(int i = 0; i < q; ++i) {
			qrs[i].ai = -qrs[i].ai;
			qrs[i].bi = -qrs[i].bi;
			qrs[i].ci = -qrs[i].ci;
			qrs[i].di = -qrs[i].di;
		}
	}
	for(int i = 0; i < q; ++i) {
		cout << res[i] + qrs[i].ti << "\n";
	}
}