#include <bits/stdc++.h>
#include <stdio.h>
#define __Shibae__ signed main()
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define fiopen(Path) freopen(Path".INP", "r", stdin); freopen(Path".OUT", "w", stdout);
#define fipen(Path) freopen(Path".INP", "r", stdin);
#define sz(s) (int)s.size()
#define all(x) x.begin(), x.end()
#define maxHeap priority_queue<int>
#define minHeap priority_queue<int, vector<int>, greater<int>>
#define getBit(x, k) (((x) >> (k)) & 1)
#define MASK(i) (1LL << (i))
#define SQR(x) (1LL * ((x) * (x)))
#define db double
#define ld long double
#define ui unsigned int
#define ll long long
#define ii pair<int, int>
#define pli pair<ll, int>
#define pil pair<int, ll>
#define pll pair<ll, ll>
#define fi first
#define se second
#define FOR(i, a, b) for(int i = a, _b = b; i <= _b; i += 1)
#define FOD(i, a, b) for(int i = a, _b = b; i >= _b; i -= 1)
#define REP(i, a) for(int i = 0, _a = a; i < _a; i++)
#define pb push_back
#define fau(u, a) for(auto &u : a)
using namespace std;
const ll mod = 1e9 + 7;
const int INF = 1e9 + 7;
const ll INFLL = (ll)2e18 + 7LL;
const ld PI = acos(-1);
const int MAX = 2210;
const int dx[] = {1, -1, 0, 0, -1, 1, 1, -1};
const int dy[] = {0, 0, 1, -1, -1, -1, 1, 1};
mt19937 rd(chrono::steady_clock::now().time_since_epoch().count());
ll Rand(ll l, ll r)
{
return l + rd() % (r - l + 1);
}
template<class SHIBA, class ENGINE>
bool minimize(SHIBA &x, const ENGINE y)
{
if(x > y)
{
x = y;
return true;
}
else return false;
}
template<class SHIBA, class ENGINE>
bool maximize(SHIBA &x, const ENGINE y)
{
if(x < y)
{
x = y;
return true;
}
else return false;
}
/* Template by: Nguyen Nhat Anh from Luong Van Chanh High School for the gifted */
/* From Min Tuoi with love */
/** TRY HARD **/
/** ORZ **/
/* -----------------[ MAIN CODE ]----------------- */
int n, m, k;
int a[MAX][MAX];
int f[4][MAX][MAX];
int xs, ys, xt, yt;
int get(int x)
{
return x / k + (x % k != 0);
}
struct owo
{
int x, y, direct, rlen, len;
bool operator < (const owo &b) const
{
return rlen + get(len) > b.rlen + get(b.len);
}
};
void input()
{
cin >> n >> m >> k;
FOR(i, 1, n) FOR(j, 1, m)
{
char c; cin >> c;
a[i][j] = (c == '.' ? 1 : -1);
f[0][i][j] = f[1][i][j] = f[2][i][j] = f[3][i][j] = INF;
}
cin >> xs >> ys >> xt >> yt;
}
bool inside(int u, int v)
{
return u >= 1 && u <= n && v >= 1 && v <= m;
}
void dijk()
{
priority_queue<owo> pq;
f[0][xs][ys] = f[1][xs][ys] = f[2][xs][ys] = f[3][xs][ys] = 0;
pq.push({xs, ys, 0, 0, 0});
pq.push({xs, ys, 1, 0, 0});
pq.push({xs, ys, 2, 0, 0});
pq.push({xs, ys, 3, 0, 0});
while(pq.size())
{
int x = pq.top().x;
int y = pq.top().y;
int dir = pq.top().direct;
int rlen = pq.top().rlen;
int len = pq.top().len;
// cout << x << " " << y << " " << rlen + get(len) << "\n";
pq.pop();
if (rlen + get(len) > f[dir][x][y]) continue;
if (x == xt && y == yt) return;
int cur = get(len+1);
int cc = get(len);
REP(i, 4)
{
int u = x + dx[i];
int v = y + dy[i];
if (inside(u, v) && a[u][v] != -1)
{
if (i == dir)
{
if (minimize(f[dir][u][v], rlen + cur))
{
pq.push({u, v, dir, rlen, len + 1});
}
}
else
{
if (minimize(f[i][u][v], rlen + cc + 1))
{
pq.push({u, v, i, rlen + cc, 1});
}
}
}
}
}
}
void solve()
{
if (xs == xt && ys == yt)
{
cout << 0;
return;
}
dijk();
int res = INF;
REP(i, 4)
{
// cout << f[i][xt][yt] << " ";
minimize(res, f[i][xt][yt]);
}
cout << (res == INF ? -1 : res);
}
__Shibae__
{
IOS
fipen("m");
input();
solve();
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CiNpbmNsdWRlIDxzdGRpby5oPgoKI2RlZmluZSBfX1NoaWJhZV9fICAgICAgc2lnbmVkIG1haW4oKQojZGVmaW5lIElPUy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CiAgICAgICAgfSAKICAgICAgICBlbHNlIHJldHVybiBmYWxzZTsKICAgIH0KdGVtcGxhdGU8Y2xhc3MgU0hJQkEsIGNsYXNzIEVOR0lORT4KICAgIGJvb2wgbWF4aW1pemUoU0hJQkEgJngsIGNvbnN0IEVOR0lORSB5KQogICAgewogICAgICAgIGlmKHggPCB5KQogICAgICAgIHsKICAgICAgICAgICAgeCA9IHk7CiAgICAgICAgICAgIHJldHVybiB0cnVlOwogICAgICAgIH0KICAgICAgICBlbHNlIHJldHVybiBmYWxzZTsKICAgIH0KCgovKiBUZW1wbGF0ZSBieTogTmd1eWVuIE5oYXQgQW5oIGZyb20gTHVvbmcgVmFuIENoYW5oIEhpZ2ggU2Nob29sIGZvciB0aGUgZ2lmdGVkICovCi8qIEZyb20gTWluIFR1b2kgd2l0aCBsb3ZlICovCiAgICAgICAgLyoqICAgICAgIFRSWSBIQVJEICAgICAgICAqKi8KICAgICAgICAvKiogICAgICAgICAgT1JaICAgICAgICAgICoqLwoKLyogLS0tLS0tLS0tLS0tLS0tLS1bIE1BSU4gQ09ERSBdLS0tLS0tLS0tLS0tLS0tLS0gKi8KCmludCBuLCBtLCBrOwppbnQgYVtNQVhdW01BWF07CmludCBmWzRdW01BWF1bTUFYXTsKaW50IHhzLCB5cywgeHQsIHl0OyAKCmludCBnZXQoaW50IHgpCnsKICAgIHJldHVybiB4IC8gayArICh4ICUgayAhPSAwKTsKfQoKc3RydWN0IG93bwp7CiAgICBpbnQgeCwgeSwgZGlyZWN0LCBybGVuLCBsZW47CgogICAgYm9vbCBvcGVyYXRvciA8IChjb25zdCBvd28gJmIpIGNvbnN0CiAgICB7CiAgICAgICAgcmV0dXJuIHJsZW4gKyBnZXQobGVuKSA+IGIucmxlbiArIGdldChiLmxlbik7CiAgICB9Cn07Cgp2b2lkIGlucHV0KCkKewogICAgY2luID4+IG4gPj4gbSA+PiBrOwoKICAgIEZPUihpLCAxLCBuKSBGT1IoaiwgMSwgbSkKICAgIHsKICAgICAgICBjaGFyIGM7IGNpbiA+PiBjOwogICAgICAgIGFbaV1bal0gPSAoYyA9PSAnLicgPyAxIDogLTEpOwogICAgICAgIGZbMF1baV1bal0gPSBmWzFdW2ldW2pdID0gZlsyXVtpXVtqXSA9IGZbM11baV1bal0gPSBJTkY7CiAgICB9CgogICAgY2luID4+IHhzID4+IHlzID4+IHh0ID4+IHl0Owp9Cgpib29sIGluc2lkZShpbnQgdSwgaW50IHYpCnsKICAgIHJldHVybiB1ID49IDEgJiYgdSA8PSBuICYmIHYgPj0gMSAmJiB2IDw9IG07Cn0KCnZvaWQgZGlqaygpCnsKICAgIHByaW9yaXR5X3F1ZXVlPG93bz4gcHE7CgogICAgZlswXVt4c11beXNdID0gZlsxXVt4c11beXNdID0gZlsyXVt4c11beXNdID0gZlszXVt4c11beXNdID0gMDsKCiAgICBwcS5wdXNoKHt4cywgeXMsIDAsIDAsIDB9KTsKICAgIHBxLnB1c2goe3hzLCB5cywgMSwgMCwgMH0pOwogICAgcHEucHVzaCh7eHMsIHlzLCAyLCAwLCAwfSk7CiAgICBwcS5wdXNoKHt4cywgeXMsIDMsIDAsIDB9KTsKCiAgICB3aGlsZShwcS5zaXplKCkpCiAgICB7CiAgICAgICAgaW50IHggPSBwcS50b3AoKS54OwogICAgICAgIGludCB5ID0gcHEudG9wKCkueTsKICAgICAgICBpbnQgZGlyID0gcHEudG9wKCkuZGlyZWN0OwogICAgICAgIGludCBybGVuID0gcHEudG9wKCkucmxlbjsKICAgICAgICBpbnQgbGVuID0gcHEudG9wKCkubGVuOwogICAgICAgIC8vIGNvdXQgPDwgeCA8PCAiICIgPDwgeSA8PCAiICIgPDwgcmxlbiArIGdldChsZW4pIDw8ICJcbiI7CgogICAgICAgIHBxLnBvcCgpOwoKICAgICAgICBpZiAocmxlbiArIGdldChsZW4pID4gZltkaXJdW3hdW3ldKSBjb250aW51ZTsKICAgICAgICBpZiAoeCA9PSB4dCAmJiB5ID09IHl0KSByZXR1cm47CgogICAgICAgIGludCBjdXIgPSBnZXQobGVuKzEpOwogICAgICAgIGludCBjYyA9IGdldChsZW4pOwoKICAgICAgICBSRVAoaSwgNCkKICAgICAgICB7CiAgICAgICAgICAgIGludCB1ID0geCArIGR4W2ldOwogICAgICAgICAgICBpbnQgdiA9IHkgKyBkeVtpXTsKCiAgICAgICAgICAgIGlmIChpbnNpZGUodSwgdikgJiYgYVt1XVt2XSAhPSAtMSkKICAgICAgICAgICAgewogICAgICAgICAgICAgICAgaWYgKGkgPT0gZGlyKQogICAgICAgICAgICAgICAgewogICAgICAgICAgICAgICAgICAgIGlmIChtaW5pbWl6ZShmW2Rpcl1bdV1bdl0sIHJsZW4gKyBjdXIpKQogICAgICAgICAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgICAgICAgICAgcHEucHVzaCh7dSwgdiwgZGlyLCBybGVuLCBsZW4gKyAxfSk7CiAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgZWxzZQogICAgICAgICAgICAgICAgewogICAgICAgICAgICAgICAgICAgIGlmIChtaW5pbWl6ZShmW2ldW3VdW3ZdLCBybGVuICsgY2MgKyAxKSkKICAgICAgICAgICAgICAgICAgICB7CiAgICAgICAgICAgICAgICAgICAgICAgIHBxLnB1c2goe3UsIHYsIGksIHJsZW4gKyBjYywgMX0pOwogICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgfQogICAgICAgIH0KICAgIH0KfQoKdm9pZCBzb2x2ZSgpCnsKICAgIGlmICh4cyA9PSB4dCAmJiB5cyA9PSB5dCkKICAgIHsKICAgICAgICBjb3V0IDw8IDA7CiAgICAgICAgcmV0dXJuOwogICAgfQoKICAgIGRpamsoKTsKCiAgICBpbnQgcmVzID0gSU5GOwoKICAgIFJFUChpLCA0KQogICAgewogICAgICAgIC8vIGNvdXQgPDwgZltpXVt4dF1beXRdIDw8ICIgIjsKICAgICAgICBtaW5pbWl6ZShyZXMsIGZbaV1beHRdW3l0XSk7CiAgICB9ICAgIAoKICAgIGNvdXQgPDwgKHJlcyA9PSBJTkYgPyAtMSA6IHJlcyk7Cn0KCl9fU2hpYmFlX18KewogICAgSU9TCiAgICBmaXBlbigibSIpOwoKICAgIGlucHV0KCk7CiAgICBzb2x2ZSgpOwogCiAgICByZXR1cm4gMDsKfQ==