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
#include <iostream>
#include <vector>
#include <unordered_set>
#include <queue>
#include <cstring>

int nwd(int a, int b) {
  return a==0?b:nwd(b%a,a);
}

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

const int MAXMN = 1000000+9;
// const int MAXT = 840; // NWW(2,3,...,8)
int MAXT = 840;
const int INF = 1000000000+9;

char S[MAXMN][9];
uint8_t L[MAXMN];
std::vector<int> M[MAXMN];

std::vector<std::unordered_set<int>> G;
std::vector<std::unordered_set<int>> I;
std::vector<bool> B;

int n,m,q;

inline int index2(int x, int y) {
  if (x < 0 || y<0 || x>=m || y >=n) return MAXMN-1;
  return y*m+x;
}

inline int index(int x, int y) {
  return y*(m+1)+x;
}

inline std::tuple<int,int> next(int x, int y, int i) {
  if (i==0) return {x+1, y};
  else if (i==1) return {x-1,y};
  else if (i==2) return {x,y+1};
  else if (i==3) return {x,y-1};
  else return {-1, -1};
}

inline bool green(int x, int y, int t, int i) {
  if (i==0) {
    return S[index2(x,y-1)][t%L[index2(x,y-1)]] == '1' || S[index2(x,y)][t%L[index2(x,y)]] == '1';
  } else if (i==1) {
    return S[index2(x-1,y-1)][t%L[index2(x-1,y-1)]] == '1' || S[index2(x-1,y)][t%L[index2(x-1,y)]] == '1';
  } else if (i==2) {
    return S[index2(x-1,y)][t%L[index2(x-1,y)]] == '0' || S[index2(x,y)][t%L[index2(x,y)]] == '0';
  } else if (i==3) {
    return S[index2(x-1,y-1)][t%L[index2(x-1,y-1)]] == '0' || S[index2(x,y-1)][t%L[index2(x,y-1)]] == '0';
  } else return false;
}

inline bool ok(int x, int y) {
  return x >= 0 && x <= m && y >= 0 && y <= n;
}

void BFS1(int x, int y, int t, int id) {
  std::queue<std::tuple<int, int>> Q;
  Q.push({x,y});
  M[index(x,y)][t] = id;
  I[id].insert(index(x,y));
  while (!Q.empty()) {
    auto [x, y] = Q.front();
    Q.pop();
    for (int i=0;i<4;++i) {
      auto [dx, dy] = next(x,y,i);
      if (ok(dx,dy) && M[index(dx,dy)][t] == 0 && green(x,y,t,i)) {
        Q.push({dx,dy});
        M[index(dx,dy)][t] = id;
        I[id].insert(index(dx,dy));
      }
    }
  }
}

void BuildG() {
  G.push_back({});
  I.push_back({});
  for (int y=0;y<=n;++y)
    for (int x=0;x<=m;++x)
      M[index(x,y)] = std::vector<int>(MAXT, 0);
  for (int t=0;t<MAXT;++t)
    for (int y=0;y<=n;++y)
      for (int x=0;x<=m;++x)
        if (M[index(x,y)][t] == 0) {
          int id = G.size();
          G.push_back({});
          I.push_back({});
          BFS1(x,y,t,id);
        }

  for (int t=0;t<MAXT-1;++t)
    for (int y=0;y<=n;++y)
      for (int x=0;x<=m;++x)
        G[M[index(x,y)][t]].insert(M[index(x,y)][t+1]);
  for (int y=0;y<=n;++y)
    for (int x=0;x<=m;++x)
      G[M[index(x,y)][MAXT-1]].insert(M[index(x,y)][0]);
}

int BFS(int x, int y, int tx, int ty, int t) {
  int ti = index(tx,ty);
  std::queue<int> Q;
  int v = M[index(x,y)][t%MAXT];
  if (I[v].count(ti)) return t;
  Q.push(v);
  std::vector<int> D(G.size(), INF);
  D[v] = 0;
  while (!Q.empty()) {
    v = Q.front();
    Q.pop();
    for (int w : G[v]) {
      if (D[w] == INF) {
        D[w] = D[v]+1;
        if (I[w].count(ti)) return t + D[w];
        Q.push(w);
      }
    }
  }
  return t;
}

int main() {
  std::ios_base::sync_with_stdio(0);
  std::cin >> n >> m >> q;
  strcpy(S[MAXMN-1], "$$$$$$$$");
  L[MAXMN-1] = 8;
  MAXT = 2;
  for (int y=0;y<n;++y)
    for (int x=0;x<m;++x)
    {
      auto& s = S[index2(x,y)];
      std::cin >> s;
      int l = L[index2(x,y)] = strlen(s);
      MAXT = nww(MAXT, l);
    }
  BuildG();

  for (int i=0;i<q;++i) {
    int sx, sy, tx, ty, t;
    std::cin >> t >> sy >> sx >> ty >> tx;
    std::cout << BFS(sx, sy, tx, ty, t) << "\n";
  }

  return 0;
}