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

const int MAX_N = 2e4;
const int COMBI = 8 * 3 * 5 * 7;
const int MAX_WORD = 8;
const int INF_TIME = MAX_N * MAX_WORD * 10;

int col_mask[MAX_N][MAX_WORD + 1];
int row_mask[MAX_N][MAX_WORD + 1];

bool passable[2][MAX_N][COMBI];
bool is_stripes[2][COMBI];
int wait[2][MAX_N][COMBI];
int wait_tree[2][2][3 * MAX_N][COMBI];
int tree_size[2];

bool is_cross[COMBI];
int time_to_cross[COMBI];
bool at_least_one = false;

int n, m;
int q;

void load_board();
void prepare();
void serve_queries();

int main() {
	ios_base::sync_with_stdio(0); cin.tie(0);
	
	load_board();
	prepare();
	
	//cerr << passable[0][1][1] << " " << passable[0][2][1] << " " << passable[0][2][2] << endl;
	//cerr << wait[0][1][1] << " " << wait[0][2][2] << endl;
	//cerr << wait[0][1][4] << " " << wait[0][1][5] << " " << wait[0][1][6]<< " " << wait[0][1][7] << endl;
	//cerr << wait_tree[0][1][1][5] << " " << wait_tree[0][1][2][5] << " " << wait_tree[0][1][3][5] << endl;
	
	//for (int i = 0; i < 4; i++)
		//cerr << is_stripes[0][i] << " "; 
	//cerr << endl;
	
	//for (int i = 0; i < 4; i++)
		//cerr << is_stripes[1][i] << " "; 
	//cerr << endl;
	
	serve_queries();
	
	return 0;
}

int get_time(int to_right, int a, int b, int t, int wait_tree[][COMBI], int i, int p, int q) {
	//cerr << a << " " << b << " " << i << " " << p << " " << q << endl;
	if (a+1 <= p && q <= b) {
		//cerr << p << " " << q << " " << wait_tree[i][t] << endl;
		return wait_tree[i][t];
	}
	
	int s = (p + q) / 2;
	
	int t1, t2;
	
	int st_i, st_left, st_right;
	int nd_i, nd_left, nd_right;
	
	st_i = 2 * i;
	st_left = p;
	st_right = s;
	
	nd_i = 2 * i + 1;
	nd_left = s + 1;
	nd_right = q;
	
	if (!to_right) {
		swap(st_i, nd_i);
		swap(st_left, nd_left);
		swap(st_right, nd_right);
	}
	
	//cerr << a << " " << b << " " << p << " "  << q << endl;
	
	if (st_left <= b && st_right > a)
		t1 = get_time(to_right, a, b, t, wait_tree, st_i, st_left, st_right);
	else 
		t1 = 0;
	
	if (t1 > time_to_cross[t]) return time_to_cross[t]; // no need to check further, this is suboptimal
	
	if (nd_left <= b && nd_right > a)
		t2 = get_time(to_right, a, b, (t + t1) % COMBI, wait_tree, nd_i, nd_left, nd_right);
	else
		t2 = 0;
	
	return t1 + t2;
}

void serve_queries() {
	while(q--) {
		int t, a, b, c, d;
		cin >> t >> a >> b >> c >> d;
		int orig_t = t;
		
		t %= COMBI;
				
		int t1, t2, result;
		
		result = time_to_cross[t];
		
		t1 = get_time(a < c, min(a, c), max(a, c), t, wait_tree[0][(a < c ? 0 : 1)], 1, 1, tree_size[0]);
		t2 = get_time(b < d, min(b, d), max(b, d), t, wait_tree[1][(b < d ? 0 : 1)], 1, 1, tree_size[1]);
		
		result = min(result, max(t1, t2));
		cout << orig_t + result << endl;
		//cerr << " " << t1 << " " << t2 << " " << time_to_cross[t] << endl; 
	}
}

void get_stripes(int n, bool passable[][COMBI], bool is_stripes[]) {
	for (int i = 0; i < COMBI; i++) {
		for (int j = 1; j <= n; j++) if(!passable[j][i]) {
			is_stripes[i] = true;
		}
	}
}

void get_crossings() {
	get_stripes(n, passable[0], is_stripes[0]);
	get_stripes(m, passable[1], is_stripes[1]);
	
	for (int i = 0; i < COMBI; i++) {
		int nxt = (i + 1) % COMBI;
		if (is_stripes[0][i] && is_stripes[1][nxt])
			is_cross[nxt] = true;
		if (is_stripes[1][i] && is_stripes[0][nxt])
			is_cross[nxt] = true;
		if (is_cross[nxt]) at_least_one = true;
	}
	
	if (at_least_one) {
		int i = 0; while(!is_cross[i]) i++;
		time_to_cross[COMBI - 1] = i + 1;
		
		for (int i = COMBI - 2; i >= 0; i--) {
			time_to_cross[i] = is_cross[i + 1] ? 1 : time_to_cross[i + 1] + 1;
		}
	} else {
		for (int i = 0; i < COMBI; i++)
			time_to_cross[i] = INF_TIME;
	}
}

void get_passable(int n, int masks[][MAX_WORD + 1], bool can_pass[][COMBI]) {
	for (int i = 1; i <= n; i++) {
		for (int j = 0; j < COMBI; j++) {
			for (int k = 2; k <= 8; k++) {
				 if (masks[i][k] & (1 << (j % k))) {
					 can_pass[i][j] = true;
					 break;
				 }
			}
		}
	}
}

void get_wait_times(int n, bool can_pass[][COMBI], int wait_time[][COMBI]) {
	for (int i = 1; i <= n; i++) {
		int j = 0; while(!can_pass[i][j]) j++;
		wait_time[i][COMBI - 1] = can_pass[i][COMBI - 1] ? 0 : j + 1;
		
		for (int j = COMBI - 2; j >= 0; j--) {
			wait_time[i][j] = (can_pass[i][j] ? 0 : wait_time[i][j + 1] + 1);
		}
	}
}

void set_wait_tree(int n, int wait_time[][COMBI], int wait_tree[][3 * MAX_N][COMBI], int &k) {
	k = 1; while(k < n) k <<= 1;
	
	for (int i = 1; i <= n; i++) 
		for (int j = 0; j < COMBI; j++) 
			wait_tree[0][k+i-1][j] = wait_tree[1][k+i-1][j] = wait_time[i][j];
	
	for (int i = k - 1; i > 0; i--) {
		for (int j = 0; j < COMBI; j++) {
			int t1, t2;
			
			t1 = wait_tree[0][2 * i][j];
			t2 = wait_tree[0][2 * i + 1][(j + t1) % COMBI];
			wait_tree[0][i][j] = t1 + t2;
			
			t1 = wait_tree[1][2 * i + 1][j];
			t2 = wait_tree[1][2 * i][(j + t1) % COMBI];
			wait_tree[1][i][j] = t1 + t2;
		}
	}
}

void prepare() {
	get_passable(n, row_mask, passable[0]);
	get_passable(m, col_mask, passable[1]);
	
	get_wait_times(n, passable[0], wait[0]);
	get_wait_times(m, passable[1], wait[1]);
	
	get_crossings();
	
	set_wait_tree(n, wait[0], wait_tree[0], tree_size[0]);
	//cerr << wait_tree[0][1][1][5] << " " << wait_tree[0][1][2][5] << " " << wait_tree[0][1][3][5] << endl;
	set_wait_tree(m, wait[1], wait_tree[1], tree_size[1]);	
}

int word_to_mask(string w) {
	int mask = 0;
	
	for (uint i = 0; i < w.size(); i++) 
		mask += ((w[i] == '1') << i);
	
	return mask;
}

int anti_mask(string w) {
	return ((1 << w.size()) - 1) ^ word_to_mask(w);
}

void load_board() {
	cin >> n >> m >> q;
	
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= m; j++) {
			string s; cin >> s;
			
			int mask = word_to_mask(s);
			int anti = anti_mask(s);
			
			col_mask[j][s.size()] |= mask;
			row_mask[i][s.size()] |= anti;
		}
	}
}