#include <bits/stdc++.h>
#include <stdio.h>
 
using namespace std;
 
#define int long long
#define rep(i, a, b) for (int i = (a); i < (b); ++i)
#define trav(i, a) for (auto &i: (a))
#define sz(x) (int)(x).size()
#define all(v) (v).begin(), (v).end()
#define fr(i, a, b) for (int i = (a); i <= (b); ++i)

void solve();

signed main () {
    clock_t beg = clock();
    cout << fixed << setprecision(15);
    ios_base::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    #ifndef ONLINE_JUDGE
    // freopen("input.txt", "r", stdin);
    #endif 
    int t = 1; 
    // cin >> t;    
    int tt = t;
    while (t--) {
        // cout << "Case #" << tt - t << ": "; 
        solve();
    }
    cerr << "\nExecution time: " << (clock() - beg) / 1000 << '\n';
    return 0;
}
vector<string> a;
vector<set<pair<int, int>>> dset[2];
vector<vector<int>> pre[2]; 
int n, m, k;
vector<vector<int>> dist;
bool valid_cell (int x, int y) {
    return x >= 1 and x <= n and y >= 1 and y <= m;
}
bool valid (int x, int y, int x2, int y2) {
    if (!valid_cell(x, y) || !valid_cell(x2, y2))
        return false;
    if (abs(x - x2) + abs(y - y2) > k)
        return false;
    int dir = 0;
    if (y == y2)
        ++dir;
    if (x < x2)
        swap(x, x2);
    if (y < y2)
        swap(y, y2);
    if (dir == 0) 
        return !(pre[dir][x][y2 - 1] - pre[dir][x][y]);
    else 
        return !(pre[dir][y][x2 - 1] - pre[dir][y][x]);
}
queue<pair<int, int>> q;
pair<int, int> get (pair<int, int> p, int d, int idx, bool smaller) {
    auto it = dset[d][idx].lower_bound(p);
    if (smaller)
        --it;
    return *it;
}
void solve() {  
    int x1, y1, x2, y2;
    cin >> n >> m >> k;
    cin >> x1 >> y1 >> x2 >> y2;
    a.resize(n + 1);
    dset[0].resize(n + 1);
    dset[1].resize(m + 1);
    pre[0].resize(n + 1);
    pre[1].resize(m + 1);
    dist.resize(n + 1);
    fr(i, 1, n) {
        dist[i].resize(m + 1, (int) 1e9);
        cin >> a[i];
        a[i] = '$' + a[i];
        pre[0][i].resize(m + 1, 0);
        pre[0][i][0] = 0;
        for (int j = 1; j <= m; ++j) {
            pre[0][i][j] = pre[0][i][j - 1] + (a[i][j] == '@');
        }
        fr(j, 0, m + 1)
            dset[0][i].insert({i, j});
    }
    fr(j, 1, m) {
        pre[1][j].resize(n + 1);
        pre[1][j][0] = 0;
        for (int i = 1; i <= n; ++i) {
            pre[1][j][i] = pre[1][j][i - 1] + (a[i][j] == '@');
        }
        fr(i, 0, n + 1) 
            dset[1][j].insert({i, j});
    }
    dist[x1][y1] = 0;
    q.push({x1, y1});
    dset[0][x1].erase({x1, y1});
    dset[1][y1].erase({x1, y1});
    while (!q.empty()) {
        auto lol = q.front();
        q.pop();
        int cur = dist[lol.first][lol.second];
        while (true) {
            auto lol2 = get(lol, 0, lol.first, 0);
            if (!valid(lol.first, lol.second, lol2.first, lol2.second))
                break;
            dset[1][lol2.second].erase(lol2);
            dset[0][lol2.first].erase(lol2);
            dist[lol2.first][lol2.second] = cur + 1;
            q.push(lol2);
        }
        while (true) {
            auto lol2 = get(lol, 0, lol.first, 1);
            if (!valid(lol.first, lol.second, lol2.first, lol2.second))
                break;
            dset[1][lol2.second].erase(lol2);
            dset[0][lol2.first].erase(lol2);
            dist[lol2.first][lol2.second] = cur + 1;
            q.push(lol2);
        }
        while (true) {
            auto lol2 = get(lol, 1, lol.second, 0);
            if (!valid(lol.first, lol.second, lol2.first, lol2.second))
                break;
            dset[1][lol2.second].erase(lol2);
            dset[0][lol2.first].erase(lol2);
            dist[lol2.first][lol2.second] = cur + 1;
            q.push(lol2);
        }
        while (true) {
            auto lol2 = get(lol, 1, lol.second, 1);
            if (!valid(lol.first, lol.second, lol2.first, lol2.second))
                break;
            dset[1][lol2.second].erase(lol2);
            dset[0][lol2.first].erase(lol2);
            dist[lol2.first][lol2.second] = cur + 1;
            q.push(lol2);
        }
    }
    if (dist[x2][y2] == (int) 1e9) {
        cout << -1;
    } else {
        cout << dist[x2][y2];
    }
}       
    

Measured memory_bytes: 371468