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
#include<bits/stdc++.h>
#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 PB push_back
#define EB emplace_back
#define FI first
#define SE second
#define umap unordered_map
#define uset unordered_set
#define vi vector<int>
#define vvi vector<vi>
#define vll vector<ll>
#define vvll vector<vll>
#define vpii vector<pii>
#define pii pair<int, int>
#define pll pair<ll, ll>
#define ALL(X) (X).begin(),(X).end()
#ifndef DEBUG
#define endl (char)10
#endif
using namespace std;
using ll = long long;
using ld = long double;

template <class T>
istream& operator>> (istream& is, vector<T>& vec){
    FOR(i,0,vec.size()) is >> vec[i];
    return is;
}
template <class T>
ostream& operator<< (ostream& os, vector<T>& vec){
    for(auto& t : vec) os << t << " ";
    return os;
}

struct FindUnion {
    vi parent;
    vi sizes;

    FindUnion(int n) : parent(n), sizes(n){
        FOR(i,0,n){
            parent[i] = i;
            sizes[i] = 1;
        }
    }

    bool Union(int x, int y){
        x = Find(x);
        y = Find(y);
        if (x != y){
            if (sizes[x] < sizes[y]){
                parent[x] = y;
                sizes[y] += sizes[x];
            } else {
                parent[y] = x;
                sizes[x] += sizes[y];
            }
            return true;
        }
        return false;
    }

    int Find(int x){
        if (parent[x] != x)
            parent[x] = Find(parent[x]);
        return parent[x];
    }
};

struct query {
    int t, x, y;
    set<int> posib;
};

FindUnion clonx(vector<vector<string>>& V, int moment){
    int n = V.size();
    int m = V[0].size();
    FindUnion res((n + 1) * (m + 1));
    FOR(i,0,n) FOR(j,0,m){
        // 0 - pion, 1 - poziom
        char kier = (V[i][j][moment % V[i][j].size()]);
        int ij = i * (m + 1) + j;
        if (kier == '0'){
            res.Union(ij, ij + m + 1);
            res.Union(ij + 1, ij + m + 2);
        } else {
            res.Union(ij, ij + 1);
            res.Union(ij + m + 1, ij + m + 2);
        }
    }
    return res;
}

vi fu_parents(FindUnion& fu){
    vi res;
    int n = fu.parent.size();
    FOR(i,0,n) if (fu.parent[i] == i) res.PB(i);
    return res;
}

int main () {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    int n, m, q;
    cin >> n >> m >> q;
    vector<vector<string>> V(n, vector<string>(m));
    vector<query> Q(q);
    cin >> V;
    FOR(i,0,q){
        int a, b, c, d;
        cin >> Q[i].t >> a >> b >> c >> d;
        Q[i].x = a * (m + 1) + b;
        Q[i].y = c * (m + 1) + d;
    }
    int okres = V[0][0].size();
    FOR(i,0,n) FOR(j,0,m) okres = (okres * V[i][j].size()) / __gcd(okres, (int) V[i][j].size());
    vvi queries(okres);
    FOR(i,0,q){
        queries[Q[i].t % okres].PB(i);
    }
    FindUnion zero = clonx(V, 0);
    vi zero_parents = fu_parents(zero);
    vector<int> kolejka;
    int tosol = q;
    int kres = 0;
    while(tosol > 0){
        //cout << "Jeszcze" << tosol << endl;
        if (!queries[kres].empty()) {
            for(int x : queries[kres]){
                int co = zero.Find(Q[x].x);
                if (co == zero.Find(Q[x].y)){
                    tosol--;
                    //cout << x << " za darmo" << endl;
                } else {
                    kolejka.PB(x);
                    co = lower_bound(ALL(zero_parents), co) - zero_parents.begin();
                    Q[x].posib.insert(co);
                }
            }
            queries[kres].clear();
        }

        kres = (kres + 1) % okres;
        FindUnion pom = clonx(V, kres);
        vi parents = fu_parents(pom);
        vector<set<int>> G(zero_parents.size());

        int ile = pom.parent.size();
        FOR(i,0,ile){
            int pocz = lower_bound(ALL(zero_parents), zero.Find(i)) - zero_parents.begin();
            int kon = lower_bound(ALL(parents), pom.Find(i)) - parents.begin();
            G[pocz].insert(kon);
        }

        FOR(idx_kol,0,kolejka.size()){
            int i = kolejka[idx_kol];
            set<int> nowe;
            for(int x : Q[i].posib){
                for(int y : G[x]) nowe.insert(y);
            }
            Q[i].t++;
            int chce_tam = lower_bound(ALL(parents), pom.Find(Q[i].y)) - parents.begin();
            //cout << i << " chce " << chce_tam << endl;
            if (nowe.count(chce_tam)){
                Q[i].posib.clear();
                swap(kolejka[idx_kol], kolejka.back());
                kolejka.pop_back();
                //cout << "Sukces " << i << endl;
                //if (i == kolejka.size()) break;
                idx_kol--;
                tosol--;
                continue;
            }
            swap(Q[i].posib, nowe);
            //cout << i << " na kolejce ze zbiorem "; for(int xx : Q[i].posib) cout << xx <<" "; cout << endl;
        }

        swap(zero, pom);
        swap(zero_parents, parents);
    }
    //cout << "Wszyscy sukces!" << endl;
    FOR(i,0,q) cout << Q[i].t << endl;
}