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
#include <bits/stdc++.h>
using namespace std;

#define REP(i, n) for (int i = 0; i < (int)n; i++)
#define ST first
#define ND second
#define MP make_pair
#define PB push_back

typedef vector<int> VI;
typedef pair<int, int> PII;
typedef vector<PII> VPII;
typedef set<int> SI;
typedef long long LL;

#ifdef DEBUG
const bool debug = true;
#else
const bool debug = false;
#endif

// #define MULTI_TASK true

const int INF = 1000 * 1000 * 1000;
const LL INFLL = 1e18;
const int MAKSN = 1000 + 13;
const LL MOD = 123456789LL;
LL N, M, k, l, a, b, c, d, q, t;
vector<LL> v;
vector<vector<string>> swiatla;
vector<vector<bool>> vis;
vector<vector<int>> dist;
string s;
vector<vector<tuple<int, int, string>>> E[MAKSN];

void zeruj() {
    // v.clear();
}

void readIn() {
    cin >> N >> M >> q;
    for (int i = 0; i <= N; i++) {
        E[i].resize(M + 1);
        vis.push_back(vector<bool>(M + 1, false));
        dist.push_back(vector<int>(M + 1, INF));
    }

    for (int i = 1; i <= N; i++) {
        // swiatla[i].push_back("");
        for (int j = 1; j <= M; j++) {
            cin >> s;
            E[i - 1][j - 1].push_back(make_tuple(i, j - 1, s));
            E[i - 1][j].push_back(make_tuple(i, j, s));

            E[i][j - 1].push_back(make_tuple(i - 1, j - 1, s));
            E[i][j].push_back(make_tuple(i - 1, j, s));
            // 0 - pozioma

            E[i - 1][j - 1].push_back(make_tuple(i - 1, j, s));
            E[i][j - 1].push_back(make_tuple(i, j, s));

            E[i - 1][j].push_back(make_tuple(i - 1, j - 1, s));
            E[i][j].push_back(make_tuple(i, j - 1, s));
            // 1 pion

            // swiatla[i].push_back(s);
        }
    }
}

void fillQWithVis(queue<pair<int, int>>& q) {
    REP(i, N + 1) {
        REP(j, M + 1) {
            if (vis[i][j]) {
                q.push({i, j});
            }
        }
    }
}

bool isPoziom(int x1, int y1, int x2, int y2) {
    return x1 == x2;
}  // poziom = 0

LL calcDist(LL t, int a, int b, int c, int d) {
    dist[a][b] = 0;
    vis[a][b] = true;
    queue<pair<int, int>> q;
    // q.push({a, b});
    int passed = 0;
    while (!vis[c][d]) {
        fillQWithVis(q);
        while (!q.empty()) {
            pair<int, int> p = q.front();
            q.pop();
            int x = p.ST, y = p.ND;

            for (int i = 0; i < E[x][y].size(); i++) {
                auto tup = E[x][y][i];
                int nx = get<0>(tup);
                int ny = get<1>(tup);
                string sw = get<2>(tup);

                if (!vis[nx][ny]) {
                    if (sw[(t + passed) % sw.size()] == '0' && isPoziom(x, y, nx, ny)) {
                        vis[nx][ny] = true;
                        dist[nx][ny] = passed;
                        q.push({nx, ny});
                    }

                    if (sw[(t + passed) % sw.size()] == '1' && !isPoziom(x, y, nx, ny)) {
                        vis[nx][ny] = true;
                        dist[nx][ny] = passed;
                        q.push({nx, ny});
                    }
                }
            }
        }

        passed++;
    }

    return dist[c][d];
}

void solve() {
    // for

    REP(i, q) {
        cin >> t >> a >> b >> c >> d;
        // vis.clear();
        // dist.clear();
        REP(ii, N+1) {
            REP(jj, M+1) {
                vis[ii][jj] = false;
                dist[ii][jj] = INF;
            }
        }
;
        cout << t + calcDist(t, a, b, c, d) << "\n";
    }
}

int main() {
    ios_base::sync_with_stdio(0);
#ifdef MULTI_TASK
    int test;
    cin >> test;
    while (test--) {
#endif
        zeruj();
        readIn();
        solve();
#ifdef MULTI_TASK
    }
#endif
    return 0;
}