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
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
#define FOR(i, l, r) for(int i = (l); i <= (r); ++i)
#define REP(i, n) FOR(i, 0, (n) - 1)
#define ssize(x) int(x.size())
template<class A, class B> auto& operator<<(ostream &o, pair<A, B> p) {
    return o << '(' << p.first << ", " << p.second << ')';
}
template<class T> auto operator<<(ostream &o, T x) -> decltype(x.end(), o) {
    o << '{'; int i = 0; for(auto e : x) o << (", ")+2*!i++ << e; return o << '}';
}
#ifdef DEBUG
#define debug(x...) cerr << "[" #x "]: ", [](auto... $) {((cerr << $ << "; "), ...); }(x), cerr << '\n'
#else
#define debug(...) {}
#endif

constexpr int states = 3 * 5 * 7 * 8;
constexpr int LOG = 14;

auto parse_input() {
    int n, m, q;
    cin >> n >> m >> q;

    vector config(n, vector<string>(m));
    REP(i, n) REP(j, m)
        cin >> config[i][j];

    vector<tuple<int, int, int, int, int>> queries(q);
    for(auto &[t, x1, y1, x2, y2] : queries)
        cin >> t >> x1 >> y1 >> x2 >> y2;

    vector<vector<bool>> connected_x(n, vector<bool>(states));
    vector<vector<bool>> connected_y(m, vector<bool>(states));

    REP(a, n) REP(b, m) {
        int s = ssize(config[a][b]);
        REP(t, s) REP(d, states / s) {
            if(config[a][b][t] == '1')
                connected_y[b][d * s + t] = true;
            else
                connected_x[a][d * s + t] = true;
        }
    }

    vector<bool> is_x(states);
    vector ile_sum(n, vector<int>(states));

    array ile = {
        vector(1, vector<int>(n)),
        vector(2, vector<int>(n)),
        vector(3, vector<int>(n)),
        vector(4, vector<int>(n)),
        vector(5, vector<int>(n)),
        vector(6, vector<int>(n)),
        vector(7, vector<int>(n)),
        vector(8, vector<int>(n)),
    };

    REP(a, n) REP(b, m) {
        int s = ssize(config[a][b]);
        REP(t, s)
            ile[s - 1][t][a] += (config[a][b][t] == '1');
    }

    REP(i, n) REP(j, 8)
        REP(k, states)
            ile_sum[i][k] += ile[j][k % (j + 1)][i];

    REP(i, n) REP(j, states)
        if(ile_sum[i][j] == m)
            is_x[j] = true;

    vector<tuple<bool, int, int, int>> parsed_queries;
    for(auto &[t, x1, y1, x2, y2] : queries) {
        if(is_x[t % states]) 
            parsed_queries.emplace_back(true, t, x1, x2);
        else
            parsed_queries.emplace_back(false, t, y1, y2);
    }

    return tuple {connected_x, connected_y, parsed_queries};
}

auto get_left(const vector<vector<bool>> &connected) {
    int n = ssize(connected);
    vector left(n + 1, vector<int>(states));
    REP(s, states) left[0][s] = -1;
    REP(i, n) REP(s, states) {
        if(connected[i][s]) {
            REP(j, states) {
                int cur = (s - j >= 0 ? s - j : s - j + states);
                int prev = (cur + 1 < states ? cur + 1 : cur + 1 - states);
                left[i + 1][cur] = (connected[i][cur] ? 0 : left[i + 1][prev] + 1);
            }
            break;
        }
    }
    return left;
}

auto get_right(const vector<vector<bool>> &connected) {
    int n = ssize(connected);
    vector right(n + 1, vector<int>(states));
    REP(s, states) right[n][s] = -1;
    REP(i, n) REP(s, states) {
        if(connected[i][s]) {
            REP(j, states) {
                int cur = (s - j >= 0 ? s - j : s - j + states);
                int prev = (cur + 1 < states ? cur + 1 : cur + 1 - states);
                right[i][cur] = (connected[i][cur] ? 0 : right[i][prev] + 1);
            }
            break;
        }
    }
    return right;
}

auto square(vector<vector<int>> &jp, int jump) {
    const int n = ssize(jp);
    vector nt(n, vector<int>(states));
    REP(i, n) REP(j, states) {
        int x = jp[i][j];
        if(x == -1)
            nt[i][j] = -1;
        else {
            int y = jp[i + jump][(j + x) % states];
            if(y == -1)
                nt[i][j] = -1;
            else
                nt[i][j] = x + y;
        }
    }
    swap(jp, nt);
}


int main() {
    cin.tie(0)->sync_with_stdio(0);

    auto [connected_x, connected_y, queries] = parse_input();

    auto cur = get_right(connected_x);
    REP(l, LOG) {
        for(auto &[f, t, p, q] : queries)
            if(f && q >= p && ((q - p) & (1 << l))) {
                t += cur[p][t % states];
                p += (1 << l);
            }
        square(cur, (1 << l));
    }

    cur = get_left(connected_x);
    REP(l, LOG) {
        for(auto &[f, t, p, q] : queries)
            if(f && p > q && ((p - q) & (1 << l))) {
                t += cur[p][t % states];
                p -= (1 << l);
            }
        square(cur, -(1 << l));
    }

    cur = get_right(connected_y);
    REP(l, LOG) {
        for(auto &[f, t, p, q] : queries)
            if(!f && q >= p && ((q - p) & (1 << l))) {
                t += cur[p][t % states];
                p += (1 << l);
            }
        square(cur, (1 << l));
    }

    cur = get_left(connected_y);

    REP(l, LOG) {
        for(auto &[f, t, p, q] : queries)
            if(!f && p > q && ((p - q) & (1 << l))) {
                t += cur[p][t % states];
                p -= (1 << l);
            }
        square(cur, -(1 << l));
    }

    for(auto &[f, t, p, q] : queries)
        cout << t << "\n";
}