#include <algorithm>
#include <cstring>
#include <iostream>
#include <vector>
#include <chrono>
using namespace std;
using in = int;
using pi = pair<in, in>;
vector<in> *rows;
vector<in> *columns;
bool **checked;
int *reverseRows;
int *reverseColumns;
// 127 ms
// 101 ms
inline int getRev(int x, int y) {
int revRow = reverseRows[y];
int revCol = reverseColumns[x];
int m = min(revRow, revCol);
if (m < 1'000'000) return m;
return 0;
}
template <bool Rows, bool Cols>
char find(vector<pair<pi, char>>& next, pi pos, pi end) {
int rev = getRev(pos.first, pos.second);
if (rev) return rev;
if (checked[pos.second][pos.first]) return false;
checked[pos.second][pos.first] = true;
if constexpr (Rows) {
for (in x : rows[pos.second]) {
// if (x == end.first) return 2;
if (x == pos.first) continue;
int rev = reverseColumns[pos.first];
if (rev < 1'000'000) return rev + 1;
next.push_back({{ x, pos.second }, 0});
}
}
if constexpr (Cols) {
for (in y : columns[pos.first]) {
// if (y == end.second) return 2;
if (y == pos.second) continue;
int rev = reverseRows[pos.second];
if (rev < 1'000'000) return rev + 1;
next.push_back({{ pos.first, y }, 1});
}
}
return false;
}
template <bool Rows, bool Cols>
int reverse(vector<pair<pi, char>>& next, pi pos, pi start, int depth) {
if (start.first == pos.first || start.second == pos.second) return 1;
if (checked[pos.second][pos.first]) return 0;
checked[pos.second][pos.first] = true;
if constexpr (Rows) if (depth < reverseRows[pos.second]) reverseRows[pos.second] = depth;
if constexpr (Cols) if (depth < reverseColumns[pos.first]) reverseColumns[pos.first] = depth;
if constexpr (Rows) {
for (in x : rows[pos.second]) {
if (x == start.first) return 2;
if (x == pos.first) continue;
next.push_back({{ x, pos.second }, 0});
}
}
if constexpr (Cols) {
for (in y : columns[pos.first]) {
if (y == start.second) return 2;
if (y == pos.second) continue;
next.push_back({{ pos.first, y }, 1});
}
}
return 0;
}
int searchReverse(pi end, pi start, int max) {
std::vector<pair<pi, char>> next{{end, 0}};
int depth = 1;
while (next.size() && depth <= max) {
std::vector<pair<pi, char>> copy;
copy.reserve(next.size() * 4);
// cout << "dep: " << depth << endl;
for (auto [pos, type] : next) {
int sus = depth == 1 || depth >= max ? reverse<true, true>(copy, pos, start, depth) :
(type == 1 ? reverse<true, false>(copy, pos, start, depth) : reverse<false, true>(copy, pos, start, depth));
if (sus) return depth + sus - 1;
}
next = copy;
depth++;
}
return 0;
}
int search(pi start, pi end, int height, int width) {
if (start.first == end.first && start.second == end.second) return 0;
for (int i = 0; i < height; i++)
memset((void*)checked[i], 0, width);
memset((void*)reverseRows, 127, height * sizeof(int));
memset((void*)reverseColumns, 127, width * sizeof(int));
// 1: 779 ms
// 2: 132 ms
// 3: 76.2 ms
// 4: 83.0 ms
// 5: 72.5 ms
// 6: 72.6 ms
// 7: 76.4 ms
int rev = searchReverse(end, start, 4);
// cout << endl;
// for (int i = 0; i < height; i++) {
// for (int j = 0; j < width; j++) {
// int rev = min(reverseRows[i], reverseColumns[j]);
// cout << ((rev > 1'000'000) ? 0 : rev) << " ";
// }
// cout << endl;
// }
if (rev) return rev;
std::vector<pair<pi, char>> next{{start, 0}};
int depth = 0;
for (int i = 0; i < height; i++)
memset((void*)checked[i], 0, width);
while (next.size()) {
std::vector<pair<pi, char>> copy;
copy.reserve(next.size() * 4);
// for (pi pos : next) {
// int sus = find(copy, pos, end);
// if (sus) return depth + sus;
// }
//
for (auto [pos, type] : next) {
int sus = depth == 0 ? find<true, true>(copy, pos, end) : (type == 1 ? find<true, false>(copy, pos, end) : find<false, true>(copy, pos, end));
if (sus) return depth + sus;
}
next = copy;
depth++;
}
return -1;
}
int main(int argc, char *argv[]) {
int height, width, count; cin >> height >> width >> count;
rows = new vector<in>[height];
columns = new vector<in>[width];
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
char c; cin >> c;
if (c == '.') {
rows[y].push_back(x);
columns[x].push_back(y);
}
}
}
reverseRows = new int[height];
reverseColumns = new int[width];
checked = new bool*[height];
for (int i = 0; i < height; i++) checked[i] = new bool[width];
vector<pair<pi, pi>> queries;
for (int i = 0; i < count; i++) {
int y1, x1, y2, x2; cin >> y1 >> x1 >> y2 >> x2;
queries.push_back({{ x1 - 1, y1 - 1 }, { x2 - 1, y2 - 1 }});
}
auto startTime = chrono::high_resolution_clock::now();
for (auto [start, end] : queries) {
int sus = search(start, end, height, width);
cout << sus << endl;
}
auto endTime = chrono::high_resolution_clock::now();
cout << "time: " << endTime - startTime << endl;
}