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
// clang-format off
#include <bits/stdc++.h>

using namespace std;

#define ALL(x) (x).begin(), (x).end()

template<class C> void mini(C &a5, C b5) { a5 = min(a5, b5); }
template<class C> void maxi(C &a5, C b5) { a5 = max(a5, b5); }

#ifdef LOCAL
const bool debug = true;
#else
const bool debug = false;
#define cerr if (true) {} else cout
#endif
// clang-format on

int nax; // = 200; //15010;
int d_len; // = 3;
int d_count; // = nax / d_len + 1;

const int mask_len = 840;
using mask_type = bitset<mask_len>;

struct Algo {
	vector<vector<int> > wait; // wait[n][mask_len]
	vector<vector<vector<int> > > dist; // dist[d_count][d_count][mask_len]
	int n;
	bool flipped;
	
	Algo(const vector<mask_type> &masks, bool _flipped) {
		flipped = _flipped;
		n = masks.size();
		wait.resize(n, vector<int>(mask_len));
		dist.resize(d_count, vector<vector<int> >(d_count, vector<int>(mask_len)));
		
		// init wait
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < mask_len; j++) {
				int k = j, ile = 0;
				while (!masks[i][k]) {
					k = (k + 1) % mask_len;
					ile++;
				}
				
				wait[i][j] = ile;
			}
		}
		
		// init dist
		for (int i = 1; i < d_count; i++) {
			for (int t = 0; t < mask_len; t++) {
				dist[i - 1][i][t] = get_dist_lazy(
					d_len * (i - 1),
					d_len * i,
					t
				);
			}
		}
		for (int d = 2; d < d_count; d++) {
			for (int i = 0; i + d < d_count; i++) {
				for (int t = 0; t < mask_len; t++) {
					int j = i + d;
					int dist_1 = dist[i][i + 1][t];
					int t2 = (t + dist_1) % mask_len;
					
					dist[i][j][t] = dist_1 + dist[i + 1][j][t2];
				}
			}
		}
	}
	
	int get_dist_lazy(int l, int r, int t) {
		if (r > n) return 0;
		int res = 0;
		while (l < r) {
			res += wait[l][t];
			t = (t + wait[l][t]) % mask_len;
			l++;
		}
		return res;
	}
	
	int get_dist(int l, int r, int t) {
		if (flipped) {
			l = n - l;
			r = n - r;
		}
		
		int l2 = (l + d_len - 1) / d_len;
		int r2 = r / d_len;
		
		if (l2 > r2) {
			return get_dist_lazy(l, r, t);
		}
		
		int dist_1 = get_dist_lazy(l, l2 * d_len, t);
		int t2 = (t + dist_1) % mask_len;
		
		int dist_2 = dist[l2][r2][t2];
		int t3 = (t2 + dist_2) % mask_len;
		
		int dist_3 = get_dist_lazy(r2 * d_len, r, t3);
		
		return dist_1 + dist_2 + dist_3;
	}
};

vector<mask_type> pion, poziom;

int32_t main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    
    int n, m, q;
    cin >> n >> m >> q;
    
    nax = max(n, m) + 10;
    d_len = sqrt(nax);
    if (q < 1e5) {
		d_len *= 100 * 1000 / q;
	}
    maxi(d_len, 2);
    d_count = nax / d_len + 1;
    
    poziom.resize(n);
    pion.resize(m);
    
    for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			string s, s2="";
			cin >> s;
			reverse(ALL(s));
			for (int k = 0; k < mask_len / (int)s.size(); k++) {
				s2 += s;
			}
			
			mask_type b(s2);
			pion[j] |= b;
			poziom[i] |= b.flip();
		}
	}
	
	Algo right(pion, false), down(poziom, false);
	reverse(ALL(pion)); reverse(ALL(poziom));
	Algo left(pion, true), up(poziom, true);
	
	while (q--) {
		int t2, a, b, c, d;
		cin >> t2 >> a >> b >> c >> d;
		
		int t = t2 % mask_len;
		int res = 0;
		
		if (a < c) {
			maxi(res, down.get_dist(a, c, t));
		} else {
			maxi(res, up.get_dist(a, c, t));
		}
		
		if (b < d) {
			maxi(res, right.get_dist(b, d, t));
		} else {
			maxi(res, left.get_dist(b, d, t));
		}
		
		cout << t2 + res << "\n";
	}
    
    return 0;
}/*
2 2 7
01 1100
001 10
0 0 0 2 2
1 0 1 2 1
5 2 1 0 0
0 0 2 2 2
15 1 1 0 0
16 1 1 0 0
7 2 2 2 2
*/