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
// Arti1990, II UWr

#include <bits/stdc++.h>

#define forr(i, n)                  for(int i=0; i<n; i++)
#define FOREACH(iter, coll)         for(auto iter = coll.begin(); iter != coll.end(); ++iter)
#define FOREACHR(iter, coll)        for(auto iter = coll.rbegin(); iter != coll.rend(); ++iter)
#define lbound(P,K,FUN)             ({auto SSS=P, PPP = P-1, KKK=(K)+1; while(PPP+1!=KKK) {SSS = (PPP+(KKK-PPP)/2); if(FUN(SSS)) KKK = SSS; else PPP = SSS;} PPP;})
#define testy()                     int _tests; scanf("%d", &_tests); FOR(_test, 1, _tests)
#define CLEAR(tab)                  memset(tab, 0, sizeof(tab))
#define CONTAIN(el, coll)           (coll.find(el) != coll.end())
#define FOR(i, a, b)                for(int i=a; i<=b; i++)
#define FORD(i, a, b)               for(int i=a; i>=b; i--)
#define MP                          make_pair
#define PB                          push_back
#define ff                          first
#define ss                          second
#define deb(X)                      X;

#define M 1000000007
#define INF 1000000007LL

#define MAKS 850 // 840 ??

using namespace std;

int n, m, q;
char slowo[1000007];
string str;
vector <vector<string>> sl;
vector <vector<int>> dl;
bool pion[15007][857], poziom[15007][857], pom[10][10]; // czy zablokowany w danej sekundzie
int skok[15][15007][857];

struct zapytanie
{
    int t, w1, w2, k1, k2, res;
};
zapytanie z[1000007];

void ustaw_ulice()
{
    forr(j, m) // pionowe ulice
    {
        forr(x, 10)
            forr(y, 10)
                pom[x][y] = 1;
        forr(i, n)
        {
            forr(d, dl[i][j])
            {
                if(sl[i][j][d] == '1')
                    pom[dl[i][j]][d] = 0;
            }
        }
        //cout << " pion[" << j << "]: ";
        forr(s, MAKS)
        {
            pion[j][s] = 1;
            FOR(d, 1, 8)
                if(pom[d][s%d] == 0)
                {
                    pion[j][s] = 0;
                    break;
                }
            //cout << pion[j][s];
        }
        //cout << endl;
    }

    forr(i, n) // poziome ulice
    {
        forr(x, 10)
            forr(y, 10)
                pom[x][y] = 1;
        forr(j, m)
        {
            forr(d, dl[i][j])
            {
                if(sl[i][j][d] == '0')
                    pom[dl[i][j]][d] = 0;
            }
        }
        //cout << " poziom[" << i << "]: ";
        forr(s, MAKS)
        {
            poziom[i][s] = 1;
            FOR(d, 1, 8)
                if(pom[d][s%d] == 0)
                {
                    poziom[i][s] = 0;
                    break;
                }
            //cout << poziom[i][s];
        }
        //cout << endl;
    }
}

void licz_skoki(int n)
{
    FOR(lvl, 1, 13)
    {
        int dl = 1<<(lvl-1);
        int dl2 = 1<<lvl;
        FOR(i, 0, n-dl2)
        {
            forr(t, 840)
            {
                int s1 = skok[lvl-1][i][t];
                skok[lvl][i][t] = s1 + skok[lvl-1][i+dl][(t+s1)%840];
                //cout << "skok[" << lvl << "][" << i << "][" << t << "] = " << skok[lvl][i][t] << endl;
            }
        }
    }
}

int licz_skok(int t, int pocz, int kon)
{
    int lvl = 0;
    int dl = kon-pocz;
    while(dl > 0)
    {
        if(dl&1)
        {
            t += skok[lvl][pocz][t%840];
            pocz += (1<<lvl);
        }
        dl /= 2;
        lvl++;
    }
//    cout << "skok: " << t << endl;
    return t;
}

int solve()
{
    /*forr(i, 15)
        forr(j, 15000)
            forr(k, 850)
                skok[i][j][k] = n++;
    return 0;*/
    scanf("%d %d %d", &n, &m, &q);
    forr(i, n)
    {
        vector <string> vs;
        vector <int> vd;
        forr(j, m)
        {
            scanf("%s", slowo);
            str = slowo;
            vs.PB(str);
            vd.PB(str.size());
        }
        sl.PB(vs);
        dl.PB(vd);
    }

    ustaw_ulice();

    forr(i, q)
    {
        scanf("%d %d %d %d %d", &z[i].t, &z[i].w1, &z[i].k1, &z[i].w2, &z[i].k2);
        z[i].res = z[i].t;
    }

//    cout << "PRAWO" << endl;
    // LICZYMY W PRAWO
    forr(i, m)
        forr(t, 840)
        {
            int x = 0;
            while(pion[i][(t+x)%840]) // wywalic modulo? (wszedzie)
                x++;
            skok[0][i][t] = x;
        }
    licz_skoki(m);
    forr(i, q)
        z[i].res = max(z[i].res, licz_skok(z[i].t, z[i].k1, z[i].k2));


//    cout << "LEWO" << endl;
    // LICZYMY W LEWO
    forr(i, m/2)
        forr(t, 840)
        {
            swap(skok[0][i][t], skok[0][m-1-i][t]);
        }
    licz_skoki(m);
    forr(i, q)
        z[i].res = max(z[i].res, licz_skok(z[i].t, m-z[i].k1, m-z[i].k2));


    // LICZYMY W DOL
//    cout << "DOL" << endl;
    forr(i, n)
        forr(t, 840)
        {
            int x = 0;
            while(poziom[i][(t+x)%840])
                x++;
            skok[0][i][t] = x;
        }
    licz_skoki(n);
    forr(i, q)
        z[i].res = max(z[i].res, licz_skok(z[i].t, z[i].w1, z[i].w2));


//    cout << "GORA" << endl;
    // LICZYMY W GORE
    forr(i, n/2)
        forr(t, 840)
        {
            swap(skok[0][i][t], skok[0][n-1-i][t]);
        }
    licz_skoki(n);
    forr(i, q)
        z[i].res = max(z[i].res, licz_skok(z[i].t, n-z[i].w1, n-z[i].w2));

    forr(i, q)
        printf("%d\n", z[i].res);

    return 0;
}

int main()
{
    solve();

    return 0;
}