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
#include <bits/stdc++.h>

#define ll long long
#define str string
#define pii pair<int, int>
#define pll pair<ll, ll>
#define fi first
#define se second

#define vc vector<char>
#define vvc vector<vc>
#define vi vector<int>
#define vvi vector<vi>
#define vvvi vector<vvi>
#define vvvvi vector<vvvi>
#define vll vector<ll>
#define vvll vector<vll>
#define vvvll vector<vvll>
#define vvvvll vector<vvvll>
#define vs vector<str>
#define vvs vector<vs>
#define vpii vector<pii>
#define vvpii vector<vpii>
#define vpll vector<pll>
#define vvpll vector<vpll>
#define vb vector<bool>
#define vvb vector<vb>
#define rep(i, a, b) for (int i = (a); i < int(b); i++)
#define repi(i, a, b) for (int i = (a); i <= int(b); i++)

using namespace std;

int nww = 1;
int n, m, q;


struct skrzy {
    int size;
    vb s;
    vi zmiana_za;
};
struct fragment {
    int l, r;
    int min_czas_zmiany;
};
struct plansza {
    vector<fragment> piony;
    vector<fragment> pozy;
};

vector<plansza> plansze;
vector<vector<skrzy>> skr;

int find_frag_containing(vector<fragment> & frags, int x) {
    int lo = 0;
    int hi = frags.size() - 1;
    while (lo <= hi) {
        int mid = (lo + hi) / 2;
        if (frags[mid].l <= x && frags[mid].r >= x)
            return mid;
        else if (frags[mid].l > x)
            hi = mid - 1;
        else
            lo = mid + 1;
    }
    return 0;
}

void fill_plansze() {
    plansze = vector<plansza>(nww);

    for (int step = 0; step < nww; step++) {

        plansza & pl = plansze[step];

        // piony
        int prev = 0;
        for (int j = 1; j <= m; j++) {
            bool jest_krecha = true;
            for (int i = 1; i <= n; i++) {
                if (skr[i][j].s[step % skr[i][j].size]) {
                    jest_krecha = false;
                    break;
                }
            }

            if (jest_krecha) {
                fragment fr = {prev, j - 1, nww};
                for (int i = 1; i <= n; i++) {
                    fr.min_czas_zmiany = min(fr.min_czas_zmiany, skr[i][j].zmiana_za[step % skr[i][j].size]);
                }
                pl.piony.push_back(fr);
                prev = j;
            }
        }
        pl.piony.push_back({prev, m, 0});

        // pozy
        prev = 0;
        for (int i = 1; i <= n; i++) {
            bool jest_krecha = true;
            for (int j = 1; j <= m; j++) {
                if (!skr[i][j].s[step % skr[i][j].size]) {
                    jest_krecha = false;
                    break;
                }
            }

            if (jest_krecha) {
                fragment fr = {prev, i - 1, nww};
                for (int j = 1; j <= m; j++) {
                    fr.min_czas_zmiany = min(fr.min_czas_zmiany, skr[i][j].zmiana_za[step % skr[i][j].size]);
                }
                pl.pozy.push_back(fr);
                prev = i;
            }
        }
        pl.pozy.push_back({prev, n, 0});
    }
}

void fill_zmiana() {
    repi(i, 1, n) {
        repi(j, 1, m) {
            skr[i][j].zmiana_za = vi(skr[i][j].size, skr[i][j].size - 1);
            for (int k = 2 * skr[i][j].size; k >= 0; k--) {
                if (skr[i][j].s[k % skr[i][j].size] != skr[i][j].s[(k + 1) % skr[i][j].size]) {
                    skr[i][j].zmiana_za[k % skr[i][j].size] = 1;
                }
                else {
                    skr[i][j].zmiana_za[k % skr[i][j].size] = skr[i][j].zmiana_za[(k + 1) % skr[i][j].size] + 1;
                }
            }
        }
    }
}

void read_data() {
    cin >> n >> m >> q;
    skr = vector<vector<skrzy>>(n + 1, vector<skrzy>(m + 1));
    repi(i, 1, n) {
        repi(j, 1, m) {
            str sl;
            cin >> sl;
            skr[i][j].size = sl.size();
            skr[i][j].s = vb(sl.size());
            rep(k, 0, sl.size()) {
                skr[i][j].s[k] = sl[k] == '1';
            }
            nww = (nww * skr[i][j].size) / gcd(nww, skr[i][j].size);
        }
    }
}


int ans_query(int a, int b, int x, int y, int t) {
    plansza & pl = plansze[t % nww];

    if (pl.piony.size() == 1) { // lecimy poziomy
        int fr_idx = find_frag_containing(pl.pozy, a);
        fragment fr = pl.pozy[fr_idx];

        if (x >= a) {
            while (x > fr.r) {
                t += fr.min_czas_zmiany;
                fr_idx = find_frag_containing(plansze[t % nww].pozy, fr.r + 1);
                fr = plansze[t % nww].pozy[fr_idx];
            }
        }
        else {
            while (x < fr.l) {
                t += plansze[t % nww].pozy[fr_idx - 1].min_czas_zmiany;
                fr_idx = find_frag_containing(plansze[t % nww].pozy, fr.l - 1);
                fr = plansze[t % nww].pozy[fr_idx];
            }
        }
    }
    else { // lecimy piony
        int fr_idx = find_frag_containing(pl.piony, b);
        fragment fr = pl.piony[fr_idx];

        if (y >= b) {
            while (y > fr.r) {
                t += fr.min_czas_zmiany;
                fr_idx = find_frag_containing(plansze[t % nww].piony, fr.r + 1);
                fr = plansze[t % nww].piony[fr_idx];
            }
        }
        else {
            while (y < fr.l) {
                t += plansze[t % nww].piony[fr_idx - 1].min_czas_zmiany;
                fr_idx = find_frag_containing(plansze[t % nww].piony, fr.l - 1);
                fr = plansze[t % nww].piony[fr_idx];
            }
        }
    }

    return t;
}


void solve() {
    read_data();
    fill_zmiana();
    fill_plansze();

    while (q--) {
        int a, b, x, y, t;
        cin >> t >> a >> b >> x >> y;
        cout << ans_query(a, b, x, y, t) << '\n';
    }
}


int main() {

    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    bool _multipleTestCases = false;

    if (_multipleTestCases) {
        ll t; cin >> t;
        while (t--)
            solve();
    }
    else {
        solve();
    }

    return 0;
}