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
#include <cstdio>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <queue>
#include <list>
#include <vector>
#include <map>
#include <unordered_set>
#include <string>
#include <cstring>
#include <cassert>
#include <limits.h>

using namespace std;

typedef long long LL;
typedef long double LD;

typedef vector<int> VI;
typedef pair<int,int> PII;
#define REP(i,n) for(int i=0;i<(n);i++)
#define FOR(i,a,b) for(VAR(i,a);i<=(b);++i)
#define FORD(i,a,b) for(VAR(i,a);i>=(b);--i)
#define FORE(a,b) for(VAR(a,(b).begin());a!=(b).end();++a)
#define VAR(a,b) __typeof(b) a=(b)
#define SIZE(a) ((int)((a).size()))
#define ALL(x) (x).begin(),(x).end()
#define CLR(x,a) memset(x,a,sizeof(x))
#define ZERO(x) memset(x,0,sizeof(x))
#define S(t,i) scanf("%" ## t, &i)
#define SI(i) scanf("%d", &i)

const int MAXN=101*1001+5;
const int M=840;

int n, m, q;
int t, a, b, c, d;
char s[MAXN][8];
char slen[MAXN];
char visited[MAXN];
short int vid[MAXN][M];
int px[2][2]={{1,-1},{0,0}};
int py[2][2]={{0,0},{-1,1}};
int cx[2][2][2]={{{1,1},{0,0}},{{0,1},{0,1}}};
int cy[2][2][2]={{{0,1},{0,1}},{{0,0},{1,1}}};
queue<pair<int, int>> qu;
unordered_set<int> addedEdges(16);
vector<list<int>> edges;
vector<int> visitedVertex;

int getCrossingId(int y, int x) {
    return y*(m+1) + x;
}
inline bool isGreen(int y, int x, int currentTime, int direction) {
    if (y<1 || x<1 || y>n || x>m) {
        return false;
    }
    int cid=getCrossingId(y,x);
    currentTime %= slen[cid];
    return s[cid][currentTime] == direction;
}

void addEdge(int v1, int v2) {
    int edgeHash=v1*10007+v2;
    if (addedEdges.find(edgeHash) == addedEdges.end()) {
        addedEdges.insert(edgeHash);
        edges[v1].push_back(v2);
    }
}

int vertexId=0;
void bfs(int ni, int mi, int currentTime) {
    addedEdges.clear();
    list<int> vertexEdges;
    edges.push_back(vertexEdges);
    visitedVertex.push_back(0);
    qu.push(make_pair(ni,mi));
    visited[getCrossingId(ni,mi)] = (currentTime+1)%2;
    while (!qu.empty()) {
        pair<int, int> p = qu.front();
        qu.pop();
        int currentId = getCrossingId(p.first,p.second);
        vid[currentId][currentTime]=vertexId;
        if (currentTime > 0) {
            addEdge(vid[currentId][currentTime-1], vid[currentId][currentTime]);
        }
        REP(direction,2) {
            REP(pi,2) {
                ni = p.first + py[direction][pi];
                mi = p.second + px[direction][pi];
                int cid = getCrossingId(ni,mi);
                if (visited[cid] != (currentTime+1)%2
                    && ni >=0 && ni <= n && mi >=0 && mi <= m &&
                    (isGreen(p.first + cy[direction][pi][0], p.second + cx[direction][pi][0], currentTime, direction) ||
                     isGreen(p.first + cy[direction][pi][1], p.second + cx[direction][pi][1], currentTime, direction))) {
                    qu.push(make_pair(ni, mi));
                    visited[cid] = (currentTime+1)%2;
                }
            }
        }
    }
        vertexId++;
}

int vertexForPlace(int y, int x, int currentTime) {
    return vid[getCrossingId(y, x)][currentTime%M];
}

int main() {
    ios_base::sync_with_stdio(0);
    
    SI(n);
    SI(m);
    SI(q);
    FOR(ni, 1, n) {
        FOR(mi, 1, m) {
            int cid = getCrossingId(ni, mi);
            scanf(" ");
            char &i = slen[cid];
            for (i=0; i<8; i++) {
                scanf("%c", &s[cid][i]);
                if (s[cid][i] != '1' && s[cid][i] != '0') {
                    break;
                }
                s[cid][i]-='0';
                s[cid][i]=(s[cid][i]+1)%2;
            }
        }
    }
    REP(i, M) {
//        if (i<4) printf("%d\n", i);
        FOR(ni, 0, n) {
            FOR(mi, 0, m) {
                int cid = getCrossingId(ni,mi);
                if (visited[cid] != (i+1)%2) {
                    bfs(ni, mi, i);
                }
//                if (i<4)  printf("%d ", vid[cid][i]);
            }
//            if (i<4) printf("\n");
        }
    }
    
    addedEdges.clear();
    FOR(ni, 0, n) {
        FOR(mi, 0, m) {
            int cid = getCrossingId(ni,mi);
            addEdge(vid[cid][M-1], vid[cid][0]);
        }
    }
    
    FOR(i, 1, q) {
        SI(t);
        SI(a);
        SI(b);
        SI(c);
        SI(d);
        queue<pair<int, int>> q;
        q.push(make_pair(vertexForPlace(a, b, t),t));
        while (!q.empty()) {
            pair<int, int> p = q.front();
            q.pop();
            if (p.first == vertexForPlace(c, d, p.second)) {
                printf("%d\n", p.second);
                break;
            }
            FORE(vi, edges[p.first]) {
                if (visitedVertex[*vi]!=i) {
                    q.push(make_pair(*vi, p.second+1));
                    visitedVertex[*vi]=i;
                }
            }
        }
        
        
    }
    
    
    return 0;
}