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
#include <bits/stdc++.h>
#define dbg(x) " [" << #x << ": " << (x) << "] "
using namespace std;
template<typename A,typename B>
ostream& operator<<(ostream& out,const pair<A,B>& p) {
    return out << "(" << p.first << ", " << p.second << ")";
}
template<typename T>
ostream& operator<<(ostream& out,const vector<T>& c) {
    out << "{";
    for(auto it = c.begin(); it != c.end(); it++) {
        if(it != c.begin()) out << ", ";
        out << *it;
    }
    return out << "}" << endl;
}
void solve() {
    int n,m,q;
    cin >> n >> m >> q;
    vector<vector<string>> g(n,vector<string>(m));
    for(int i = 0; i < n; i++) {
        for(int j = 0; j < m; j++) {
            cin >> g[i][j];
        }
    }
    vector<int> dir(840);
    vector<vector<short>> v(840);
    vector<vector<short>> md(840,vector<short>(9));
    for(int i = 0; i < 840; i++) {
        for(int j = 2; j < 9; j++) {
            md[i][j] = i % j;
        }
    }
    for(int t = 0; t < 840; t++) {
        v[t].push_back(0);
        for(int i = 0; i < n; i++) {
            bool f = true;
            for(int j = 0; j < m; j++) {
                if(g[i][j][md[t][g[i][j].size()]] == '0') {
                    f = false;
                    break;
                }
            }
            if(f) {
                v[t].push_back(i + 1);
            }
        }
        if(v[t].size() == 1) {
            dir[t] = 1;
            for(int j = 0; j < m; j++) {
                bool f = true;
                for(int i = 0; i < n; i++) {
                    if(g[i][j][md[t][g[i][j].size()]] == '1') {
                        f = false;
                        break;
                    }
                }
                if(f) {
                    v[t].push_back(j + 1);
                }
            }
        }
    }
    bool ok = false;
    for(int i = 0; i < 840; i++) {
        if(dir[i] != dir[0]) ok = true;
    }
    const int LG = ok ? 10 : 17;
    vector<vector<vector<short>>> l(LG,vector<vector<short>>(840));
    vector<vector<vector<short>>> r(LG,vector<vector<short>>(840));
    vector<vector<bool>> ch(840,vector<bool>(LG));
    for(int lg = 0; lg < LG; lg++) {
        int pw = 1 << lg;
        int ppw = pw % 840;
        int ppw2 = 0;
        if(lg) {
            ppw2 = (1 << (lg - 1)) % 840;
        }
        for(int t = 0; t < 840; t++) {
            l[lg][t].resize(v[t].size());
            r[lg][t].resize(v[t].size());
            int tm = t + pw;
            bool f = false;
            if(lg <= 9) {
                if(tm >= 840) {
                    tm -= 840;
                    for(int i = t; i < 840; i++) {
                        if(dir[t] != dir[i]) f = true;
                    }
                    for(int i = 0; i <= tm; i++) {
                        if(dir[t] != dir[i]) f = true;
                    }
                } else {
                    for(int i = t; i <= tm; i++) {
                        if(dir[t] != dir[i]) f = true;
                    }
                }
            } else {
                for(int i = 0; i < 840; i++) {
                    if(dir[t] != dir[i]) f = true;
                }
                tm = t + ppw;
                if(tm >= 840) tm -= 840;
            }
            ch[t][lg] = f;
            if(f) continue;
            if(lg == 0) {
                for(int i = 0; i < v[t].size(); i++) {
                    l[lg][t][i] = upper_bound(v[tm].begin(),v[tm].end(),v[t][i]) - v[tm].begin() - 1;
                    if(i + 1 == v[t].size()) {
                        r[lg][t][i] = (int)v[tm].size() - 1;
                    } else {
                        int ll = 0,rr = v[tm].size();
                        while(ll + 1 < rr) {
                            int mm = (ll + rr) >> 1;
                            if(v[tm][mm] < v[t][i + 1]) {
                                ll = mm;
                            } else {
                                rr = mm;
                            }
                        }
                        r[lg][t][i] = ll;
                    }
                }
            } else {
                int pw2 = 1 << (lg - 1);
                tm = t + ppw2;
                if(tm >= 840) {
                    tm -= 840;
                }
                for(int i = 0; i < v[t].size(); i++) {
                    l[lg][t][i] = l[lg - 1][tm][l[lg - 1][t][i]];
                    r[lg][t][i] = r[lg - 1][tm][r[lg - 1][t][i]];
                }
            }
        }
    }
    vector<vector<short>> bl(840);
    for(int t = 0; t < 840; t++) {
        if(dir[t]) {
            bl[t].resize(m + 1);
            short k = 0;
            for(int i = 0; i < m + 1; i++) {
                if(k + 1 < v[t].size() && v[t][k + 1] <= i) {
                    k++;
                }
                bl[t][i] = k;
            }
        } else {
            bl[t].resize(n + 1);
            short k = 0;
            for(int i = 0; i < n + 1; i++) {
                if(k + 1 < v[t].size() && v[t][k + 1] <= i) {
                    k++;
                }
                bl[t][i] = k;
            }
        }
    }
    while(q--) {
        int t,a,b,cc,d;
        cin >> t >> a >> b >> cc >> d;
        int tt = t;
        t %= 840;
        int x = dir[t];
        if(x) {
            swap(a,b);
            swap(cc,d);
        }
        bool right = a < cc;
        a = bl[t][a];
        int ans = 1;
        if(a == bl[t][cc]) {
            cout << tt << '\n';
        } else {
            int tm = t;
            for(int i = LG - 1; i >= 0; i--) {
                if(ch[tm][i]) continue;
                int tmm = tm + (1 << i);
                tmm %= 840;
                int nxt;
                if(right) {
                    nxt = r[i][tm][a];
                    if(nxt < bl[tmm][cc]) {
                        tm = tmm;
                        a = nxt;
                        ans += 1 << i;
                    }
                } else {
                    nxt = l[i][tm][a];
                    if(nxt > bl[tmm][cc]) {
                        tm = tmm;
                        a = nxt;
                        ans += 1 << i;
                    }
                }
            }
            cout << tt + ans << '\n';
        }
    }
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int testNum = 1;
    //cin >> testNum;
    for(int i = 1; i <= testNum; i++) {
        solve();
    }
    return 0;
}