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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
#include <bits/stdc++.h>
#include <random>
#define ll long long int
#define pb push_back
#define st first
#define nd second
#define pii pair<int,int>
#define mp make_pair
#define pll pair<long long,long long>
#define ld long double
#define ull unsigned long long

using namespace std;

struct zap{
    pii from;
    pii to;
    int startTime;
    int id;
};

int n, m, q;
const int nax = 15005;
vector<vector<int> >  a;
vector<vector<int> > cycleLen;
vector<zap> all[840];

int ans[1000007];

bool sasCol[nax];
bool sasRow[nax];
int gora[842][nax];
int dol[842][nax];
int prawo[842][nax];
int lewo[842][nax];
bool goradol[842];

short int jmp[17][841][nax]; //[841][nax][17];

int pp[25];

void solve(){
    pp[0] = 1;
    for(int i=1;i<25;i++){
        pp[i] = pp[i - 1] * 2;
        pp[i] %= 840;
    }
    cin >> n >> m >> q;
    a.resize(n + 1);
    cycleLen.resize(n + 1);
    for(int i=0;i<=n;i++){
        a[i].resize(m + 1);
        cycleLen[i].resize(m + 1);
    }

    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            string s; cin >> s;
            cycleLen[i][j] = s.size();
            int v = 0;
            int w = 1;
            for(char x : s){
                v += w * (x - '0');
                w *= 2;
            }
            a[i][j] = v;
        }
    }
    for(int i=1;i<=q;i++){
        zap cur;
        cin >> cur.startTime;

        pii from, to;
        int a1,a2,a3,a4;
        cin >> a1 >> a2 >> a3 >> a4;

        cur.from = mp(a1, a2);
        cur.to = mp(a3, a4);
        cur.id = i;
        all[cur.startTime % 840].pb(cur);
    }
    
    for(int mod=0;mod<840;mod++){
        int w[9];
        for(int j=1;j<=8;j++) w[j] = mod % j;
        for(int i=0;i<nax;i++){
            sasCol[i] = false;
            sasRow[i] = false;
        }
        for(int i=1;i<=n;i++){
            for(int j=1;j<=m;j++){
                int v = (a[i][j] & (1 << w[cycleLen[i][j]]));
                sasRow[i - 1] |= (v == 0);
                sasCol[j - 1] |= (v != 0);
            }
        }
        bool colsUnited = true;
        bool rowsUnited = true;
        for(int i=0;i<n;i++){
            if(sasRow[i] == false) rowsUnited = false;
        }
        for(int i=0;i<m;i++){
            if(sasCol[i] == false) colsUnited = false;
        }
        if(colsUnited) goradol[mod] = true;
        // da sie przejsc wierszem z lewo na prawo xd, czyli mamy warstwy z gory do dolu
        gora[mod][0] = 0;
        for(int i=1;i<=n;i++){
            if(sasRow[i - 1]) gora[mod][i] = gora[mod][i - 1];
            else gora[mod][i] = i;
        }
        dol[mod][n] = n;
        for(int i=n-1;i>=0;i--){
            if(sasRow[i]) dol[mod][i] = dol[mod][i + 1];
            else dol[mod][i] = i;
        }

        lewo[mod][0] = 0;
        for(int i=1;i<=m;i++){
            if(sasCol[i - 1]) lewo[mod][i] = lewo[mod][i - 1];
            else lewo[mod][i] = i;
        }

        prawo[mod][m] = m;
        for(int i=m-1;i>=0;i--){
            if(sasCol[i]) prawo[mod][i] = prawo[mod][i + 1];
            else prawo[mod][i] = i;
        }
    }

    // jump pointery w PRAWO
    for(int mod=0;mod<840;mod++){
        for(int j=0;j<=m;j++){
            int go = prawo[mod][j];
            int nxt = mod + 1;
            if(nxt == 840) nxt -= 840;
            go = prawo[nxt][go];
            jmp[0][mod][j] = go;
        }
    }
    // mod J 17
    for(int lv=1;lv<17;lv++){
        for(int mod=0;mod<840;mod++){
            for(int j=0;j<=m;j++){
                int go = jmp[lv - 1][mod][j];
                int timer = (mod + pp[lv - 1]);
                if(timer >= 840) timer -= 840;
                go = jmp[lv - 1][timer][go];
                jmp[lv][mod][j] = go;
            }
        }
    }

    for(int mod=0;mod<840;mod++){
        for(auto cur : all[mod]){
            if(goradol[mod] == false && cur.from.nd <= cur.to.nd){
                int id = cur.id;
                // chcemy isc w prawo
                int start = cur.from.nd;
                int finish = cur.to.nd;
                if(prawo[mod][start] >= finish){
                    ans[id] = cur.startTime;
                    continue;
                }
                int cost = 0;
                int timer = mod;
                int pos = start;
                for(int i=16;i>=0;i--){
                    int go = jmp[i][timer][pos];
                    if(go < finish){
                        cost += (1 << i);
                        timer += pp[i];
                        if(timer >= 840) timer -= 840;
                        pos = go;
                    }
                }
                cost += 1;
                ans[id] = cur.startTime + cost;
            }
        }
    }


    //// LEWO

    // jump pointery w LEWO
    for(int mod=0;mod<840;mod++){
        for(int j=0;j<=m;j++){
            int go = lewo[mod][j];
            int nxt = mod + 1;
            if(nxt == 840) nxt -= 840;
            go = lewo[nxt][go];
            jmp[0][mod][j] = go;
        }
    }
    // mod J 17
    for(int lv=1;lv<17;lv++){
        for(int mod=0;mod<840;mod++){
            for(int j=0;j<=m;j++){
                int go = jmp[lv - 1][mod][j];
                int timer = (mod + pp[lv - 1]);
                if(timer >= 840) timer -= 840;
                go = jmp[lv - 1][timer][go];
                jmp[lv][mod][j] = go;
            }
        }
    }

    for(int mod=0;mod<840;mod++){
        for(auto cur : all[mod]){
            if(goradol[mod] == false && cur.from.nd > cur.to.nd){
                int id = cur.id;
                // chcemy isc w prawo
                int start = cur.from.nd;
                int finish = cur.to.nd;
                if(lewo[mod][start] <= finish){
                    ans[id] = cur.startTime;
                    continue;
                }
                int cost = 0;
                int timer = mod;
                int pos = start;
                for(int i=16;i>=0;i--){
                    int go = jmp[i][timer][pos];
                    if(go > finish){
                        cost += (1 << i);
                        timer += pp[i];
                        if(timer >= 840) timer -= 840;
                        pos = go;
                    }
                }
                cost += 1;
                ans[id] = cur.startTime + cost;
            }
        }
    }


    //////// DOL

    for(int mod=0;mod<840;mod++){
        for(int j=0;j<=n;j++){
            int go = dol[mod][j];
            int nxt = (mod + 1);
            if(nxt == 840) nxt -= 840;
            go = dol[nxt][go];
            jmp[0][mod][j] = go;
        }
    }
    // mod J 17
    for(int lv=1;lv<17;lv++){
        for(int mod=0;mod<840;mod++){
            for(int j=0;j<=n;j++){
                int go = jmp[lv-1][mod][j];
                int timer = (mod + pp[lv - 1]);
                if(timer >= 840) timer -= 840;
                go = jmp[lv-1][timer][go];
                jmp[lv][mod][j] = go;
            }
        }
    }

    for(int mod=0;mod<840;mod++){
        for(auto cur : all[mod]){
            if(goradol[mod] == true && cur.from.st <= cur.to.st){
                int id = cur.id;
                // chcemy isc w prawo
                int start = cur.from.st;
                int finish = cur.to.st;
                if(dol[mod][start] >= finish){
                    ans[id] = cur.startTime;
                    continue;
                }
                int cost = 0;
                int timer = mod;
                int pos = start;
                for(int i=16;i>=0;i--){
                    int go = jmp[i][timer][pos];
                    if(go < finish){
                        cost += (1 << i);
                        timer += pp[i];
                        if(timer >= 840) timer -= 840;
                        pos = go;
                    }
                }
                cost += 1;
                ans[id] = cur.startTime + cost;
            }
        }
    }


    ////////// GORA

    for(int mod=0;mod<840;mod++){
        for(int j=0;j<=n;j++){
            int go = gora[mod][j];
            int nxt = (mod + 1);
            if(nxt == 840) nxt -= 840;
            go = gora[nxt][go];
            jmp[0][mod][j] = go;
        }
    }
    // mod J 17
    for(int lv=1;lv<17;lv++){
        for(int mod=0;mod<840;mod++){
            for(int j=0;j<=n;j++){
                int go = jmp[lv-1][mod][j];
                int timer = (mod + pp[lv - 1]);
                if(timer >= 840) timer -= 840;
                go = jmp[lv-1][timer][go];
                jmp[lv][mod][j] = go;
            }
        }
    }

    for(int mod=0;mod<840;mod++){
        for(auto cur : all[mod]){
            if(goradol[mod] == true && cur.from.st >= cur.to.st){
                int id = cur.id;
                // chcemy isc w prawo
                int start = cur.from.st;
                int finish = cur.to.st;
                if(gora[mod][start] <= finish){
                    ans[id] = cur.startTime;
                    continue;
                }
                int cost = 0;
                int timer = mod;
                int pos = start;
                for(int i=16;i>=0;i--){
                    int go = jmp[i][timer][pos];
                    if(go > finish){
                        cost += (1 << i);
                        timer += pp[i];
                        if(timer >= 840) timer -= 840;
                        pos = go;
                    }
                }
                cost += 1;
                ans[id] = cur.startTime + cost;
            }
        }
    }
   
    for(int i=1;i<=q;i++) cout << ans[i] << "\n";

}

int main(){
    ios_base::sync_with_stdio(0); cin.tie(0);

    int tt = 1;
    // cin >> tt;
    while(tt--) solve();

    return 0;
}