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
#include "bits/stdc++.h" // Tomasz Nowak
using namespace std;     // University of Warsaw
using LL = long long;
#define FOR(i, l, r) for(int i = (l); i <= (r); ++i)
#define REP(i, n) FOR(i, 0, (n) - 1)
#define ssize(x) int(x.size())
template<class A, class B> auto& operator<<(ostream &o, pair<A, B> p) {
	return o << '(' << p.first << ", " << p.second << ')';
}
template<class T> auto operator<<(ostream &o, T x) -> decltype(x.end(), o) {
	o << '{'; int i = 0; for(auto e : x) o << (", ")+2*!i++ << e; return o << '}';
}
ostream&operator<<(ostream&o,string s){return o<<s.data();}
#ifdef DEBUG
#define debug(x...) cerr << "[" #x "]: ", [](auto... $) {((cerr << $ << "; "), ...); }(x), cerr << '\n'
#else
#define debug(...) {}
#endif

constexpr int nww = 8 * 7 * 5 * 3;

int main() {
	cin.tie(0)->sync_with_stdio(0);

	int n, m, q;
// #define TEST_RUN
#ifdef TEST_RUN
	mt19937 rng(0);
	auto rd = [&](int l, int r) {
		return rng()%(r-l+1)+l;
	};
	n = 15000;
	m = int(1e6) / n;
	q = int(1e6);
#else
	cin >> n >> m >> q;
#endif

	vector signals(m, vector<string>(n));
	REP(y, n)
		REP(x, m) {
#ifdef TEST_RUN
			int len = rd(2, 8);
			string s;
			while(len --> 0)
				s += char('0' + rd(0, 1));
			while(*max_element(s.begin(), s.end()) == '0' or *min_element(s.begin(), s.end()) == '1')
				s[rd(0, ssize(s) - 1)] = char('0' + rd(0, 1));
			signals[x][y] = s;
#else
			cin >> signals[x][y];
#endif
		}

	vector<tuple<int, pair<int, int>, pair<int, int>>> queries(q);
	for(auto &[t, p1, p2] : queries) {
		auto &[x1, y1] = p1;
		auto &[x2, y2] = p2;
#ifdef TEST_RUN
		constexpr int max_t = int(1e9);
		t = rd(0, max_t);
		y1 = rd(0, n);
		x1 = rd(0, m);
		y2 = rd(0, n);
		x2 = rd(0, m);
#else	
		cin >> t >> y1 >> x1 >> y2 >> x2;
#endif
		debug(t, x1, y1, x2, y2);
	}

	auto is_valid = [&](int x, int y) {
		return 0 <= min(x, y) and x <= m and y <= n;
	};
	constexpr int sides = 4;
	const array dx = {0, 1, -1, 0};
	const array dy = {1, 0, 0, -1};

	auto get_signal = [&](int x, int y, int t) {
		return signals[x][y][t % ssize(signals[x][y])] == '1';
	};

	auto can_pass = [&](int x, int y, int t, int dir) {
		assert(is_valid(x, y));
		assert(is_valid(x + dx[dir], y + dy[dir]));
		assert(0 <= t and t < nww);
		assert(0 <= dir and dir < sides);

		if(dir == 2) // lewo
			return (y != 0 and get_signal(x - 1, y - 1, t) == 1) or (y != n and get_signal(x - 1, y, t) == 1);
		else if(dir == 1) // prawo
			return (y != 0 and get_signal(x, y - 1, t) == 1) or (y != n and get_signal(x, y, t) == 1);
		else if(dir == 3) // dół
			return (x != 0 and get_signal(x - 1, y - 1, t) == 0) or (x != m and get_signal(x, y - 1, t) == 0);
		else // góra
			return (x != 0 and get_signal(x - 1, y, t) == 0) or (x != m and get_signal(x, y, t) == 0);
	};

	vector visited(m + 1, vector<bool>(n + 1, false));
	auto get_side = [&](int t) {
		vector<pair<int, int>> visited_v;
		int max_x = 0, max_y = 0;
		function<bool (int, int)> comp_dfs = [&](int x, int y) {
			debug(x, y);
			visited[x][y] = true;
			visited_v.emplace_back(x, y);
			max_x = max(max_x, x);
			max_y = max(max_y, y);
			if(max_x == m or max_y == n)
				return true;
			REP(s, sides) {
				int xx = x + dx[s];
				int yy = y + dy[s];
				if(is_valid(xx, yy) and not visited[xx][yy] and can_pass(x, y, t, s))
					if(comp_dfs(xx, yy))
						return true;
			}
			return false;
		};
#ifdef LOCAL
		assert(ssize(visited_v) < 2 * (n + m));
#endif
		comp_dfs(0, 0);
		for(auto [x, y] : visited_v)
			visited[x][y] = false;
		return max_y == n;
	};
	vector<bool> starting_side(nww);
	REP(t, nww)
		starting_side[t] = get_side(t);
	debug(starting_side);

	auto mirror_input = [&] {
		vector new_signals(n, vector<string>(m));
		REP(y, n)
			REP(x, m) {
				new_signals[y][x] = signals[x][y];
				for(char &c : new_signals[y][x])
					c = (c == '1' ? '0' : '1');
			}
		signals = new_signals;

		for(auto &[t, p1, p2] : queries) {
			auto &[x1, y1] = p1;
			auto &[x2, y2] = p2;
			swap(x1, y1);
			swap(x2, y2);
			debug(t, p1, p2);
		}

		REP(i, nww)
			starting_side[i] = not starting_side[i];

		swap(n, m);
	};

	auto get_can_move = [&] {
		vector or_for_len(m, vector(9, vector<bool>()));
		REP(x, m)
			FOR(len, 2, 8)
				or_for_len[x][len].resize(len);

		REP(i, m)
			REP(y, n)
				REP(j, ssize(signals[i][y]))
					if(signals[i][y][j] == '1')
						or_for_len[i][ssize(signals[i][y])][j] = true;

		vector ret(m, vector<bool>(nww));
		REP(i, m)
			REP(j, nww)
				FOR(len, 2, 8)
					if(or_for_len[i][len][j % len])
						ret[i][j] = true;
		return ret;
	};

	auto get_next_i_table = [&] {
		debug(signals);
		auto can_move = get_can_move();
		debug(can_move);

		vector next_i(m, vector(nww, -1));
		REP(i, m)
			REP(j, nww) {
				int delta_t = 0;
				while(not can_move[i][(j + delta_t) % nww]) // oh shoot ja tu kwadracę?
					++delta_t;
				debug(i, j, can_move[i][j], delta_t);
				next_i[i][j] = delta_t;
			}
		return next_i;
	};

	vector<int> answer(q, -1);
	REP(iter_mirror, 2) {
		auto next_i = get_next_i_table();
		REP(iter, 2) {
			debug(iter_mirror, iter);
			debug(next_i);

			vector<int> considering_queries;
			REP(i, q) {
				auto &[t, p1, p2] = queries[i];
				auto &[x1, y1] = p1;
				auto &[x2, y2] = p2;
				if(answer[i] != -1)
					continue;
				if(x1 > x2 or starting_side[t % nww] == 0)
					continue;
				assert(x1 <= x2);
				debug("considering", i, t, p1, p2);
				considering_queries.emplace_back(i);
				answer[i] = t;
				t %= nww;
			}
			debug(iter_mirror, iter, considering_queries);

			vector jmp = next_i;

			for(int b = 0; true; ++b) {
				for(int i : considering_queries) {
					auto &[t, p1, p2] = queries[i];
					auto &[x1, y1] = p1;
					auto &[x2, y2] = p2;

					debug(i, b, x1 + (1 << b));
					if((x2 - x1) & (1 << b)) {
						// assert(ssize(jmp[t]) > x1);
						int delta_t = jmp[x1][t];
						debug(b, x1, t, delta_t);
						assert(delta_t != -1);
						x1 += (1 << b);
						answer[i] += delta_t;
						t = (t + delta_t) % nww;
					}
					debug(x1, x2, answer[i], t);
				}

				int new_len = m + 1 - (1 << (b + 1));
				debug(b, new_len);
				if(new_len <= 0)
					break;

				vector new_jmp(new_len, vector(nww, -1));
				REP(i, new_len)
					REP(j, nww)
						new_jmp[i][j] = jmp[i][j] + jmp[i + (1 << b)][(j + jmp[i][j]) % nww];
				swap(jmp, new_jmp);
			}
			for(int i : considering_queries) {
				auto &[t, p1, p2] = queries[i];
				auto &[x1, y1] = p1;
				auto &[x2, y2] = p2;
				assert(x1 == x2);
			}

			reverse(next_i.begin(), next_i.end());
			for(auto &[t, p1, p2] : queries) {
				auto &[x1, y1] = p1;
				auto &[x2, y2] = p2;
				x1 = m - x1;
				x2 = m - x2;
				y1 = n - y1;
				y2 = n - y2;
			}
		}
		mirror_input();
	}
	for(int a : answer)
		cout << a << '\n';
}