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
#include <cstdio>
#include <iostream>
#include <vector>
#include <string>

using namespace std;

const int MAXN = 15010;
const int POW = 16 * 1024;
const int JMP = 120;
const int LCM = 8 * 7 * 3 * 5;

vector<string> cycle[MAXN];

bool horizontal[MAXN][9][9];
bool vertical[MAXN][9][9];
bool isVertical[LCM];


int segBounds[2 * POW + 1][2];
int jumpsLeft[2 * POW + 1][LCM];
int jumpsRight[2 * POW + 1][LCM];
int jumpsUp[2 * POW + 1][LCM];
int jumpsDown[2 * POW + 1][LCM];


bool getHorizontal(int x, int r) {
  for (int l = 2; l <= 8; l++) {
    if (horizontal[x][l][r % l]) return true;
  }
  return false;
}

bool getVertical(int y, int r) {
  for (int l = 2; l <= 8; l++) {
    if (vertical[y][l][r % l]) return true;
  }
  return false;
}


void buildJumps(int n, int m) {
  for (int r = 0; r < LCM; r++) {
    for (int x = 0; x < m; x++) {
      int rr = r;
      while (!getHorizontal(x, rr)) rr++;
      jumpsLeft[x + POW][r] = rr - r;
      jumpsRight[x + POW][r] = rr - r;
    }
    for (int y = 0; y < n; y++) {
      int rr = r;
      while (!getVertical(y, rr)) rr++;
      jumpsUp[y + POW][r] = rr - r;
      jumpsDown[y + POW][r] = rr - r;
    }
  }

  for (int i = POW - 1; i >= 1; i--) {
    for (int r = 0; r < LCM; r++) {
      int jr0 = jumpsRight[i << 1][r];
      int jr1 = jumpsRight[(i << 1) + 1][(r + jr0) % LCM];
      jumpsRight[i][r] = jr0 + jr1;

      int jl0 = jumpsLeft[(i << 1) + 1][r];
      int jl1 = jumpsLeft[i << 1][(r + jl0) % LCM];
      jumpsLeft[i][r] = jl0 + jl1;
    }
  }

  for (int i = POW - 1; i >= 1; i--) {
    for (int r = 0; r < LCM; r++) {
      int jd0 = jumpsDown[i << 1][r];
      int jd1 = jumpsDown[(i << 1) + 1][(r + jd0) % LCM];
      jumpsDown[i][r] = jd0 + jd1;

      int ju0 = jumpsUp[(i << 1) + 1][r];
      int ju1 = jumpsUp[i << 1][(r + ju0) % LCM];
      jumpsUp[i][r] = ju0 + ju1;
    }
  }


  for (int x = 0; x < POW; x++) {
    segBounds[x + POW][0] = x;
    segBounds[x + POW][1] = x + 1;
  }

  for (int x = POW - 1; x >= 1; x--) {
    segBounds[x][0] = segBounds[x << 1][0];
    segBounds[x][1] = segBounds[(x << 1) + 1][1];
  }
}


int queryRight(int t, int a, int b, int v) {
  if (a == segBounds[v][0] && b == segBounds[v][1]) {
    return t + jumpsRight[v][t % LCM];
  }
  int mid = segBounds[v << 1][1];
  if (b <= mid) {
    return queryRight(t, a, b, v << 1);
  }
  if (a >= mid) {
    return queryRight(t, a, b, (v << 1) + 1);
  }

  int t2 = queryRight(t, a, mid, v << 1);
  return queryRight(t2, mid, b, (v << 1) + 1);
}

int queryLeft(int t, int a, int b, int v) {
  if (b == segBounds[v][0] && a == segBounds[v][1]) {
    return t + jumpsLeft[v][t % LCM];
  }
  int mid = segBounds[v << 1][1];
  if (a <= mid) {
    return queryLeft(t, a, b, v << 1);
  }
  if (b >= mid) {
    return queryLeft(t, a, b, (v << 1) + 1);
  }

  int t2 = queryLeft(t, a, mid, (v << 1) + 1);
  return queryLeft(t2, mid, b, v << 1);
}

int queryDown(int t, int a, int b, int v) {
  if (a == segBounds[v][0] && b == segBounds[v][1]) {
    return t + jumpsDown[v][t % LCM];
  }
  int mid = segBounds[v << 1][1];
  if (b <= mid) {
    return queryDown(t, a, b, v << 1);
  }
  if (a >= mid) {
    return queryDown(t, a, b, (v << 1) + 1);
  }

  int t2 = queryDown(t, a, mid, v << 1);
  return queryDown(t2, mid, b, (v << 1) + 1);
}

int queryUp(int t, int a, int b, int v) {
  if (b == segBounds[v][0] && a == segBounds[v][1]) {
    return t + jumpsUp[v][t % LCM];
  }
  int mid = segBounds[v << 1][1];
  if (a <= mid) {
    return queryUp(t, a, b, v << 1);
  }
  if (b >= mid) {
    return queryUp(t, a, b, (v << 1) + 1);
  }

  int t2 = queryUp(t, a, mid, (v << 1) + 1);
  return queryUp(t2, mid, b, v << 1);
}





int main() {
  int n, m, q;
  cin >> n >> m >> q;
  for (int y = 0; y < n; y++) {
    cycle[y] = vector<string>(m);
    for (int x = 0; x < m; x++) {
      cin >> cycle[y][x];
      int l = cycle[y][x].size();
      for (int i = 0; i < l; i++) {
        if (cycle[y][x][i] == '1') {
          horizontal[x][l][i] = true;
        } else {
          vertical[y][l][i] = true;
        }
      }
    }
  }

  for (int r = 0; r < LCM; r++) {
    for (int y = 0; y < n; y++) {
      if (!getVertical(y, r)) {
        isVertical[r] = true;
      }
    }
  }

  buildJumps(n, m);


  while (q--) {
    int t, ys, xs, yt, xt;
    scanf("%d%d%d%d%d", &t, &ys, &xs, &yt, &xt);

    if (isVertical[t % LCM]) {
      if (ys < yt) {
        t = queryDown(t, ys, yt, 1);
      } else if (ys > yt) {
        t = queryUp(t, ys, yt, 1);
      }
    } else {
      if (xs < xt) {
        t = queryRight(t, xs, xt, 1);
      } else if (xs > xt) {
        t = queryLeft(t, xs, xt, 1);
      }
    }

    printf("%d\n", t);
  }
}