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
#pragma GCC optimize("O3")

// #define ASSERT

#include<bits/stdc++.h>
using namespace std;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define losuj(a, b) uniform_int_distribution<long long>(a, b)(rng)

#define sz(s) (int)s.size()
#define all(s) s.begin(), s.end()
#define f first
#define s second
#define pb push_back

#define ii pair<int, int>
#define vi vector<int>
#define vii vector<ii>
#define vvi vector<vi>
#define vvii vector<vii>

#define ll long long

const int LCM = 840;
const int N = 15007;
const int LOG = 18;
const int INF = 1e9 + 7;

enum kier {
    W_LEWO,
    W_PRAWO
};

int n, m, q;
short zero[2][LCM][N]; // 0 - lewo, 1 - prawo
short skok[LOG][2][LCM][N];

vector<vector<string> > tab;
bool mam_wiersze[LCM]; // 0 - wiersze, 1 - kolumny
vi min_ans(LCM, INF);
bool Union[LCM][N][2];

void prep() {
    // tworzymy poziomy
    for (int i = 0; i < n; i++)
        for (int j = 0; j < m; j++) {
            int dl = sz(tab[i][j]);
            for (int k = 0; k < dl; k++) {
                if (tab[i][j][k] == '0') {
                    for (int l = k; l < LCM; l += dl)
                        Union[l][i][0] = true; // laczymy dwa wiersze
                }
                else {
                    for (int l = k; l < LCM; l += dl)
                        Union[l][j][1] = true; // laczymy dwie kolumny
                }
            }
        }

    for (int tura = 0; tura < LCM; tura++) {
        bool mam_wiersz = 0;

        for (int i = 0; i < n; i++)
            if (Union[tura][i][0] == 0)
                mam_wiersz = true;

#ifdef ASSERT
        for (int i = 0; i < m; i++)
            if (Union[tura][i][1] == 0)
                assert(not mam_wiersz);
#endif

        mam_wiersze[tura] = mam_wiersz;

        bool ind = mam_wiersz ^ 1;

        zero[kier::W_LEWO][tura][0] = 0;
        for (int i = 1; i < N; i++)
            zero[kier::W_LEWO][tura][i] = Union[tura][i - 1][ind] == 0 ? i : zero[kier::W_LEWO][tura][i - 1];

        zero[kier::W_PRAWO][tura][N - 1] = N - 1;
        for (int i = N - 2; i >= 0; i--)
            zero[kier::W_PRAWO][tura][i] = Union[tura][i][ind] == 0 ? i : zero[kier::W_PRAWO][tura][i + 1];
    }

    // zrob min_ans
    for (int i = 0; i < LCM; i++)
        for (int j = 1; j < LCM; j++)
            if (mam_wiersze[i] != mam_wiersze[(i + j) % LCM]) {
                min_ans[i] = j;
                break;
            }

    // zrob skakanie
    // pierwszy skok

    for (int tura = 0; tura < LCM; tura++)
        for (int k = kier::W_LEWO; k <= kier::W_PRAWO; k++)
            for (int i = 0; i < N; i++)
                skok[0][k][tura][i] = zero[k][(tura + 1) % LCM][zero[k][tura][i]];

    // reszta skoków
    for (int poz = 1; poz < LOG; poz++)
        for (int k = kier::W_LEWO; k <= kier::W_PRAWO; k++)
            for (int tura = 0; tura < LCM; tura++)
                for (int i = 0; i < N; i++)
                    skok[poz][k][tura][i] = skok[poz - 1][k][(tura + (1 << (poz - 1))) % LCM][skok[poz - 1][k][tura][i]];
}

void solve() {
    cin >> n >> m >> q;

    tab = vector<vector<string> >(n, vector<string>(m));
    for (int i = 0; i < n; i++)
        for (int j = 0; j < m; j++)
            cin >> tab[i][j];

    prep();

    for (int i = 0; i < q; i++) {
        int t, a, b, c, d;
        cin >> t >> a >> b >> c >> d;

        int tmod = t % LCM;
        int pocz, kon;

        if (mam_wiersze[tmod]) {
            pocz = a;
            kon = c;
        }
        else {
            pocz = b;
            kon = d;
        }

        int my_kier = (pocz <= kon) ? kier::W_PRAWO : kier::W_LEWO;
        int ans = 0;

        // proboje na paue skokami
        int my_poz = pocz;
        int my_tour = tmod;

        for (int poz = LOG - 1; poz >= 0; poz--) {
            int next_poz = skok[poz][my_kier][my_tour][my_poz];
            int next_tour = (my_tour + (1 << poz)) % LCM;

            if ((my_kier == kier::W_LEWO and next_poz > kon) or
                (my_kier == kier::W_PRAWO and next_poz < kon)) {
                ans += (1 << poz);
                my_poz = next_poz;
                my_tour = next_tour;
            }
        }
        ans++; // wykonujemy ostatni skok, żeby osiągnąć cel

        if ((my_kier == kier::W_LEWO and kon >= zero[kier::W_LEWO][tmod][pocz]) or
            (my_kier == kier::W_PRAWO and kon <= zero[kier::W_PRAWO][tmod][pocz]))
            ans = 0;

        cout << t + min(ans, min_ans[tmod]) << '\n';
    }
}

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

    int tests = 1;
    // cin >> tests;

    for (int i = 0; i < tests; i++)
        solve();

    return 0;
}