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
#include <iostream>
#include <vector>
#include <numeric>

using namespace std;

vector<vector<string>> lights;

enum Direction {
    HORIZONTAL,
    VERTICAL
};

struct TimeGrid {
    vector<Direction> directions;
    vector<vector<int>> down, up;

    void resize(int s) {
        directions.resize(s);
        down.resize(s);
        up.resize(s);
    }
};

Direction dir(int row, int col, int time) {
    return lights[row][col][time % size(lights[row][col])] == '1' ? HORIZONTAL : VERTICAL;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int m, n, q;
    cin >> n >> m >> q;
    lights.resize(n + 1);
    int mod = 1;
    for (int row = 1; row <= n; ++row) {
        lights[row].resize(m + 1);
        for (int col = 1; col < m + 1; ++col) {
            cin >> lights[row][col];
            mod = lcm(mod, (int)size(lights[row][col]));
        }
    }
    TimeGrid time_grid;
    time_grid.resize(mod);
    for (int t = 0; t < mod; ++t) {
        int x{}, y{};
        while (x != n && y != m) {
            dir(x + 1, y + 1, t) == VERTICAL ? ++x : ++y;
        }
        time_grid.directions[t] = x == n ? VERTICAL : HORIZONTAL;
        if (time_grid.directions[t] == VERTICAL) {
            int strip1{};
            time_grid.down[t].resize(m + 1);
            for (int strip2{}; strip2 < m; ++strip2) {
                bool can_move = false;
                for (int f = 1; f <= n; ++f) {
                    if (dir(f, strip2 + 1, t) == HORIZONTAL) {
                        can_move = true;
                        break;
                    }
                }
                if (!can_move) {
                    for (int ind = strip1; ind <= strip2; ++ind) {
                        time_grid.down[t][ind] = strip2;
                    }
                    strip1 = strip2 + 1;
                }
            }
            for (int ind = strip1; ind <= m; ++ind) {
                time_grid.down[t][ind] = m;
            }

            strip1 = m;
            time_grid.up[t].resize(m + 1);
            for (int strip2 = m; strip2 > 0; --strip2) {
                bool can_move = false;
                for (int f = 1; f <= n; ++f) {
                    if (dir(f, strip2, t) == HORIZONTAL) {
                        can_move = true;
                        break;
                    }
                }
                if (!can_move) {
                    for (int ind = strip1; ind >= strip2; --ind) {
                        time_grid.up[t][ind] = strip2;
                    }
                    strip1 = strip2 - 1;
                }
            }
            for (int ind = strip1; ind >= 0; --ind) {
                time_grid.up[t][ind] = 0;
            }
        } else {
            int strip1{};
            time_grid.down[t].resize(n + 1);
            for (int strip2{}; strip2 < n; ++strip2) {
                bool can_move = false;
                for (int f = 1; f <= m; ++f) {
                    if (dir(strip2 + 1, f, t) == VERTICAL) {
                        can_move = true;
                        break;
                    }
                }
                if (!can_move) {
                    for (int ind = strip1; ind <= strip2; ++ind) {
                        time_grid.down[t][ind] = strip2;
                    }
                    strip1 = strip2 + 1;
                }
            }
            for (int ind = strip1; ind <= n; ++ind) {
                time_grid.down[t][ind] = n;
            }

            strip1 = n;
            time_grid.up[t].resize(n + 1);
            for (int strip2 = n; strip2 > 0; --strip2) {
                bool can_move = false;
                for (int f = 1; f <= m; ++f) {
                    if (dir(strip2, f, t) == VERTICAL) {
                        can_move = true;
                        break;
                    }
                }
                if (!can_move) {
                    for (int ind = strip1; ind >= strip2; --ind) {
                        time_grid.up[t][ind] = strip2;
                    }
                    strip1 = strip2 - 1;
                }
            }
            for (int ind = strip1; ind >= 0; --ind) {
                time_grid.up[t][ind] = 0;
            }
        }
    }
    for (int r = 0; r < q; ++r) {
        int t, a, b, c, d;
        cin >> t >> a >> b >> c >> d;
        int ct = t;
        if (a == c && b == d) {
            cout << ct << '\n';
            continue;
        }
        while (a != c || b != d) {
            if (time_grid.directions[ct % mod] == HORIZONTAL) {
                b = d;
                if (a < c) {
                    a = min(time_grid.down[ct % mod][a], c);
                } else {
                    a = max(time_grid.up[ct % mod][a], c);
                }
            } else {
                a = c;
                if (b < d) {
                    b = min(time_grid.down[ct % mod][b], d);
                } else {
                    b = max(time_grid.up[ct % mod][b], d);
                }
            }
            ++ct;
        }
        cout << ct - 1 << '\n';
    }
}