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
#include <iostream>
#include <string>

using namespace std;

const int MAX_N = 15000, MAX_M = 15000, NWW=840, MAX_S_SIZE=8, POT_DWA= 16384;

int n, m, ku, sa, sl, tmpi, l, p, wyn, sa2;
string sb;
int heh;

int orr[MAX_S_SIZE+1][MAX_N], ord[MAX_S_SIZE+1][MAX_M];//or z sekwencji skrzyzowan odpowiednio po wierszach i po kolumnach
bool onr[NWW][MAX_N], ond[NWW][MAX_M];
int ttd[NWW][POT_DWA*2+1], ttu[NWW][POT_DWA*2+1], ttr[NWW][POT_DWA*2+1], ttl[NWW][POT_DWA * 2];
//minimalny czas potrzebny do przemieszczenia sie w danym kierunku startujac w minucie (t%NWW)(seria drzew niemalze sum przedzialowych)

int drogaud(int t, int a, int b)
{
	if (a == b)return 0;
	t %= NWW;
	int ts;
	if (a < b)
	{
		ts = ttd[t][a];
		//if(heh==64)cout << ":td:" << a << " " << b << " " << ts << ".....\n";
		if (a + 1 == b)return ts;
		if (1 == a % 2)return ts + drogaud(t + ts, a + 1, b);
		if (0 == b % 2)
		{
			ts = drogaud(t, a, b - 1);
			return ts + drogaud(t + ts, b - 1, b);
		}
		return drogaud(t, a / 2, b / 2);
	}
	else
	{
		ts = ttu[t][a-1];
		//if(heh==64)cout << ":tu:" << a << " " << b << " " << ts << ".....\n";
		if (b + 1 == a)return ts;
		if (0 == a % 2)return ts + drogaud(t + ts, a - 1, b);
		if (1 == b % 2)
		{
			ts = drogaud(t, a, b + 1);
			return ts + drogaud(t + ts, b + 1, b);
		}
		return drogaud(t, a / 2, b / 2);
	}
}

int drogalr(int t, int a, int b)
{
	if (a == b)return 0;
	t %= NWW;
	int ts;
	if (a < b)
	{
		ts = ttr[t][a];
		//if(heh==64)cout << ":tr:" << a << " " << b << " " << ts << ".....\n";
		if (a + 1 == b)return ts;
		if (1 == a % 2)return ts + drogalr(t + ts, a + 1, b);
		if (0 == b % 2)
		{
			ts = drogalr(t, a, b - 1);
			return ts + drogalr(t + ts, b - 1, b);
		}
		return drogalr(t, a / 2, b / 2);
	}
	else
	{
		ts = ttl[t][a-1];
		//if(heh==64)cout << ":tl:" << a << " " << b << " " << ts << ".....\n";
		if (b + 1 == a)return ts;
		if (0 == a % 2)return ts + drogalr(t + ts, a - 1, b);
		if (1 == b % 2)
		{
			ts = drogalr(t, a, b + 1);
			return ts + drogalr(t + ts, b + 1, b);
		}
		return drogalr(t, a / 2, b / 2);
	}

}

int main()
{
	ios_base::sync_with_stdio(0);
	int a, b, c, d, t;
	for (int i = 0; MAX_S_SIZE > i; i++)//wyzerowanie tablic orr,ond
	{
		for (int j = 0; MAX_N > j; j++)
		{
			orr[i][j] = 0;
		}
		for (int j = 0; MAX_M > j; j++)
		{
			ord[i][j] = 0;
		}
	}
	for (int i = 0; i < NWW; i++)//wyfalsowanie tblic onr,ond;
	{
		for (int j = 0; j < MAX_N; j++)
		{
			onr[i][j] = false;
		}
		for (int j = 0; j < MAX_M; j++)
		{
			ond[i][j] = false;
		}
	}
	cin >> n>> m>> ku;
	for (int i = 0; i < n; i++)//wczytanie cykli skrzyrzowan i z-or-owanie ich z wyrazami tej samej dlugosci w kolumnie i rzedzie
	{
		for (int j = 0; j < m; j++)
		{
			cin >> sb;
			sa = 0;
			sa2 = 0;
			sl = sb.length();
			for(int k = 0; k < sl; k++)
			{
				sa *= 2;
				sa2 *= 2;
				if ('1' == sb[k])sa++;
				else sa2++;
			}
			orr[sl][i] = (orr[sl][i] | sa2);
			//cout << sa << " ";//sprawdzenie konwersji stringa na liczbe;
			ord[sl][j] = (ord[sl][j] | sa);
		}
	}
	for (int i = 0; i < n; i++)//z-or-owanie z onr
	{
		for (int j = 2; j <= MAX_S_SIZE; j++)
		{
			tmpi = orr[j][i];
			for (int k = j - 1; k >= 0; k--)
			{
				if (1 == tmpi % 2)
				{
					for (int l = k; l < NWW; l += j)
					{
						onr[l][i] = true;
					}
				}
				tmpi /= 2;
			}
		}
	}
	for (int i = 0; i < m; i++)//z-or-owanie ond
	{
		for (int j = 2; j <= MAX_S_SIZE; j++)
		{
			tmpi = ord[j][i];
			for (int k = j - 1; k >= 0; k--)
			{
				if (1 == tmpi % 2)
				{
					for (int l = k; l < NWW; l += j)
					{
						ond[l][i] = true;
					}
				}
				tmpi /= 2;
			}
		}
	}
	//for (int i = 0; i < 40; i++)
	//{
	//	if (onr[i][0] == true)cout << "1";
	//	else cout << "0";
	//}
	for (int i = 0; i < NWW; i++)
	{
		for (int j = 0; j < m; j++)//wypelnienie lisci kierujac sie w prawo lub w lewo
		{
			for (int k = i;; k = (k + 1) % NWW)
			{
				if (true == ond[k][j])
				{
					ttr[i][POT_DWA + j] = (NWW+k - i)%NWW;
					ttl[i][POT_DWA + j] = (NWW+k - i)%NWW;
					break;
				}
			}
		}
		for (int j = 0; j < n; j++)//wypelnienie lisci kierujac sie w gore lub dol
		{
			for (int k = i;; k = (k + 1) % NWW)
			{
				if (true == onr[k][j])
				{
					ttu[i][POT_DWA + j] = (NWW+k - i)%NWW;
					ttd[i][POT_DWA + j] = (NWW+k - i)%NWW;
					break;
				}
			}
		}
	}
	/*
	for (int i = 0; i <= 14; i++)
	{
		cout << ttr[i][POT_DWA];
	}
	*/
	l = POT_DWA / 2;
	p = (POT_DWA + m - 1) / 2;
	if (1 != m)
	{
		for (;;l/=2,p=(p-1)/2)
		{
			for (int i = l; i <= p; i++)
			{
				for (int j = 0; j < NWW; j++)
				{
					ttr[j][i] = ttr[j][i * 2] + ttr[(j + ttr[j][i * 2]) % NWW][i * 2 + 1];
					ttl[j][i] = ttl[(j + ttl[j][i * 2 + 1]) % NWW][i * 2] + ttl[j][i * 2 + 1];
				}
			}
			if (l == p)break;
		}
	}
	l = POT_DWA / 2;
	p = (POT_DWA + n - 1) / 2;
	if (1 != n)
	{
		for (;; l /= 2, p = (p - 1) / 2)
		{
			for (int i = l; i <= p; i++)
			{
				for (int j = 0; j < NWW; j++)
				{
					ttd[j][i] = ttd[j][2 * i] + ttd[(j + ttd[j][2 * i]) % NWW][2 * i + 1];
					ttu[j][i] = ttu[(j + ttu[j][2 * i + 1]) % NWW][2 * i] + ttu[j][2 * i + 1];
				}
			}
			if (l == p)break;
		}
	}
	//for (int i = 0; i <= 40;i++)cout << ttl[i][8194] << " ";
	for (heh = 0; heh < ku; heh++)
	{
		cin >> t >> a >> b >> c >> d;
		wyn = t + drogaud(t, a+POT_DWA, c+POT_DWA) + drogalr(t, b+POT_DWA, d+POT_DWA);
		cout << wyn << "\n";
	}
}