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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

/*
#pragma GCC target("avx2")
#pragma GCC optimization("Ofast")
#pragma GCC optimization("unroll-loops")
*/

using namespace __gnu_pbds;
using namespace std;

#define mp make_pair
#define eb emplace_back
#define pb push_back
#define e1 first
#define e2 second
#define vt vector
#define maxi(x,y) x=max(x,y)
#define mini(x,y) x=min(x,y)
#define size(x) (int)x.size()
#define all(r) begin(r),end(r)
#define fastio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define cx real()
#define cy imag()
#define precision(x) setprecision(x)<<fixed
#define time chrono::high_resolution_clock().now().time_since_epoch().count()
#ifndef DEBUG
#define DEBUG false
#endif
#define dbg if(DEBUG)

typedef long long ll;
typedef long double ld;
typedef complex<long double> cplx;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef vt<int> vi;
typedef vt<vi> vvi;
typedef vt<bool> vb;
typedef vt<vb> vvb;
typedef vt<char> vc;
typedef vt<vc> vvc;
typedef vt<string> vs;
typedef vt<vs> vvs;
typedef vt<ll> vll;
typedef vt<vll> vvll;
typedef vt<ld> vld;
typedef vt<vld> vvld;
typedef vt<cplx> vcplx;
typedef vt<vcplx> vvcplx;

template<typename T>
using ordset=tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>;

template<typename T1,typename T2>
ostream& operator<<(ostream &os,pair<T1,T2>& VAR){
	os<<"("<<VAR.e1<<","<<VAR.e2<<")";
	return os;
}

template<typename T>
ostream& operator<<(ostream& os,vt<T>& VAR){
	os<<"[";
	for(int i=0;i<size(VAR)-1;i++)
		os<<VAR[i]<<",";
	if(size(VAR))
		os<<VAR.back();
	os<<"]";
	return os;
}

template<typename T>
istream& operator>>(istream& is,complex<T>& VAR){
	T value;
	is>>value;
	VAR.real(value);
	is>>value;
	VAR.imag(value);
	return is;
}

struct queryStruct{
	int t,a,b,c,d,ans,tmod;
	queryStruct(){}
};

const bool TESTS=false;

const int INF=1e8+2137;

const int MAXN=15'000+10;
const int MAXQ=1000'000+10;
const int LG=14;
const int LCM=3*5*7*8;

int jump[LG][LCM][MAXN];
bitset<MAXN> vertical_cut[LCM];
bitset<MAXN> horizontal_cut[LCM];
bitset<LCM> horizontal;
bitset<LCM> vertical;
int next_vertical[LCM];
int next_horizontal[LCM];
int next_vertical_open[LCM][MAXN];
int next_horizontal_open[LCM][MAXN];

queryStruct query[MAXQ];

bitset<LG> strval;

void solve(){
	int n,m,q;
	cin>>n>>m>>q;
	for(int i=0;i<LCM;i++){
		horizontal_cut[i].set();
		vertical_cut[i].set();
	}
	for(int i=1;i<=n;i++)
		for(int j=1;j<=m;j++){
			string s;
			cin>>s;
			for(int k=0;k<size(s);k++)
				if(s[k]=='1')
					strval[k]=true;
				else
					strval[k]=false;
			int strpos=0;
			for(int k=0;k<LCM;k++){
				if(strval[strpos])
					vertical_cut[k][j]=false;
				else
					horizontal_cut[k][i]=false;
				strpos++;
				if(strpos==size(s))
					strpos=0;
			}
		}
	for(int i=0;i<LCM;i++){
		int horizontal_count=0;
		int vertical_count=0;
		for(int j=1;j<=m;j++)
			if(vertical_cut[i][j])
				vertical_count++;
		for(int j=1;j<=n;j++)
			if(horizontal_cut[i][j])
				horizontal_count++;
		if(horizontal_count==0)
			vertical[i]=true;
		if(vertical_count==0)
			horizontal[i]=true;
	}
	for(int i=0;i<LCM;i++){
		next_horizontal[i]=next_vertical[i]=INF;
		for(int j=LCM-1;j>=0;j--){
			if(vertical[(i+j)%LCM])
				next_vertical[i]=j;
			if(horizontal[(i+j)%LCM])
				next_horizontal[i]=j;
		}
		for(int j=1;j<=m;j++){
			next_vertical_open[i][j]=INF;
			for(int k=8-1;k>=0;k--)
				if(!vertical_cut[(i+k)%LCM][j])
					next_vertical_open[i][j]=k;
		}
		for(int j=1;j<=n;j++){
			next_horizontal_open[i][j]=INF;
			for(int k=8-1;k>=0;k--)
				if(!horizontal_cut[(i+k)%LCM][j])
					next_horizontal_open[i][j]=k;
		}
	}
	for(int i=0;i<q;i++){
		cin>>query[i].t>>query[i].a>>query[i].b>>query[i].c>>query[i].d;
		query[i].ans=INF;
		query[i].tmod=query[i].t%LCM;
		int using_both=max(next_vertical[query[i].tmod],next_horizontal[query[i].tmod]);
		if(using_both<query[i].ans)
			query[i].ans=using_both;
		if((query[i].a==query[i].c)&&(query[i].b==query[i].d))
			query[i].ans=0;
	}
	// left  -> right
	for(int i=0;i<LCM;i++)
		for(int j=0;j<m;j++)
			jump[0][i][j]=next_vertical_open[i][j+1];
	for(int b=1;b<LG;b++)
		for(int i=0;i<LCM;i++)
			for(int j=0;j+(1<<b)<=m;j++){
				int czas=jump[b-1][i][j];
				czas+=jump[b-1][(i+czas)%LCM][j+(1<<(b-1))];
				jump[b][i][j]=czas;
			}
	for(int i=0;i<q;i++){
		if(!vertical[query[i].tmod])
			continue;
		if(!(query[i].b<=query[i].d))
			continue;
		int total_time=0;
		int cur_time=query[i].tmod;
		int position=query[i].b;
		for(int j=LG-1;j>=0;j--){
			if(!(position+(1<<j)<=query[i].d))
				continue;
			int new_time=jump[j][cur_time][position];
			position+=(1<<j);
			total_time+=new_time;
			cur_time=(cur_time+new_time)%LCM;
		}
		mini(query[i].ans,total_time);
	}
	// right -> left
	for(int i=0;i<LCM;i++)
		for(int j=m;j>0;j--)
			jump[0][i][j]=next_vertical_open[i][j];
	for(int b=1;b<LG;b++)
		for(int i=0;i<LCM;i++)
			for(int j=m;j-(1<<b)>=0;j--){
				int czas=jump[b-1][i][j];
				czas+=jump[b-1][(i+czas)%LCM][j-(1<<(b-1))];
				jump[b][i][j]=czas;
			}
	for(int i=0;i<q;i++){
		if(!vertical[query[i].tmod])
			continue;
		if(!(query[i].b>=query[i].d))
			continue;
		int total_time=0;
		int cur_time=query[i].tmod;
		int position=query[i].b;
		for(int j=LG-1;j>=0;j--){
			if(!(position-(1<<j)>=query[i].d))
				continue;
			int new_time=jump[j][cur_time][position];
			position-=(1<<j);
			total_time+=new_time;
			cur_time=(cur_time+new_time)%LCM;
		}
		mini(query[i].ans,total_time);
	}
	// up    -> down
	for(int i=0;i<LCM;i++)
		for(int j=0;j<n;j++)
			jump[0][i][j]=next_horizontal_open[i][j+1];
	for(int b=1;b<LG;b++)
		for(int i=0;i<LCM;i++)
			for(int j=0;j+(1<<b)<=n;j++){
				int czas=jump[b-1][i][j];
				czas+=jump[b-1][(i+czas)%LCM][j+(1<<(b-1))];
				jump[b][i][j]=czas;
			}
	for(int i=0;i<q;i++){
		if(!horizontal[query[i].tmod])
			continue;
		if(!(query[i].a<=query[i].c))
			continue;
		int total_time=0;
		int cur_time=query[i].tmod;
		int position=query[i].a;
		for(int j=LG-1;j>=0;j--){
			if(!(position+(1<<j)<=query[i].c))
				continue;
			int new_time=jump[j][cur_time][position];
			position+=(1<<j);
			total_time+=new_time;
			cur_time=(cur_time+new_time)%LCM;
		}
		mini(query[i].ans,total_time);
	}
	// down  -> up
	for(int i=0;i<LCM;i++)
		for(int j=n;j>0;j--)
			jump[0][i][j]=next_horizontal_open[i][j];
	for(int b=1;b<LG;b++)
		for(int i=0;i<LCM;i++)
			for(int j=n;j-(1<<b)>=0;j--){
				int czas=jump[b-1][i][j];
				czas+=jump[b-1][(i+czas)%LCM][j-(1<<(b-1))];
				jump[b][i][j]=czas;
			}
	for(int i=0;i<q;i++){
		if(!horizontal[query[i].tmod])
			continue;
		if(!(query[i].a>=query[i].c))
			continue;
		int total_time=0;
		int cur_time=query[i].tmod;
		int position=query[i].a;
		for(int j=LG-1;j>=0;j--){
			if(!(position-(1<<j)>=query[i].c))
				continue;
			int new_time=jump[j][cur_time][position];
			position-=(1<<j);
			total_time+=new_time;
			cur_time=(cur_time+new_time)%LCM;
		}
		mini(query[i].ans,total_time);
	}
	for(int i=0;i<q;i++)
		cout<<query[i].t+query[i].ans<<'\n';
}

int32_t main(){
	fastio;
	if(TESTS){
		int TEST_COUNT;
		cin>>TEST_COUNT;
		for(int i=0;i<TEST_COUNT;i++)
			solve();
	}
	else
		solve();
	return 0;
}