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
#include<bits/stdc++.h>
#define VAR(i,n) __typeof(n) i = (n)
#define loop(i,j,s) for(int i=j;i<s;i++)
#define loopback(i,j,s) for(int i=j;i>=s;i--)
#define foreach(i,c) for(VAR(i,(c).begin());i!=(c).end();i++)
#define pln( x ) cout << x << "\n"
#define ps( x ) cout << x << " "
#define entr cout << "\n"
#define pcnt(i) __builtin_popcount(i)
#define ll long long
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define sz(c) ((int)(c).size())
#define ALL(c) (c).begin(), (c).end()
using namespace std;
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> PII;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef vector<vs> vvs;
const int INFTY=20000000;
const int MAX=500100;
const int MOD=10000000;

void coutTab(int* tab,int n){
	loop(i,0,n){
		cout<<tab[i]<<" ";
	}
	cout<<"\n";
}

template<class T> void coutVec(vector<T> tab){
	for(T t : tab){
		cout<<t<<" ";
	}
	cout<<"\n";
}
//------------------------------------------
int n,m;
int k;
vvs s;
int*** C;
vector<set<int>> G;
int nwd(int a, int b) {
    if(a<b) swap(a,b);
    if(b==0) return a;
    return nwd(b, a%b);
}

int nww(int a, int b) {
    return a*b/nwd(a,b);
}

int id=0;

bool is_in(int i, int j) {
    return i>=0 && i<=n && j >=0 && j<=m;
}

bool is_horizontal(string s, int lvl) {
    return s[lvl%sz(s)] == '1';
}

bool is_vertical(string s, int lvl) {
    return s[lvl%sz(s)] == '0';
}

void color(int i, int j, int lvl, int c) {
    // cout<<i<<" "<<j<<" "<<lvl<<endl;
    C[lvl][i][j] = c;
    if(is_in(i+1,j) && (
            ( j > 0 && is_vertical(s[i][j-1], lvl) ) || ( j < m && is_vertical(s[i][j], lvl))
        ) && C[lvl][i+1][j] == -1
    ) color(i+1, j, lvl, c);

    
    if(is_in(i-1,j) && (
            ( j > 0 && is_vertical(s[i-1][j-1], lvl) ) || ( j < m && is_vertical(s[i-1][j], lvl))
        ) && C[lvl][i-1][j] == -1
    ) color(i-1, j, lvl, c);

    if(is_in(i,j+1) && (
            ( i > 0 && is_horizontal(s[i-1][j], lvl) ) || ( i < n && is_horizontal(s[i][j], lvl))
        ) && C[lvl][i][j+1] == -1
    ) color(i, j+1, lvl, c);

    
    if(is_in(i,j-1) && (
            ( i > 0 && is_horizontal(s[i-1][j-1], lvl) ) || ( i < n && is_horizontal(s[i][j-1], lvl))
        ) && C[lvl][i][j-1] == -1
    ) color(i, j-1, lvl, c);
}

void create_lvl(int lvl) {
    loop(i,0,n+1) {
        loop(j,0,m+1) {
            if(C[lvl][i][j] == -1) {
                color(i,j,lvl,id++);
            }
        }
    }
}

void create_graph() {
    loop(l,0,k) {
        loop(i,0,n+1) {
            loop(j,0,m+1) {
                int a = C[l][i][j];
                int b = C[(l+1)%k][i][j];
                //cout<<a<<" "<<id<<endl;
                G[a].insert(b);
                //G[b].insert(a);
            }
        }
    }
}

void __printlvl(int lvl) {
    ps("----LEVEL: ");ps(lvl);pln("--------");
    loop(i,0,n+1) {
        loop(j,0,m+1) {
            ps(C[lvl][i][j]);
        }
        entr;
    }
    pln("---------------------------");
}

void __printgraph() {
    ps("----GRAPH: ");pln("--------");
    loop(i,0,id) {
        ps(i);ps(": ");
        for(int u: G[i]) {
            ps(u);
        }
        entr;
    }
    pln("---------------------------");
}

int bfs(int v, int t, int x, int y) {
    queue<int> Q;
    Q.push(v);
    map<int, int> d;
    d[v] = t;
    while(!Q.empty()) {
        int v = Q.front();
        Q.pop();
        int ct = d[v];
        for(int u : G[v]) {
            //if(d.find(u) == d.end()) {
            d[u] = ct + 1;
            Q.push(u);
            if(u == C[(ct+1)%k][x][y]) return d[u];
            //}
        }
    }
    return -1;
}

int main(){
	ios_base::sync_with_stdio(0);
    int q;
    cin>>n>>m>>q;
    s = vvs(n, vs(m));
    k = 1;
    loop(i,0,n) {
        loop(j,0,m) {
            cin>>s[i][j];
            k = nww(k, sz(s[i][j]));
        }
    }
    
    // init C
    C = new int**[k];
    loop(l,0,k) {
        C[l] = new int*[n+1];
        loop(i,0,n+1) {
            C[l][i] = new int[m+1];
            loop(j,0,m+1) {
                C[l][i][j] = -1;
            }
        }
    }
    
    // pln(k);
    // cout<<"LVLV"<<endl;

    loop(i,0,k) {
        create_lvl(i);
        //__printlvl(i);
    }

    // cout<<"GRAPH"<<endl;
    G = vector<set<int>>(id+1);
    create_graph();
    //__printgraph();
    // cout<<"QUERY"<<endl;
    int t,a,b,c,d;
    while(q--) {
        cin>>t>>a>>b>>c>>d;
        //cout<<"AFTERCIN"<<endl;
        if(C[t%k][a][b] == C[t%k][c][d]) pln(t);
        else pln(bfs(C[t%k][a][b], t, c, d));
    }

    // init C
    loop(l,0,k) {
        loop(i,0,n+1) {
            delete [] C[l][i];
        }
        delete [] C[l];
    }
    delete [] C;
}