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
#include <bits/stdc++.h>
using namespace std;
void imax(int &a, int b){
	a=max(a, b);
}
void imin(int &a, int b){
	a=min(a, b);
}
void lmax(long long &a, long long b){
	a=max(a, b);
}
void lmin(long long &a, long long b){
	a=min(a, b);
}
/*
	WARNING: I'm using strange bracket style!
*/
#define penta pair <int, pair <pair <int, int>, pair <int, int> > >
#define tt first
#define aa second.first.first
#define bb second.first.second
#define cc second.second.first
#define dd second.second.second
#define xx first
#define yy second
const int SIZE=15100;
const int LCM=840;
const int LOG=16;
vector <pair <int, int> > blocks[LCM];
int n, m, q, Lcm, a, b, c, d, t;
bool blocked[SIZE][LCM][2];
int p[SIZE][LCM+10][LOG];
vector <string> S[SIZE];
vector <penta> Q;
int ans[SIZE*80];
string s;
int jumpUpClassic(int x, int y, int time){
	int d=y-x, newX, newTime;
	for (int i=0; i<LOG; i++)
		if (d&(1<<i))
			newX=x+(1<<i), newTime=time+p[x][time%Lcm][i], x=newX, time=newTime;
	return time;
}
int jumpUpReverse(int x, int y, int time){
	int d=y-x, newY, newTime;
	for (int i=0; i<LOG; i++)
		if (d&(1<<i))
			newY=y-(1<<i), newTime=time+p[y][time%Lcm][i], y=newY, time=newTime;
	return time;
}
int main()
	{
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin>>n>>m>>q, Lcm=1;
	for (int i=1; i<=n; i++)
		{
		S[i].push_back("");
		for (int j=1; j<=m; j++)
			cin>>s, S[i].push_back(s), Lcm=Lcm*s.size()/__gcd((int)s.size(), Lcm);
		}
	for (int i=1; i<=q; i++)
		cin>>t>>a>>b>>c>>d, Q.push_back({t, {{a, b}, {c, d}}}), ans[i-1]=t;
	for (int time=0; time<Lcm; time++)
		for (int i=1; i<=n; i++)
			{
			bool B=true;
			for (int j=1; j<=m; j++)
				B&=(S[i][j][time%S[i][j].size()]=='1');
			if (B) blocks[time].push_back({i, 1});
			}
	for (int time=0; time<Lcm; time++)
		for (int j=1; j<=m; j++)
			{
			bool B=true;
			for (int i=1; i<=n; i++)
				B&=(S[i][j][time%S[i][j].size()]=='0');
			if (B) blocks[time].push_back({j, 0});
			}
	for (int i=0; i<Lcm; i++)
		for (auto j: blocks[i])
			blocked[j.xx][i][j.yy]=1;

	for (int time=0; time<Lcm; time++)
		for (int i=0; i<n; i++)
			{
			int t=0;
			while (blocked[i+1][(time+t)%Lcm][1]) (t++);
			p[i][time][0]=t;
			}
	for (int j=1; j<LOG; j++)
		for (int i=0; i<n; i++)
			for (int time=0; time<Lcm; time++)
				if (i+(1<<(j-1))<=n)
					p[i][time][j]=p[i][time][j-1]+p[i+(1<<(j-1))][(time+p[i][time][j-1])%Lcm][j-1];
	for (int i=0; i<q; i++)
		if (Q[i].aa<=Q[i].cc)
			imax(ans[i], jumpUpClassic(Q[i].aa, Q[i].cc, Q[i].tt));

	for (int time=0; time<Lcm; time++)
		for (int i=0; i<m; i++)
			{
			int t=0;
			while (blocked[i+1][(time+t)%Lcm][0]) (t++);
			p[i][time][0]=t;
			}
	for (int j=1; j<LOG; j++)
		for (int i=0; i<m; i++)
			for (int time=0; time<Lcm; time++)
				if (i+(1<<(j-1))<=m)
					p[i][time][j]=p[i][time][j-1]+p[i+(1<<(j-1))][(time+p[i][time][j-1])%Lcm][j-1];
	for (int i=0; i<q; i++)
		if (Q[i].bb<=Q[i].dd)
			imax(ans[i], jumpUpClassic(Q[i].bb, Q[i].dd, Q[i].tt));

	for (int time=0; time<Lcm; time++)
		for (int i=n; i>0; i--)
			{
			int t=0;
			while (blocked[i][(time+t)%Lcm][1]) (t++);
			p[i][time][0]=t;
			}
	for (int j=1; j<LOG; j++)
		for (int i=n; i>0; i--)
			for (int time=0; time<Lcm; time++)
				if (i-(1<<(j-1))>=0)
					p[i][time][j]=p[i][time][j-1]+p[i-(1<<(j-1))][(time+p[i][time][j-1])%Lcm][j-1];
	for (int i=0; i<q; i++)
		if (Q[i].aa>=Q[i].cc)
			imax(ans[i], jumpUpReverse(Q[i].cc, Q[i].aa, Q[i].tt));

	for (int time=0; time<Lcm; time++)
		for (int i=m; i>0; i--)
			{
			int t=0;
			while (blocked[i][(time+t)%Lcm][0]) (t++);
			p[i][time][0]=t;
			}
	for (int j=1; j<LOG; j++)
		for (int i=m; i>0; i--)
			for (int time=0; time<Lcm; time++)
				if (i-(1<<(j-1))>=0)
					p[i][time][j]=p[i][time][j-1]+p[i-(1<<(j-1))][(time+p[i][time][j-1])%Lcm][j-1];
	for (int i=0; i<q; i++)
		if (Q[i].bb>=Q[i].dd)
			imax(ans[i], jumpUpReverse(Q[i].dd, Q[i].bb, Q[i].tt));
	
	for (int i=0; i<q; i++)
		cout<<ans[i]<<"\n";
	return 0;
	}