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
#include <iostream>
#include <vector>
#include <string>
#include <set>

using namespace std;

struct BlockedN {
	bool blocked(int i, int N) { return i == N; };
};
struct BlockedZero {
	bool blocked(int i, int N) { return i == 0; };
};

int nwd(int a, int b) {
	while (b != 0) {
		int c = a % b;
		a = b;
		b = c;
	}
	return a;
}
int nww(int a, int b) {
	return (a*b)/nwd(a,b);
}

template <typename Block>
struct V : Block {
	vector<int> v;
	int N{0};
	void add(const string &s) {
		int sz = v.size();
		int l = s.length();
		int newsz = l;
		if (sz > 0)
			newsz = nww(l, sz);
		v.resize(newsz, 0);
		if (sz > 0)
			for (unsigned i = sz; i < v.size(); i++)
				v[i] = v[i%sz];
		for (unsigned i = 0; i < v.size(); i++)
			v[i] += s[i%l] == '0' ? 0 : 1;
		N++;
	}
	bool blocked(int i) {
		return Block::blocked(v[i%v.size()], N);
	}
	void print() {
		for (auto i: v)
			cout << (Block::blocked(i, N) ? "1" : "0");
		cout << endl;
	}
};

using Row = V<BlockedN>;
using Column = V<BlockedZero>;

typedef pair<int, int> P;
struct Query : P {
	Query(int idx, int t_, int tm_, int a_, int b_, int c_, int d_)
		: P{tm_, idx}, t{t_}, a{a_}, b{b_}, c{c_}, d{d_}
	{}
	int t;
	int a;
	int b;
	int c;
	int d;
	int result{0};
};

typedef std::pair<int, Query *> QWrap;

typedef set<QWrap> S;

struct SetWrap {
	S *s{nullptr};
	void insert(QWrap q) {
		if (!s) s = new S;
		s->insert(q);
	}
	void merge(SetWrap &other) {
		if (!s) {
			s = other.s;
		} else {
			if (s->size() <= other.s->size()) {
				s->merge(*other.s);
				delete other.s;
			} else {
				other.s->merge(*s);
				delete s;
				s = other.s;
			}
		}
		other.s = nullptr;
	}
	bool empty() {
		if (!s)
			return true; 
		else
			return s->empty();
	}
	void erase(auto it) {
		s->erase(it);
		if (s->empty()) {
			delete s;
			s = nullptr;
		}
	}
	auto begin() { return s->begin(); }
	auto end() { return s->end(); }
};

int main() {
	ios_base::sync_with_stdio(false);

	int N, M, Q;
	cin >> N;
	cin >> M;
	cin >> Q;

	vector<Row> vr(N);
	vector<Column> vc(M);

	int NWW = 1;

	for (int n = 0; n < N; n++) {
		for (int m = 0; m < M; m++) {
			string s;
			cin >> s;
			vr[n].add(s);
			vc[m].add(s);
			NWW = nww(s.length(), NWW);
		}
	}

	vector<vector<int>> timeline_down(NWW, vector<int>(N+1, 0));
	vector<vector<int>> timeline_up(NWW, vector<int>(N+1, 0));
	vector<vector<int>> timeline_left(NWW, vector<int>(M+1, 0));
	vector<vector<int>> timeline_right(NWW, vector<int>(M+1, 0));

	for (int i = 0; i < NWW; i++) {
		for (int j = 0; j < N; j++) {
			if (vr[j].blocked(i)) {
				timeline_down[i][j] = -1;
				timeline_up[i][j+1] = -1;
			}
		}
		for (int j = 0; j < M; j++) {
			if (vc[j].blocked(i)) {
				timeline_right[i][j] = -1;
				timeline_left[i][j+1] = -1;
			}
		}
	}

	auto fill_timeline = [](auto &timeline, int i, int start, int stop, int step) {
		int last = start;
		for (int j = start; j != stop; j += step) {
			if (timeline[i][j] == 0)
				timeline[i][j] = last;
			else
				last = j;
		}
	};

	for (int i = 0; i < NWW; i++) {
		fill_timeline(timeline_down, i, N, -1, -1);
		fill_timeline(timeline_up, i, 0, N+1, 1);
		fill_timeline(timeline_right, i, M, -1, -1);
		fill_timeline(timeline_left, i, 0, M+1, 1);
	}

	vector<Query> vq;
	vq.reserve(Q);

	for (int q = 0; q < Q; q++) {
		int t, a, b, c, d;
		cin >> t >> a >> b >> c >> d;
		vq.push_back(Query{q, t, t%NWW, a, b, c, d});
	}

	typedef vector<S> VSet;
	vector<VSet> solver(NWW, VSet(max(N+1, M+1), S()));


	auto beginf = [](auto &s) { return s.begin(); };
	auto rbeginf = [](auto &s) { return --s.end(); };

	int cnt = 0;
	auto solve = [&solver, &cnt, &NWW](const auto &timeline, auto beginfun, auto cmp) {
		int i = 0;
		while (cnt > 0) {
			int timing = i%NWW;
			for (unsigned j = 0; j < timeline[timing].size(); j++) {
				int next = timeline[timing][j];
				if (next == -1) {
					next = j;
				}
				S &this_set = solver[timing][j];
				S &next_set = solver[(timing+1)%NWW][next];
				next_set.merge(this_set);
				while (!next_set.empty() && cmp(beginfun(next_set)->first, next)) {
					auto it = beginfun(next_set);
					QWrap finisher = *it;
					next_set.erase(it);
					finisher.second->result = max(finisher.second->result, i);
					cnt--;
				}
			}
			i++;
		}
	};

	for (auto &item: vq) {
		if (item.a == item.c && item.b == item.d)
			item.result = item.first;
	}

	for (auto &item: vq) {
		if (item.a < item.c) {
			solver[item.first][item.a].insert(QWrap(item.c, &item));
			cnt++;
		}
	}
	solve(timeline_down, beginf, less_equal<int>());
	for (auto &item: vq) {
		if (item.a > item.c) {
			solver[item.first][item.a].insert(QWrap(item.c, &item));
			cnt++;
		}
	}
	solve(timeline_up, rbeginf, greater_equal<int>());
	for (auto &item: vq) {
		if (item.b < item.d) {
			solver[item.first][item.b].insert(QWrap(item.d, &item));
			cnt++;
		}
	}
	solve(timeline_right, beginf, less_equal<int>());
	for (auto &item: vq) {
		if (item.b > item.d) {
			solver[item.first][item.b].insert(QWrap(item.d, &item));
			cnt++;
		}
	}
	solve(timeline_left, rbeginf, greater_equal<int>());

	for (int q = 0; q < Q; q++) {
		cout << vq[q].result + (vq[q].t/NWW)*NWW << endl;
	}

	return 0;
}