/**
* author: mamion
* created: Tuesday 2024-10-29
**/
#include<bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include<cpp-dump-main/cpp-dump.hpp>
#define debug(...) cpp_dump(__VA_ARGS__)
CPP_DUMP_SET_OPTION_GLOBAL(max_line_width, 100);
CPP_DUMP_SET_OPTION_GLOBAL(log_label_func, cpp_dump::log_label::filename());
CPP_DUMP_SET_OPTION_GLOBAL(enable_asterisk, true);
#else
#define debug(...)
#endif // LOCAL
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
struct dsu {
vector<int> lab;
dsu(int n) {
lab.resize(n + 2, -1);
}
int find(int u) {
return lab[u] < 0 ? u : lab[u] = find(lab[u]);
}
int join(int u, int v) {
u = find(u); v = find(v);
if (u == v) return false;
lab[u] += lab[v];
lab[v] = u;
return true;
}
};
const int N = 2210;
int n, m, k;
char c[N][N];
int sx, sy, fx, fy;
int d[N][N];
vector<dsu> trai, tren, phai, duoi;
void remove4dir(int x, int y) {
if (x <= n) tren[y].join(x + 1, x);
if (x >= 1) duoi[y].join(x - 1, x);
if (y <= m) phai[x].join(y + 1, y);
if (y >= 1) trai[x].join(y - 1, y);
}
void solve() {
cin >> n >> m >> k;
for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++)
cin >> c[i][j];
cin >> sx >> sy >> fx >> fy;
trai = phai = vector<dsu>(n + 2, dsu(m));
tren = duoi = vector<dsu>(m + 2, dsu(n));
queue<pair<int, int>> q; q.push({sx, sy}); remove4dir(sx, sy);
while (q.size()) {
int u = q.front().first, v = q.front().second; q.pop();
if (u == fx && v == fy) {
cout << d[u][v];
return;
}
// di len
while (true) {
int x = tren[v].find(u), y = v;
if (x > n || x - u > k || c[x][y] == '#') break;
remove4dir(x, y);
q.push({x, y});
d[x][y] = d[u][v] + 1;
if (x == fx && y == fy) {
cout << d[x][y];
return;
}
}
// di xuong
while (true) {
int x = duoi[v].find(u), y = v;
if (x < 1 || u - x > k || c[x][y] == '#') break;
remove4dir(x, y);
q.push({x, y});
d[x][y] = d[u][v] + 1;
if (x == fx && y == fy) {
cout << d[x][y];
return;
}
}
// di phai
while (true) {
int x = u, y = phai[u].find(v);
if (y > m || y - v > k || c[x][y] == '#') break;
remove4dir(x, y);
q.push({x, y});
d[x][y] = d[u][v] + 1;
if (x == fx && y == fy) {
cout << d[x][y];
return;
}
}
// di trai
while (true) {
int x = u, y = trai[u].find(v);
if (y < 1 || v - y > k || c[x][y] == '#') break;
remove4dir(x, y);
q.push({x, y});
d[x][y] = d[u][v] + 1;
if (x == fx && y == fy) {
cout << d[x][y];
return;
}
}
}
cout << -1;
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#ifdef LOCAL
freopen("main.inp", "r", stdin);
freopen("main.out", "w", stdout);
#else
#define file "flash"
if (fopen(file".inp", "r")) {
freopen(file".inp", "r", stdin);
freopen(file".out", "w", stdout);
}
#endif // LOCAL
int T; T = 1; if (0) cin >> T;
for (int i = 1; i <= T; i++)
{
solve();
}
}
LyoqCiAqICAgIGF1dGhvcjogIG1hbWlvbgogKiAgICBjcmVhdGVkOiBUdWVzZGF5IDIwMjQtMTAtMjkKKiovCgojaW5jbHVkZTxiaXRzL3N0ZGMrKy5oPgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKI2lmZGVmIExPQ0FMCiNpbmNsdWRlPGNwcC1kdW1wLW1haW4vY3BwLWR1bXAuaHBwPgojZGVmaW5lIGRlYnVnKC4uLikgY3BwX2R1bXAoX19WQV9BUkdTX18pCkNQUF9EVU1QX1NFVF9PUFRJT05fR0xPQkFMKG1heF9saW5lX3dpZHRoLCAxMDApOwpDUFBfRFVNUF9TRVRfT1BUSU9OX0dMT0JBTChsb2dfbGFiZWxfZnVuYywgY3BwX2R1bXA6OmxvZ19sYWJlbDo6ZmlsZW5hbWUoKSk7CkNQUF9EVU1QX1NFVF9PUFRJT05fR0xPQkFMKGVuYWJsZV9hc3RlcmlzaywgdHJ1ZSk7CiNlbHNlCiNkZWZpbmUgZGVid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uOyBpKyspIGZvciAoaW50IGogPSAxOyBqIDw9IG07IGorKykKICAgICAgICBjaW4gPj4gY1tpXVtqXTsKICAgIGNpbiA+PiBzeCA+PiBzeSA+PiBmeCA+PiBmeTsKICAgIHRyYWkgPSBwaGFpID0gdmVjdG9yPGRzdT4obiArIDIsIGRzdShtKSk7CiAgICB0cmVuID0gZHVvaSA9IHZlY3Rvcjxkc3U+KG0gKyAyLCBkc3UobikpOwogICAgcXVldWU8cGFpcjxpbnQsIGludD4+IHE7IHEucHVzaCh7c3gsIHN5fSk7IHJlbW92ZTRkaXIoc3gsIHN5KTsKICAgIHdoaWxlIChxLnNpemUoKSkgewogICAgICAgIGludCB1ID0gcS5mcm9udCgpLmZpcnN0LCB2ID0gcS5mcm9udCgpLnNlY29uZDsgcS5wb3AoKTsKICAgICAgICBpZiAodSA9PSBmeCAmJiB2ID09IGZ5KSB7CiAgICAgICAgICAgIGNvdXQgPDwgZFt1XVt2XTsKICAgICAgICAgICAgcmV0dXJuOwogICAgICAgIH0KICAgICAgICAvLyBkaSBsZW4KICAgICAgICB3aGlsZSAodHJ1ZSkgewogICAgICAgICAgICBpbnQgeCA9IHRyZW5bdl0uZmluZCh1KSwgeSA9IHY7CiAgICAgICAgICAgIGlmICh4ID4gbiB8fCB4IC0gdSA+IGsgfHwgY1t4XVt5XSA9PSAnIycpIGJyZWFrOwogICAgICAgICAgICByZW1vdmU0ZGlyKHgsIHkpOwogICAgICAgICAgICBxLnB1c2goe3gsIHl9KTsKICAgICAgICAgICAgZFt4XVt5XSA9IGRbdV1bdl0gKyAxOwogICAgICAgICAgICBpZiAoeCA9PSBmeCAmJiB5ID09IGZ5KSB7CiAgICAgICAgICAgICAgICBjb3V0IDw8IGRbeF1beV07CiAgICAgICAgICAgICAgICByZXR1cm47CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgLy8gZGkgeHVvbmcKICAgICAgICB3aGlsZSAodHJ1ZSkgewogICAgICAgICAgICBpbnQgeCA9IGR1b2lbdl0uZmluZCh1KSwgeSA9IHY7CiAgICAgICAgICAgIGlmICh4IDwgMSB8fCB1IC0geCA+IGsgfHwgY1t4XVt5XSA9PSAnIycpIGJyZWFrOwogICAgICAgICAgICByZW1vdmU0ZGlyKHgsIHkpOwogICAgICAgICAgICBxLnB1c2goe3gsIHl9KTsKICAgICAgICAgICAgZFt4XVt5XSA9IGRbdV1bdl0gKyAxOwogICAgICAgICAgICBpZiAoeCA9PSBmeCAmJiB5ID09IGZ5KSB7CiAgICAgICAgICAgICAgICBjb3V0IDw8IGRbeF1beV07CiAgICAgICAgICAgICAgICByZXR1cm47CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgLy8gZGkgcGhhaQogICAgICAgIHdoaWxlICh0cnVlKSB7CiAgICAgICAgICAgIGludCB4ID0gdSwgeSA9IHBoYWlbdV0uZmluZCh2KTsKICAgICAgICAgICAgaWYgKHkgPiBtIHx8IHkgLSB2ID4gayB8fCBjW3hdW3ldID09ICcjJykgYnJlYWs7CiAgICAgICAgICAgIHJlbW92ZTRkaXIoeCwgeSk7CiAgICAgICAgICAgIHEucHVzaCh7eCwgeX0pOwogICAgICAgICAgICBkW3hdW3ldID0gZFt1XVt2XSArIDE7CiAgICAgICAgICAgIGlmICh4ID09IGZ4ICYmIHkgPT0gZnkpIHsKICAgICAgICAgICAgICAgIGNvdXQgPDwgZFt4XVt5XTsKICAgICAgICAgICAgICAgIHJldHVybjsKICAgICAgICAgICAgfQogICAgICAgIH0KICAgICAgICAvLyBkaSB0cmFpCiAgICAgICAgd2hpbGUgKHRydWUpIHsKICAgICAgICAgICAgaW50IHggPSB1LCB5ID0gdHJhaVt1XS5maW5kKHYpOwogICAgICAgICAgICBpZiAoeSA8IDEgfHwgdiAtIHkgPiBrIHx8IGNbeF1beV0gPT0gJyMnKSBicmVhazsKICAgICAgICAgICAgcmVtb3ZlNGRpcih4LCB5KTsKICAgICAgICAgICAgcS5wdXNoKHt4LCB5fSk7CiAgICAgICAgICAgIGRbeF1beV0gPSBkW3VdW3ZdICsgMTsKICAgICAgICAgICAgaWYgKHggPT0gZnggJiYgeSA9PSBmeSkgewogICAgICAgICAgICAgICAgY291dCA8PCBkW3hdW3ldOwogICAgICAgICAgICAgICAgcmV0dXJuOwogICAgICAgICAgICB9CiAgICAgICAgfQogICAgfQogICAgY291dCA8PCAtMTsKfQoKaW50IG1haW4oKSB7CiAgICBpb3NfYmFzZTo6c3luY193aXRoX3N0ZGlvKDApOyBjaW4udGllKDApOyBjb3V0LnRpZSgwKTsKCiNpZmRlZiBMT0NBTAogICAgZnJlb3BlbigibWFpbi5pbnAiLCAiciIsIHN0ZGluKTsKICAgIGZyZW9wZW4oIm1haW4ub3V0IiwgInciLCBzdGRvdXQpOwojZWxzZQogICAgI2RlZmluZSBmaWxlICJmbGFzaCIKICAgIGlmIChmb3BlbihmaWxlIi5pbnAiLCAiciIpKSB7CiAgICAgICAgZnJlb3BlbihmaWxlIi5pbnAiLCAiciIsIHN0ZGluKTsKICAgICAgICBmcmVvcGVuKGZpbGUiLm91dCIsICJ3Iiwgc3Rkb3V0KTsKICAgIH0KI2VuZGlmIC8vIExPQ0FMCgogICAgaW50IFQ7IFQgPSAxOyBpZiAoMCkgY2luID4+IFQ7CiAgICBmb3IgKGludCBpID0gMTsgaSA8PSBUOyBpKyspCiAgICB7CiAgICAgICAgc29sdmUoKTsKICAgIH0KfQo=