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
247
248
249
250
251
252
253
254
#include <iostream>
#include <algorithm>
#include <list>
#include <string.h>

#ifdef TEST_MODE
#include <fstream>
#endif

using namespace std;

int t;
int n, m;
int sn, sm;

class Square;
class Light;

Square * squares;
Light * lights;

class Light {
public:
    char stateForTime(int time) {
        return sequence[time % period];
    }
    bool canHorizontally() {
        return sequence[t % period] == '1';
    }
    bool canVertically() {
        return sequence[t % period] == '0';
    }
    char period;
    char sequence[9];
};



class Square {
public:
    int i, j;
    bool occupied = false;
    bool east() {
        if ( j == m)
            return false;
        if ( squares[sm*i + j+1].occupied )
            return false;
        return lightsEast();
    };
    bool west() {
        if ( j == 0)
            return false;
        if ( squares[sm*i + j-1].occupied )
            return false;
        return lightsWest();
    };
    bool north() {
        if ( i == 0)
            return false;
        if ( squares[sm*(i-1) + j].occupied )
            return false;
        return lightsNorth();
    };
    bool south() {
        if ( i == n)
            return false;
        if ( squares[sm*(i+1) + j].occupied )
            return false;
        return lightsSouth();
    }
    bool lightsEast() {
        bool can1, can2;
        if ( i == 0 ) {
            can1 = false;
        } else {
            can1 = lights[(i-1)*m+j].canHorizontally();
        }
        if ( i == n ) {
            can2 = false;
        } else {
            can2 = lights[i*m+j].canHorizontally();
        }
        return can1 || can2;
    }
    
    bool lightsWest() {
        bool can1, can2;
        if ( i == 0 ) {
            can1 = false;
        } else {
            can1 = lights[(i-1)*m+j-1].canHorizontally();
        }
        if ( i == n ) {
            can2 = false;
        } else {
            can2 = lights[i*m+j-1].canHorizontally();
        }
        return can1 || can2;
    }
    
    bool lightsNorth() {
        bool can1, can2;
        if ( j == 0 ) {
            can1 = false;
        } else {
            can1 = lights[(i-1)*m+j-1].canVertically();
        }
        if ( j == m ) {
            can2 = false;
        } else {
            can2 = lights[(i-1)*m+j].canVertically();
        }
        return can1 || can2;
    }
    
    bool lightsSouth() {
        bool can1, can2;
        if ( j == 0 ) {
            can1 = false;
        } else {
            can1 = lights[i*m+j-1].canVertically();
        }
        if ( j == m ) {
            can2 = false;
        } else {
            can2 = lights[i*m+j].canVertically();
        }
        return can1 || can2;
    }
};

int main() {
    
#ifdef TEST_MODE
    std::ifstream in("1.in"); std::cin.rdbuf(in.rdbuf());
#endif
    std::ios_base::sync_with_stdio(false);
    int q;
    cin >> n >> m >> q;
    sn = n + 1;
    sm = m + 1;
    lights = new Light[n*m];
    for ( int i = 0; i < n; ++ i ) {
        for ( int j = 0; j < m; ++j ) {
            Light * l = &lights[i*m+j];
            cin >> l->sequence;
            l->period = strlen(l->sequence);
        }
    }
    
    squares = new Square[(n+1)*(m+1)];
    for ( int i = 0; i <= n; ++i ) {
        for ( int j = 0; j <= m; ++j ) {
            Square * s = &squares[i*sm+j];
            s->i = i; s->j = j; s->occupied = false;
        }
    }
    
    std::list<Square*> squareList;
    std::list<Square*> newSquareList;
    std::list<Square*> iterList;
    while (q--) {
        for ( int i = 0; i <= n; ++i ) {
            for ( int j = 0; j <= m; ++j ) {
                squares[i*sm+j].occupied = false;
            }
        }
        
        squareList.clear();
        
        int i1, j1, i2, j2;
        cin >> t >> i1 >> j1 >> i2 >> j2;
        
        if ( i1 == i2 && j1 == j2 ) {
            cout << t << endl;
        } else {
            t--;
            squares[i1*sn+j1].occupied = true;
            squareList.push_back(&squares[i1*sn+j1]);
            newSquareList = std::list(squareList);
            bool found = false;
            int moved = 0;
            while ( !found ) {
                
                if (moved > 0 ) {
                    moved = 0;
                    iterList = std::list(newSquareList);
                    newSquareList.clear();
                } else {
                    t++;
                    iterList = std::list(squareList);
                }
                auto it = iterList.begin();
               
                while ( it != iterList.end() ) {
                    Square * s = *it;
                    Square * s2;
                    if ( s->north() ) {
                        s2 = &squares[(s->i-1)*sn + s->j];
                        if ( s2->i == i2 && s2->j == j2 ) {
                            found = true;
                            break;
                        } else {
                            moved++;
                            s2->occupied = true;
                            squareList.push_front(s2);
                            newSquareList.push_back(s2);
                        }
                    }
                    if ( s->south() ) {
                        s2 = &squares[(s->i+1)*sn + s->j];
                        if ( s2->i == i2 && s2->j == j2 ) {
                            found = true;
                            break;
                        } else {
                            moved++;
                            s2->occupied = true;
                            squareList.push_front(s2);
                            newSquareList.push_back(s2);
                        }
                    }
                    if ( s->east() ) {
                        s2 = &squares[(s->i)*sn + s->j + 1];
                        if ( s2->i == i2 && s2->j == j2 ) {
                            found = true;
                            break;
                        } else {
                            moved++;
                            s2->occupied = true;
                            squareList.push_front(s2);
                            newSquareList.push_back(s2);
                        }
                    }
                    if ( s->west() ) {
                        s2 = &squares[(s->i)*sn + s->j - 1];
                        if ( s2->i == i2 && s2->j == j2 ) {
                            found = true;
                            break;
                        } else {
                            moved++;
                            s2->occupied = true;
                            squareList.push_front(s2);
                            newSquareList.push_back(s2);
                        }
                    }
                    it++;
                }
            }
            cout << t << endl;
        }
    }
    
    return 0;
}