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
#include <cstdio>
#include <vector>
#include <string>
#include <bitset>
#include <cstring>
#include <algorithm>

using namespace std;

typedef long long LL;

const int A = 2;
const int B = 9;
const int M = 840;
const int K = 20;

vector<vector<bool>> aggregate(const vector<vector<unsigned int>>& dim, int n) {
  vector<vector<bool>> res(n, vector<bool>(M));
  for (int i=0; i<n; ++i) {
    for (int j=0; j<M; ++j) {
      bool step = 0;
      for (int k=A; k<B; ++k) {
        step |= dim[i][k] & (1 << (j % k));
      }
      res[i][j] = step;
    }
  }
  return res;
}

vector<vector<int>> zip(const vector<vector<bool>>& dim, int n) {
  vector<vector<int>> res(n, vector<int>(M));
  for (int j=0; j<M; ++j) {
    int l = n;
    for (int i=n-1; i>=0; --i) {
      if (!dim[i][j]) {
        l = i;
      }
      res[i][j] = l;
    }
  }
  return res;
}


typedef vector<vector<vector<int>>> Con;
Con concatenate(
  const vector<vector<int>>& dim, int n) {
  Con res;
  res.push_back(dim); 
  for (int j=0; j<M; ++j) {
    res[0].push_back(vector<int>(M, n));
  }
  int step = 1;
  for (int k=0; k<K; ++k) {
    res.push_back(vector<vector<int>>(n, vector<int>(M)));
    for (int i=0; i<n; ++i) {
      for (int j=0; j<M; ++j) {
        int t = res[k][i][j];
        if (t < n) {
          t = res[k][t][(j + step) % M];
        }
        res[k+1][i][j] = t;
      }
    }
    step *= 2;
  }
  return res;
}

int dist(int a, int b, int t, const Con& con, int n) {
  int step = 1;
  int k = 0;
  while (con[k][a][t] < b) {
    ++k;
    step *= 2;
  }
  
  int sum = 0;
  while (step > 1) {
    --k;
    step /= 2;
    if (con[k][a][t] < b) {
      sum += step;
      a = con[k][a][t];
      t = (t + step) % M;
    }
  }
  return sum;
}

int main() {
  int n, m, q; scanf("%d %d %d", &n, &m, &q);

  vector<vector<unsigned int>> rows(n, vector<unsigned int>(B, 0)), cols(m, vector<unsigned int>(B, 0));
  for (int i=0; i<n; ++i) {
    for (int j=0; j<m; ++j) {
      char s[10]; scanf(" %s", &s);
      int l = strlen(s);
      reverse(s, s+l);
      cols[j][l] |= (unsigned int)stoi(s, 0, 2);
      for (int i=0; i<l; ++i) s[i] = '1' + '0' - s[i];
      rows[i][l] |= (unsigned int)stoi(s, 0, 2);;
    }
  }

  auto ragg = aggregate(rows, n);
  auto cagg = aggregate(cols, m);

  auto raggr = ragg; reverse(raggr.begin(), raggr.end());
  auto caggr = cagg; reverse(caggr.begin(), caggr.end());

  auto rzip = zip(ragg, n);
  auto czip = zip(cagg, m);
  auto rzipr = zip(raggr, n);
  auto czipr = zip(caggr, m);
  auto rcon = concatenate(rzip, n);
  auto ccon = concatenate(czip, m);
  auto rconr = concatenate(rzipr, n);
  auto cconr = concatenate(czipr, m);

  for (int i=0; i<q; ++i) {
    int x, y, xx, yy, t; scanf("%d %d %d %d %d", &t, &x, &y, &xx, &yy);
    auto rcon_ref = &rcon;
    auto ccon_ref = &ccon;
    if (x > xx) {
      x = n-x;
      xx = n-xx;
      rcon_ref = &rconr;
    }
    if (y > yy) {
      y = m-y;
      yy = m-yy;
      ccon_ref = &cconr;
    }

    if ((*rcon_ref)[0][x][(t % M)] < xx) {
      printf("%d\n", t + dist(x, xx, (t % M), *rcon_ref, n));
    } else if ((*ccon_ref)[0][y][(t % M)] < yy) {
      printf("%d\n", t + dist(y, yy, (t % M), *ccon_ref, m));
    } else {
      printf("%d\n", t);
    }
  }
  return 0;
}