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
#include<bits/stdc++.h>

using namespace std;

const int BITS = 840;

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	int n, m, q; cin>>n>>m>>q;
	vector<bitset<BITS>> rowsSplit(n+1, bitset<BITS>().set()); //bit 1 oznacza ze miedzy wierszami nie ma polaczenia, liczymy jako AND
	vector<bitset<BITS>> colsConnected(m+1); //bit 1 oznacza ze jest polaczenie, liczymy ORem
	for(int i = 1; i <= n; i++){
		for(int j = 1; j <= m; j++){
			string str; cin>>str;
			reverse(str.begin(), str.end());
			bitset<BITS> bts{str};
			int k = str.length();
			while(k < BITS){
				bts |= (bts<<k);
				k *= 2;
			}
			rowsSplit[i] &= bts;
			colsConnected[j] |= bts;
		}
	}
	/*for(auto x : rowsSplit)cout<<x<<"\n";
	cout<<"\n\n";
	for(auto x : colsConnected)cout<<x<<"\n";*/
	bitset<BITS> rowsSummary;//jezeli jest 1 to znaczy ze w tej chwili jakies wiersze sa rodzielone
	for(int i = 1; i <= n; i++)rowsSummary |= rowsSplit[i];
	bitset<BITS> colsSummary = bitset<BITS>().set();//jezeli jest 0 to znaczy ze jakies kolumny sa rodzielone
	for(int j = 1; j <= m; j++)colsSummary &= colsConnected[j];
	//cout<<rowsSummary<<"\n";
	//cout<<colsSummary<<"\n";
	vector<short> type(BITS);//typ podzialu w danym momencie 0-jedna skladowa 1-podzial na wiersze 2-podzial na kolumny
	for(int i = 0; i < BITS; i++){
		if(rowsSummary[i] == 0 && colsSummary[i] == 1){
			type[i] = 0;
		}else if(rowsSummary[i] == 1){
			type[i] = 1;
		}else{
			type[i] = 2;
		}
	}
	//for(auto x : type)cout<<x;
	vector<vector<pair<short, short>>> movement(BITS);//zalezy od typu danej warstwy w czasie, first - najmniejszy w tej skladowej, second - najwiekszy (odpowiednio wiersz lub kolumna)
	for(int k = 0; k < BITS; k++){
		if(type[k] == 1){
			movement[k].resize(n+1);
			int a = 0;
			for(int i = 1; i <= n; i++){
				if(rowsSplit[i][k]){
					for(int l = a; l < i; l++){
						movement[k][l] = {a, i-1};
					}
					a = i;
				}
			}
			for(int l = a; l <= n; l++){
				movement[k][l] = {a, n};
			}
		}else if(type[k] == 2){
			movement[k].resize(m+1);
			int a = 0;
			for(int j = 1; j <= m; j++){
				if(!colsConnected[j][k]){
					for(int l = a; l < j; l++){
						movement[k][l] = {a, j-1};
					}
					a = j;
				}
			}
			for(int l = a; l <= m; l++){
				movement[k][l] = {a, m};
			}
		}//jak typ == 0 to wszystko jest polaczone i nie ma sie czym przejmowac
	}
	/*for(int k = 0; k < 20; k++){
		cout<<type[k]<<"\n";
		for(auto x : movement[k])cout<<x.first<<" "<<x.second<<"\n";
	}*/
	//teraz czas na potezna 0,5GB tablice jmpptr, jak to zadziala to bedzie cud, robimy 4 jako podstawowy skok zeby oszcedzic pamiec
	vector<vector<array<short, 10>>> big(BITS);
	vector<vector<array<short, 10>>> small(BITS);
	for(int t1 = 0; t1 < BITS; t1++){
		int t2 = (t1 + 1) % BITS;
		if(type[t1] == 1){
			big[t1].resize(n+1);
			small[t1].resize(n+1);
			if(type[t2] == 1){
				for(int i = 0; i <= n; i++){
					big[t1][i][0] = movement[t2][movement[t1][i].second].second;
					small[t1][i][0] = movement[t2][movement[t1][i].first].first;
				}
			}else{
				for(int i = 0; i <= n; i++){
					big[t1][i][0] = n;
					small[t1][i][0] = 0;
				}
			}
		}else if(type[t1] == 2){
			big[t1].resize(m+1);
			small[t1].resize(m+1);
			if(type[t2] == 2){
				for(int i = 0; i <= m; i++){
					big[t1][i][0] = movement[t2][movement[t1][i].second].second;
					small[t1][i][0] = movement[t2][movement[t1][i].first].first;
				}
			}else{
				for(int i = 0; i <= m; i++){
					big[t1][i][0] = m;
					small[t1][i][0] = 0;
				}
			}
		}
	}
	/*for(int t = 0; t < 4; t++){
		cout<<type[t]<<"\n";
		if(type[t] == 1){
			for(int i = 0; i <= n; i++){
				for(int k = 0; k < 10; k++)cout<<small[t][i][k]<<" ";
				cout<<"\n";
				for(int k = 0; k < 10; k++)cout<<big[t][i][k]<<" ";
				cout<<"\n";
			}
		}else if(type[t] == 2){
			for(int i = 0; i <= m; i++){
				cout<<i<<"\n";
				for(int k = 0; k < 10; k++)cout<<small[t][i][k]<<" ";
				cout<<"\n";
				for(int k = 0; k < 10; k++)cout<<big[t][i][k]<<" ";
				cout<<"\n";
			}
		}
		cout<<"\n\n";
	}*/
	vector<int> pows(10);
	pows[0] = 1;
	for(int k = 1; k < 10; k++){
		pows[k] = 4 * pows[k-1];
		int pow = pows[k-1];
		for(int t1 = 0; t1 < BITS; t1++){
			//cout<<t1<<"\n";
			if(type[t1] == 1){
				for(int i = 0; i <= n; i++){
					int t = t1;
					int x = small[t][i][k-1];
					int y = big[t][i][k-1];
					for(int j = 0; j < 3 && (x > 0 || y < n); j++){
						t = (t + pow)% BITS;
						x = small[t][x][k-1];
						y = big[t][y][k-1];
					}
					small[t1][i][k] = x;
					big[t1][i][k] = y;
				}
			}else if(type[t1] == 2){
				for(int i = 0; i <= m; i++){
					int t = t1;
					int x = small[t][i][k-1];
					int y = big[t][i][k-1];
					for(int j = 0; j < 3 && (x > 0 || y < m); j++){
						t = (t + pow)% BITS;
						x = small[t][x][k-1];
						y = big[t][y][k-1];
					}
					small[t1][i][k] = x;
					big[t1][i][k] = y;
				}
			}
		}
	}
	/*for(int t = 0; t < 4; t++){
		cout<<type[t]<<"\n";
		if(type[t] == 1){
			for(int i = 0; i <= n; i++){
				for(int k = 0; k < 10; k++)cout<<small[t][i][k]<<" ";
				cout<<"\n";
				for(int k = 0; k < 10; k++)cout<<big[t][i][k]<<" ";
				cout<<"\n";
			}
		}else if(type[t] == 2){
			for(int i = 0; i <= m; i++){
				for(int k = 0; k < 10; k++)cout<<small[t][i][k]<<" ";
				cout<<"\n";
				for(int k = 0; k < 10; k++)cout<<big[t][i][k]<<" ";
				cout<<"\n";
			}
		}
		cout<<"\n\n";
	}*/
	while(q--){
		int t, a, b, c, d; cin>>t>>a>>b>>c>>d;
		int T = t%BITS;
		if(type[T] == 0){
			cout<<t<<"\n";
		}else if(type[T] == 1){
			int x = movement[T][a].first;
			int y = movement[T][a].second;
			if(x <= c && c <= y){
				cout<<t<<"\n";
			}else{
				int time = 0;
				for(int k = 9; k >= 0; k--){
					while(!(small[T][x][k] <= c && c <= big[T][y][k])){
						x = small[T][x][k];
						y = big[T][y][k];
						time += pows[k];
						T = (T + pows[k]) % BITS;
					}
				}
				cout<<(t + time + 1)<<"\n";
			}
		}else if(type[T] == 2){
			int x = movement[T][b].first;
			int y = movement[T][b].second;
			if(x <= d && d <= y){
				cout<<t<<"\n";
			}else{
				int time = 0;
				for(int k = 9; k >= 0; k--){
					while(!(small[T][x][k] <= d && d<= big[T][y][k])){
						x = small[T][x][k];
						y = big[T][y][k];
						time += pows[k];
						T = (T + pows[k]) % BITS;
					}
				}
				cout<<(t + time + 1)<<"\n";
			}
		}
	}
}