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

const int maxQ = 1e6 + 10;
const int maxM = 840;
int ans[maxQ];

struct Qu {
    int x1, y1, x2, y2, t;
    Qu() { scanf("%d%d%d%d%d", &t, &x1, &y1, &x2, &y2); }
};

vector<Qu> queries;
vector<vector<int>> tlen;
vector<vector<int>> t;
int M, Q;

int gcd(int a, int b) { return a == 0 ? b : gcd(b % a, a); }

void get_t(int x, int y) {
    char s[16];
    scanf("%s", s);
    int &n = tlen[x][y];
    while (s[n])
        n++;
    for (int i = 0; i < n; ++i)
        if (s[i] == '1')
            t[x][y] |= 1 << i;
    M = n * M / gcd(n, M);
}

int t_inverted(int x, int y) { return t[x][y] ^ ((1 << tlen[x][y]) - 1); }

int X, Y;

vector<int> roads[maxM];

struct Street {
    int t[10];
    Street() {
        for (int i = 0; i < 10; ++i)
            t[i] = (1 << i) - 1;
    }

    void add(int len, int val) { t[len] &= val; }

    void finish() {
        for (int mask, i = 2; i <= 8; ++i) {
            mask = t[i];
            for (int j = 2 * i; j <= 8; j += i) {
                mask = (mask << i) | t[i];
                t[j] &= mask;
            }
        }
    }

    void append_to_locked_minutes(int id) {
        if (t[5] == 0 || t[6] == 0 || t[7] == 0 || t[8] == 0)
            return;
        for (int i = 0; i < M; ++i)
            if ((t[5] & (1 << (i % 5))) && (t[6] & (1 << (i % 6)))
                && (t[7] & (1 << (i % 7))) && (t[8] & (1 << (i % 8)))) {
                // printf("%d locked in %d\n", id, i);
                roads[i].push_back(id);
            }
    }
};

int find_next(vector<int> &v, int x) {
    int a = -1, b = v.size();
    while (b - a > 1) {
        int avg = (a + b) / 2;
        if (v[avg] < x)
            a = avg;
        else
            b = avg;
    }
    return b;
}

int timedelta(int x1, int x2, int t) {
    // printf("computing %d --> %d (%d)\n", x1, x2, t);
    int steps = 0;
    while (true) {
        // printf("[%d,%d]", t, steps);
        // for (int a : roads[t])
        //     printf(" %d", a);
        // puts("");

        if (roads[t].empty())
            return steps;
        int i = find_next(roads[t], x1);
        // printf(">> %d <<\n", i);
        if (x1 <= x2) {
            if (i == roads[t].size() || roads[t][i] >= x2)
                return steps;
            // printf("x:%d --> %d\n", x1, roads[t][i]);
            x1 = roads[t][i];

        } else {
            i -= 1;
            if (i < 0 || roads[t][i] < x2)
                return steps;
            // printf("x:%d --> %d\n", x1, roads[t][i] + 1);
            x1 = roads[t][i] + 1;
        }

        steps++;
        t = (t + 1) % M;
    }
    return -1;
}

void solveX() {
    for (int x = 0; x < X; ++x) {
        Street s;
        for (int y = 0; y < Y; ++y)
            s.add(tlen[x][y], t[x][y]);
        s.finish();
        s.append_to_locked_minutes(x);
    }

    for (int i = 0; i < Q; ++i) {
        ans[i] = timedelta(queries[i].x1, queries[i].x2, queries[i].t % M);
    }
}

void solveY() {
    for (int i = 0; i < M; ++i)
        roads[i].clear();
    for (int y = 0; y < Y; ++y) {
        Street s;
        for (int x = 0; x < X; ++x)
            s.add(tlen[x][y], t_inverted(x, y));
        s.finish();
        s.append_to_locked_minutes(y);
    }

    for (int i = 0; i < Q; ++i) {
        ans[i] = max(ans[i],
                     timedelta(queries[i].y1, queries[i].y2, queries[i].t % M));
    }
}

int main() {
    M = 1;
    scanf("%d%d%d", &X, &Y, &Q);
    tlen.reserve(X);
    t.reserve(X);
    queries.reserve(Q);

    for (int x = 0; x < X; ++x) {
        t.emplace_back(Y);
        tlen.emplace_back(Y);
        for (int y = 0; y < Y; ++y)
            get_t(x, y);
    }
    // printf("%d\n", M);
    // for (int x = 0; x < X; ++x) {
    //     for (int y = 0; y < Y; ++y)
    //         printf("%d:%d ", tlen[x][y], t[x][y]);
    //     puts("");
    // }
    for (int i = 0; i < Q; ++i)
        queries.emplace_back();
    solveX();
    solveY();
    for (int i = 0; i < Q; ++i)
        printf("%d\n", ans[i] + queries[i].t);
}