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
#include<bits/stdc++.h>
#include<unistd.h>
using namespace std;

const int LEN = 8;
const int M = 840; // gcd(2,3,4,5,6,7,8)
const int QUICK = 7; // Jump by 2^QUICK
const int MAXN = 15000;

int n,m;
bitset<M> preprocess[LEN+1][1<<LEN];
bitset<M> cols[MAXN], rows[MAXN];
short left_bound[MAXN+1][M], right_bound[MAXN+1][M];
short top_bound[MAXN+1][M], bottom_bound[MAXN+1][M];

short quick_left[QUICK+1][M][MAXN+1], quick_right[QUICK+1][M][MAXN+1];
short quick_top[QUICK+1][M][MAXN+1], quick_bottom[QUICK+1][M][MAXN+1];

short fast_left[MAXN+1], fast_right[MAXN+1];
short fast_top[MAXN+1], fast_bottom[MAXN+1];

bitset<M> construct(const string& s) { return preprocess[s.length()][stoi(s,0,2)]; }

constexpr int addM(int a, int b) { return (a+b >= M ? a+b-M : a+b); }

void prepare() { // Prepare structures for faster simulating
	// Calculte left
	for(int t=0;t<M;t++) left_bound[0][t] = 0;
	for(int i=1;i<=m;i++)
		for(int t=0;t<M;t++)
			left_bound[i][t] = (cols[i-1][t] == 0 ? i : left_bound[i-1][t]);

	// Calculate quick left
	for(int i=0;i<=m;i++)
		for(int t=0;t<M;t++)
			quick_left[0][t][i] = left_bound[i][t];
	for(int l=0;l<QUICK;l++)
		for(int t=0;t<M;t++)
			for(int i=0;i<=m;i++)
				quick_left[l+1][t][i] = quick_left[l][addM(t,(1<<l))][quick_left[l][t][i]];

	// Calculate fast left
	for(int i=0;i<=m;i++) fast_left[i] = left_bound[i][M-1];
	for(int t=M-2;t>=0;t--)
		for(int i=0;i<=m;i++)
			fast_left[i] = fast_left[left_bound[i][t]];

	// Calculte right
	for(int t=0;t<M;t++) right_bound[m][t] = m;
	for(int i=m-1;i>=0;i--)
		for(int t=0;t<M;t++)
			right_bound[i][t] = (cols[i][t] == 0 ? i : right_bound[i+1][t]);

	// Calculate quick right
	for(int i=0;i<=m;i++)
		for(int t=0;t<M;t++)
			quick_right[0][t][i] = right_bound[i][t];
	for(int l=0;l<QUICK;l++)
		for(int t=0;t<M;t++)
			for(int i=0;i<=m;i++)
				quick_right[l+1][t][i] = quick_right[l][addM(t,(1<<l))][quick_right[l][t][i]];

	// Calculate fast right
	for(int i=0;i<=m;i++) fast_right[i] = right_bound[i][M-1];
	for(int t=M-2;t>=0;t--)
		for(int i=0;i<=m;i++)
			fast_right[i] = fast_right[right_bound[i][t]];

	// Calculte top
	for(int t=0;t<M;t++) top_bound[0][t] = 0;
	for(int i=1;i<=n;i++)
		for(int t=0;t<M;t++)
			top_bound[i][t] = (rows[i-1][t] == 1 ? i : top_bound[i-1][t]);

	// Calculate quick top
	for(int i=0;i<=n;i++)
		for(int t=0;t<M;t++)
			quick_top[0][t][i] = top_bound[i][t];
	for(int l=0;l<QUICK;l++)
		for(int t=0;t<M;t++)
			for(int i=0;i<=n;i++)
				quick_top[l+1][t][i] = quick_top[l][addM(t,(1<<l))][quick_top[l][t][i]];

	// Calculate fast top
	for(int i=0;i<=n;i++) fast_top[i] = top_bound[i][M-1];
	for(int t=M-2;t>=0;t--)
		for(int i=0;i<=n;i++)
			fast_top[i] = fast_top[top_bound[i][t]];

	// Calculte bottom
	for(int t=0;t<M;t++) bottom_bound[n][t] = n;
	for(int i=n-1;i>=0;i--)
		for(int t=0;t<M;t++)
			bottom_bound[i][t] = (rows[i][t] == 1 ? i : bottom_bound[i+1][t]);

	// Calculate quick bottom
	for(int i=0;i<=n;i++)
		for(int t=0;t<M;t++)
			quick_bottom[0][t][i] = bottom_bound[i][t];
	for(int l=0;l<QUICK;l++)
		for(int t=0;t<M;t++)
			for(int i=0;i<=n;i++)
				quick_bottom[l+1][t][i] = quick_bottom[l][addM(t,(1<<l))][quick_bottom[l][t][i]];

	// Calculate fast bottom
	for(int i=0;i<=n;i++) fast_bottom[i] = bottom_bound[i][M-1];
	for(int t=M-2;t>=0;t--)
		for(int i=0;i<=n;i++)
			fast_bottom[i] = fast_bottom[bottom_bound[i][t]];
}

int solve(int tm, int x, int y, int p, int q) {
	int l,r,t,b;
	int lc,rc,tc,bc;
	l = r = y;
	t = b = x;
	int mod = tm%M;

	for(int Q=QUICK;Q>=1;Q--) // Quick
		while(mod+(1<<Q) < M) {
			lc = quick_left[Q][mod][l];
			rc = quick_right[Q][mod][r];
			tc = quick_top[Q][mod][t];
			bc = quick_bottom[Q][mod][b];
			if(lc <= q && q <= rc && tc <= p && p <= bc) break;
			l = lc, r = rc, t = tc, b = bc;
			mod = addM(mod,(1<<Q));
			tm += (1<<Q);
		}

	while(mod != 0) { // Naive
		l = left_bound[l][mod];
		r = right_bound[r][mod];
		t = top_bound[t][mod];
		b = bottom_bound[b][mod];
		if(l <= q && q <= r && t <= p && p <= b) return tm;
		mod = addM(mod,1);
		tm++;
	}

	while(true) { // Fast (change >= M/8 => at most about 150 iterations)
		lc = fast_left[l];
		rc = fast_right[r];
		tc = fast_top[t];
		bc = fast_bottom[b];
		if(lc <= q && q <= rc && tc <= p && p <= bc) break;
		l = lc, r = rc, t = tc, b = bc;
		tm += M;
	}

	for(int Q=QUICK;Q>=1;Q--) // Quick
		while(true) {
			lc = quick_left[Q][mod][l];
			rc = quick_right[Q][mod][r];
			tc = quick_top[Q][mod][t];
			bc = quick_bottom[Q][mod][b];
			if(lc <= q && q <= rc && tc <= p && p <= bc) break;
			l = lc, r = rc, t = tc, b = bc;
			mod = addM(mod,(1<<Q));
			tm += (1<<Q);
		}

	while(true) { // Naive
		l = left_bound[l][mod];
		r = right_bound[r][mod];
		t = top_bound[t][mod];
		b = bottom_bound[b][mod];
		if(l <= q && q <= r && t <= p && p <= b) return tm;
		mod = addM(mod,1);
		tm++;
	}
}

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);

	for(int i=2;i<=LEN;i++)
		for(int m=1;m<(1<<i);m++)
			for(int j=0,x=i-1;j<M;j++,x--) {
				if(x == -1) x = i-1;
				preprocess[i][m][j] = ((m&(1<<x))>>x);
			}

	int q;
	cin >> n >> m >> q;

	for(int i=0;i<n;i++) rows[i].set();

	string s;
	for(int i=0;i<n;i++)
		for(int j=0;j<m;j++) {
			cin >> s;
			auto x = construct(s);
			rows[i] &= x;
			cols[j] |= x;
		}

	prepare();

	while(q--) {
		int tm,x,y,p,q;
		cin >> tm >> x >> y >> p >> q;
		cout << solve(tm,x,y,p,q) << "\n";
	}

	return 0;
}