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
#include <bits/stdc++.h>
#define mp make_pair
#define fi first
#define se second

using namespace std;

int n, m, z;
const long long INF = 1e17;
map<pair<int, int>, string> s;
map<pair<int, int>, long long> res;
priority_queue<pair<long long, pair<int, int>>>q;

long long za_ile(long long t, int i, int j, int num) {
  string st = s[mp(i, j)];
  int curr = t % st.size();
  int ile = 0;
  // cout << " st[" << curr << "] = " << st[curr] << endl;
  while(st[curr] != num + '0') {
    curr++;
    ile++;
    curr %= st.size();
    // cout << " st[" << curr << "] = " << st[curr] << endl;
  }
  return ile;
}

long long dijkstra(long long t, int pi, int pj, int ki, int kj) {
  while(!q.empty()) {
    q.pop();
  }
  res.clear();

  q.push(mp(-t, mp(pi, pj)));
  res[mp(pi, pj)] = t;

  while(!q.empty()) {
    long long t = -q.top().fi;
    int i = q.top().se.fi;
    int j = q.top().se.se;
    q.pop();
    // cout << i << " " << j << " t " << t << endl;

    if (i == ki && j == kj) {
      return t;
    }

    if (res[mp(i, j)] == t) {
      if (i > 0 && (res.find(mp(i - 1, j)) == res.end() || res[mp(i - 1, j)] > t)) {
        long long odl = INF;
        if (j > 0) {
          odl = za_ile(t, i, j, 0);
        }
        if (j + 1 <= m) {
          odl = min(odl, za_ile(t, i, j + 1, 0));
        }
        // cout << "     " << i - 1 << " " << j << endl;
        // cout << "     " << t + odl << endl;
        if (res.find(mp(i - 1, j)) == res.end() || t + odl < res[mp(i - 1, j)]) {
          res[mp(i - 1, j)] = t + odl;
          q.push(mp(-t - odl, mp(i - 1, j)));
          // cout <<  "     OK" << endl;
        }
      }

      if (i + 1 <= n && (res.find(mp(i + 1, j)) == res.end() || res[mp(i + 1, j)] > t)) {
        long long odl = INF;
        if (j > 0) {
          odl = za_ile(t, i + 1, j, 0);
        }
        if (j + 1 <= m) {
          odl = min(odl, za_ile(t, i + 1, j + 1, 0));
        }
        if (res.find(mp(i + 1, j)) == res.end() || t + odl < res[mp(i + 1, j)]) {
          res[mp(i + 1, j)] = t + odl;
          q.push(mp(-t - odl, mp(i + 1, j)));
        }
      }

    //   if (j > 0) {
    //     cout << "     res[" << i << "][" << j - 1 << "]";
    //     if ((res.find(mp(i, j - 1)) != res.end())) {
    //       cout << " " << res[mp(i, j - 1)];
    //     } else {
    //       cout << " INF";
    //     }
    //    cout << endl;
    //  }
      if (j > 0 && (res.find(mp(i, j - 1)) == res.end() || res[mp(i, j - 1)] > t)) {
        long long odl = INF;
        if (i > 0) {
          odl = za_ile(t, i, j, 1);
        }
        if (i + 1 <= n) {
          odl = min(odl, za_ile(t, i + 1, j, 1));
        }
        // cout << " dla " << i << " " << j - 1 << " odl " << odl << endl;
        if (res.find(mp(i, j - 1)) == res.end() || t + odl < res[mp(i, j - 1)]) {
          res[mp(i, j - 1)] = t + odl;
          q.push(mp(-t - odl, mp(i, j - 1)));
        }
      }

      // cout << "     res[" << i << "][" << j + 1 << "]";
    //   if ((res.find(mp(i, j + 1)) != res.end())) {
    //     cout << " " << res[mp(i, j + 1)];
    //   } else {
    //     cout << " INF";
    //   }
    //  cout << endl;
      if (j + 1 <= m && (res.find(mp(i, j + 1)) == res.end() || res[mp(i, j + 1)] > t)) {
        long long odl = INF;
        if (i > 0) {
          odl = za_ile(t, i, j + 1, 1);
        }
        if (i + 1 <= n) {
          odl = min(odl, za_ile(t, i + 1, j + 1, 1));
        }
        // cout << "     " << j + 1 << " " << j << endl;
        // cout << "     " << t + odl << endl;
        if (res.find(mp(i, j + 1)) == res.end() || t + odl < res[mp(i, j + 1)]) {
          res[mp(i, j + 1)] = t + odl;
          q.push(mp(-t - odl, mp(i, j + 1)));
          // cout <<  "     OK" << endl;
        }
      }
    }
  }
  return -1;
}

int main()
{
  ios_base::sync_with_stdio(0);

  cin >> n >> m >> z;

  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      cin >> s[mp(i, j)]; // TODO spamietac najblizsze jedynki i zera
    }
  }

  int t, pi, pj, ki, kj;
  while(z--) {
    cin >> t >> pi >> pj >> ki >> kj;
    cout << dijkstra(t, pi, pj, ki, kj) << endl;
  }

  return 0;
}