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
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int n,m,q;

struct mappartition {
    bool horizontal;
    int prevbound;
    int prev_bound[15009];
    int next_bound[15009];
    void init(bool is_horizontal) {
        horizontal = is_horizontal;
        prevbound = -1;
        int mymax = horizontal ? m : n;
        for(int i=0; i<=mymax; i++) {
            prev_bound[i] = -1;
            next_bound[i] = mymax;
        }
    }
    void add_bound(int idx) {
        for(int i=prevbound+1; i<=idx; i++) {
            prev_bound[i] = prevbound;
            next_bound[i] = idx;
        }
        prevbound = idx;
    }
    void finish() {
        int mymax = horizontal ? m : n;
        for(int i=prevbound+1; i<=mymax; i++) {
            prev_bound[i] = prevbound;
        }
    }
    int travel(int from, int to) {
        if(to > from) {
            return min(to, next_bound[from]);
        } else {
            return max(to, prev_bound[from]+1);
        }
    }
};

string lights[1000000]; // skrzyżowanie (i,j) -> lights[i*m+j]
bool passable[2][9][8][15000]; // is wall horizontal, mod, value, wall
mappartition p[840];
int cached_dist[2][840][159][159]; // is wall horizontal, mod, from/100, to/100

int distance_between(bool horizontal, int mod, int from, int to, bool check_cache) {
    int out = 0;

    if(check_cache){
        if(from/100 == to/100) return distance_between(horizontal,mod,from,to,false);
        int mid1, mid2;
        if(to > from) {
            mid1 = (from%100 != 0 ? (from+100)/100*100 : from);
            mid2 = to/100*100;
        } else {
            mid1 = from/100*100;
            mid2 = (to%100 != 0 ? (to+100)/100*100 : to);
        }
        int new_mod = mod;

        out += distance_between(horizontal, new_mod, from, mid1, false);
        new_mod = (mod+out)%840;
        out += cached_dist[horizontal][new_mod][mid1/100][mid2/100];
        if(cached_dist[horizontal][new_mod][mid1/100][mid2/100] == -1) cout << mid1 << " " << mid2 << endl;
        new_mod = (mod+out)%840;
        out += distance_between(horizontal, new_mod, mid2, to, false);
        return out;
    }

    while(true) {
        if(p[mod].horizontal != horizontal) {
            from = to;
        } else {
            from = p[mod].travel(from,to);
        }
        if(from == to) return out;
        else {
            mod = (mod+1)%840;
            out++;
        }
    }
}

void compute_cached() {
    for(int i=0; i<2; i++) {
        for(int j=0; j<840; j++) {
            for(int k=0; k<=150; k++) {
                for(int l=0; l<=150; l++) {
                    cached_dist[i][j][k][l] = -1;
                }
            }
        }
    }
    for(int hor=0; hor<2; hor++) {
        int mymax = hor ? m : n;
        for(int i=0; i<=mymax; i+=100) {
            for(int mod=0; mod<840; mod++) {
                cached_dist[hor][mod][i/100][i/100] = 0;
            }
        }
    }
    for(int hor=0; hor<2; hor++) {
        int mymax = hor ? m : n;
        for(int len=100; len<=mymax; len+=100) {
            for(int i=0; i+len<=mymax; i+=100) {
                for(int mod=0; mod<840; mod++) {
                    if(len == 100) cached_dist[hor][mod][i/100][(i+len)/100] = distance_between(hor,mod,i,i+len,false);
                    else {
                        int small_dist = cached_dist[hor][mod][i/100][i/100+1];
                        int new_mod = (mod+small_dist)%840;
                        cached_dist[hor][mod][i/100][(i+len)/100] = small_dist + cached_dist[hor][new_mod][i/100+1][(i+len)/100];
                    }
                }
            }
        }
    }
    for(int hor=0; hor<2; hor++) {
        int mymax = hor ? m : n;
        for(int len=100; len<=mymax; len+=100) {
            for(int i=mymax/100*100; i-len>=0; i-=100) {
                for(int mod=0; mod<840; mod++) {
                    if(len == 100) cached_dist[hor][mod][i/100][(i-len)/100] = distance_between(hor,mod,i,i-len,false);
                    else {
                        int small_dist = cached_dist[hor][mod][i/100][i/100-1];
                        int new_mod = (mod+small_dist)%840;
                        cached_dist[hor][mod][i/100][(i-len)/100] = small_dist + cached_dist[hor][new_mod][i/100-1][(i-len)/100];
                    }
                }
            }
        }
    }
}

int main() {
    ios_base::sync_with_stdio(0);
    cin >> n >> m >> q;
    for(int i=0; i<n; i++) {
        for(int j=0; j<m; j++) {
            cin >> lights[i*m+j];
            int mod = lights[i*m+j].length();
            for(int k=0; k<mod; k++) {
                char c = lights[i*m+j][k];
                if(c == '0') passable[0][mod][k][i] = true;
                else passable[1][mod][k][j] = true;
            }
        }
    }
    for(int i=0; i<840; i++) {
        pair<int,int> pos = {0,0};
        while(pos.first != n && pos.second != m) {
            int mod = i % lights[pos.first*m+pos.second].length();
            char c = lights[pos.first*m+pos.second][mod];
            if(c == '0') pos.first++;
            else pos.second++;
        }
        if(pos.first == n) { // pasy pionowe
            p[i].init(true);
            while(pos.second != m) {
                bool flag = false;
                for(int mod=2; mod<=8; mod++) {
                    if(passable[1][mod][i%mod][pos.second]) {
                        pos.second++;
                        flag = true;
                        break;
                    }
                }
                if(!flag) {
                    p[i].add_bound(pos.second);
                    pos.second++;
                }
            }
        }
        else { // pasy poziome
            p[i].init(false);
            while(pos.first != n) {
                bool flag = false;
                for(int mod=2; mod<=8; mod++) {
                    if(passable[0][mod][i%mod][pos.first]) {
                        pos.first++;
                        flag = true;
                        break;
                    }
                }
                if(!flag) {
                    p[i].add_bound(pos.first);
                    pos.first++;
                }
            }
        }
        p[i].finish();
    }
    compute_cached();
    for(int i=0; i<q; i++) {
        // if(i%10000 == 0) cout << i/10000 << "%" << endl;
        int time, timemod, elapsed;
        pair<int,int> pos, to;
        cin >> time >> pos.first >> pos.second >> to.first >> to.second;
        timemod = time % 840;
        if(p[timemod].horizontal) {
            elapsed = distance_between(true, timemod, pos.second, to.second, true);
        } else {
            elapsed = distance_between(false, timemod, pos.first, to.first, true);
        }
        cout << time+elapsed << endl;
    }
}