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

using namespace std;

typedef long long lld;

constexpr int MAX_N =  15000;
constexpr int MAX_M =  15000;
constexpr int MAX_NM = 1000000;
constexpr int MAX_Q = 1000000;
constexpr int S_SIZE = 9;
constexpr int S_NWW = 840;
constexpr int INF = (1<<29) - 1;

int err;
int n,m,q;
int hv[4][MAX_M+9][S_NWW][4];
vector<vector<vector<char> > > v;
vector<vector<char> > empty1;
vector<char> empty2;
char s[S_SIZE];

int get_zn(int kier) {
	if(kier&1) {
		return -1;
	}
	else {
		return 1;
	}
}

void fix(int kier, int t, int i, int jmp) {
	t %= S_NWW;
	//printf("%d - %d = %d\n", INF, hv[kier][i][t][jmp], INF - hv[kier][i][t][jmp]);
	if(hv[kier][i][t][jmp] >= INF) {
		//printf("fixing kier=%d  t=%d  i=%d  jmp=%d\n", kier, t, i, jmp);
		int mv = 1;
		for(int ii = 1; ii < jmp; ++ii) mv *= 10;
		int zn = get_zn(kier);
		int t1 = t;
		int i1 = i;
		int i2 = i + mv*10*zn;
		while(i1 != i2) {
			fix(kier, t1, i1, jmp-1);
			t1 += hv[kier][i1][t1%S_NWW][jmp-1];
			i1 += mv*zn;
		}
		hv[kier][i][t][jmp] = t1-t;
	}
}

int go(int kier, int t1, int i1, int i2) {
	if(i1 == i2) {
		return 0;
	}
	t1 %= S_NWW;
	if(abs(i2 - i1) >= 1000) {
		fix(kier, t1, i1, 3);
		return hv[kier][i1][t1][3] + go(kier, t1 + hv[kier][i1][t1][3], i1+1000*get_zn(kier), i2);
	}
	else if(abs(i2 - i1) >= 100) {
		fix(kier, t1, i1, 2);
		return hv[kier][i1][t1][2] + go(kier, t1 + hv[kier][i1][t1][2], i1+100*get_zn(kier), i2);
	}
	else if(abs(i2 - i1) >= 10) {
		//printf("going from %d to %d with in direction %d, step 10\n", i1, i2, kier);
		fix(kier, t1, i1, 1);
		//printf("hv[%d][%d][%d][1] = %d\n", kier ,i1, t1, hv[kier][i1][t1][1]);
		return hv[kier][i1][t1][1] + go(kier, t1 + hv[kier][i1][t1][1], i1+10*get_zn(kier), i2);
	}
	else {
		return hv[kier][i1][t1][0] + go(kier, t1 + hv[kier][i1][t1][0], i1+1*get_zn(kier), i2);
	}
}

int main() {
	err = scanf("%d%d%d", &n, &m, &q);
	
	for(int odl = 0; odl < 4; ++odl) {
		for(int t = 0; t < S_NWW; ++t) {
			for(int i = 0; i <= n; ++i) {
				hv[1][i][t][odl] = INF;
				hv[0][i][t][odl] = INF;
			}
			for(int j = 0; j <= m; ++j) {
				hv[3][j][t][odl] = INF;
				hv[2][j][t][odl] = INF;
			}
		}
	}
	
	for(int i = 0; i < n; ++i) {
		v.pb(empty1);
		for(int j = 0; j < m; ++j) {
			v[i].pb(empty2);
			err = scanf("%s", s);
			int nn = strlen(s);
			for(int h = 0; h < nn; ++h) {
				v[i][j].push_back(s[h] - '0');
			} 
		}
	}
	
	
	
	for(int i = 0; i < n; ++i) {
		for(size_t h = 2; h <= 8; ++h) {
			for(size_t g = 0; g < h; ++g) {
				s[g] = 1;
			}
			for(int j = 0; j < m; ++j) {
				if(v[i][j].size() == h) {
					for(size_t g = 0; g < h; ++g) {
						s[g] &= v[i][j][g];
					}
				}
			}
			
			for(int k = 0; k < S_NWW; ++k) {
				hv[0][i][k][0] &= INF*s[k%h];
				hv[1][i+1][k][0] &= INF*s[k%h];
			}
		}
	}
	
	for(int j = 0; j < m; ++j) {
		for(size_t h = 2; h <= 8; ++h) {
			for(size_t g = 0; g < h; ++g) {
				s[g] = 1;
			}
			for(int i = 0; i < n; ++i) {
				if(v[i][j].size() == h) {
					for(size_t g = 0; g < h; ++g) {
						s[g] &= v[i][j][g]^1;
					}
				}
			}
			
			for(int k = 0; k < S_NWW; ++k) {
				hv[2][j][k][0] &= INF*s[k%h];
				hv[3][j+1][k][0] &= INF*s[k%h];
			}
		}
	}
	
	for(int kier = 0; kier < 4; ++kier) {
		int nm;
		if(kier < 2) {
			nm = n;
		}
		else {
			nm = m;
		}
		for(int i = 0; i <= nm; ++i) {
			for(int k = 10; k >= 0; --k) {
				hv[kier][i][k][0] = min(hv[kier][i][k][0], hv[kier][i][k+1][0]+1);
			}
			hv[kier][i][S_NWW-1][0] = min(hv[kier][i][S_NWW-1][0], hv[kier][i][0][0]+1);
			for(int k = S_NWW-2; k >= 0; --k) {
				hv[kier][i][k][0] = min(hv[kier][i][k][0], hv[kier][i][k+1][0]+1);
			}
			
			/*
			for(int k = 0; k < 8; ++k) {
				printf("hv[%d][%d][%d][0] = %d\n", kier, i, k, hv[kier][i][k][0]);
			}
			printf("\n");
			//*/
		}
	}
	
	
	while(q--) {
		int t1,i1,j1,i2,j2;
		err = scanf("%d%d%d%d%d", &t1, &i1, &j1, &i2, &j2);
		int hor_wyn = 0;
		if(i1 < i2) {
			hor_wyn = go(0, t1, i1, i2);
		}
		else {
			hor_wyn = go(1, t1, i1, i2);
		}
		int ver_wyn = 0;
		if(j1 < j2) {
			ver_wyn = go(2, t1, j1, j2);
		} 
		else {
			ver_wyn = go(3, t1, j1, j2);
		}
		//printf("hor:%d | ver:%d\n", hor_wyn, ver_wyn);
		printf("%d\n", hor_wyn + ver_wyn + t1);
	}
}
/*
2 10 1
00010 1100001 10110101 01111100 00101 1100001 00101 00010 00101 1100001
00101 10110101 10110101 1100001 00101 1100001 1100001 01111100 01111100 00101
324777887 2 10 1 0

//*/