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
#include <bits/stdc++.h>
using namespace std;
 
typedef unsigned long long ull;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> PII;
typedef pair<ll, int> PILL;
typedef pair<ll, ll> PLL;
 
const int MAX_N = 1e6+5;
const int M = 3e4+5;
const ll INF = (ll)(1e16);
const int inf = 1e9;
const ll MOD = 1000000007LL;

int n, m, queries;
vector<vector<string>> city;

string normalize(string s) {
	string res = s;
	int pos = 0;
	while (res.size() < 8) {
		res += s[pos];
		pos++;
		pos %= 8;
	}
	return res;
}

int cell_to_number(int row, int col) {
	return row * (m + 1) + col + 1;
}

bool inside(int row, int col) {
	return row >= 0 && row <= n && col >= 0 && col <= m;
}

bool inside_crossings(int row, int col) {
	return row >= 0 && row < n && col >= 0 && col < m;
}

int get(int row, int col, int now, char wanted) {
	int len = city[row][col].size();
	now %= len;
	int res = 0;
	while (city[row][col][now] != wanted) {
		res++;
		now++;
		now %= len;
	}
	return res;
}

void dijkstra(int a, int b, int c, int d, int t) {
	vector<vector<int>> dist(n + 1, vector<int>(m + 1, inf));
	dist[a][b] = 0;
	set<pair<int, PII>> Q;
	Q.insert(make_pair(0, make_pair(a, b)));
	
	while (!Q.empty()) {
		pair<int, PII> best = *Q.begin();
		int row = best.second.first;
		int col = best.second.second;
		int now = t + dist[row][col];
		
		//cout << "NOW: " << now << '\n';
		//cout << row << ", " << col << ": " << dist[row][col] << '\n';
		
		if (row == c && col == d) {
			break;
		}
		Q.erase(Q.begin());
		
		
		// up
		if (inside(row - 1, col)) {
			int cost = 8;
			if (inside_crossings(row-1, col-1)) {
				cost = min(cost, get(row-1, col-1, now, '0'));
			}
			if (inside_crossings(row-1, col)) {
				cost = min(cost, get(row-1, col, now, '0'));
			}
			
			int tmp = dist[row][col] + cost;
			if (tmp < dist[row-1][col]) {
				dist[row-1][col] = tmp;
				Q.insert(make_pair(tmp, make_pair(row-1, col)));
			}
		}
		
		// down
		if (inside(row + 1, col)) {
			int cost = 8;
			if (inside_crossings(row, col)) {
				cost = min(cost, get(row, col, now, '0'));
			}
			if (inside_crossings(row, col-1)) {
				cost = min(cost, get(row, col-1, now, '0'));
			}
			
			int tmp = dist[row][col] + cost;
			if (tmp < dist[row+1][col]) {
				dist[row+1][col] = tmp;
				Q.insert(make_pair(tmp, make_pair(row+1, col)));
			}
		}
		
		// left
		if (inside(row, col - 1)) {
			int cost = 8;
			if (inside_crossings(row-1, col-1)) {
				cost = min(cost, get(row-1, col-1, now, '1'));
			}
			if (inside_crossings(row, col-1)) {
				cost = min(cost, get(row, col-1, now, '1'));
			}
			
			int tmp = dist[row][col] + cost;
			if (tmp < dist[row][col-1]) {
				dist[row][col-1] = tmp;
				Q.insert(make_pair(tmp, make_pair(row, col-1)));
			}
		}
		
		// right
		if (inside(row, col + 1)) {
			int cost = 8;
			if (inside_crossings(row-1, col)) {
				cost = min(cost, get(row-1, col, now, '1'));
			}
			if (inside_crossings(row, col)) {
				cost = min(cost, get(row, col, now, '1'));
			}
			
			int tmp = dist[row][col] + cost;
			if (tmp < dist[row][col+1]) {
				dist[row][col+1] = tmp;
				Q.insert(make_pair(tmp, make_pair(row, col+1)));
			}
		}
	}
	
	/*for (int i = 0; i <= n; i++) {
		for (int j = 0; j <= m; j++) {
			cout << dist[i][j] << ' ';
		}
		cout << '\n';
	}
	cout << '\n';*/
	
	cout << t + dist[c][d] << '\n';
}
 
int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
			
	cin >> n >> m >> queries;
	city.resize(n, vector<string>(m));
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			cin >> city[i][j];
		}
	}
	
	for (int qq = 0; qq < queries; qq++) {
		int a, b, c, d;
		int t;
		cin >> t >> a >> b >> c >> d;
		
		dijkstra(a, b, c, d, t);
	}
	

	
    return 0;
}