#include<bits/stdc++.h>
using namespace std;
#define fine cout<<"fine"<<endl;
#define ll long long int
#define pb push_back
#define FASTIO ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl "\n"
#define ff first
#define ss second
#define all(v) v.begin(),v.end()
const ll inf=(ll)2e18;
const int mod=1e9+7;
bool overflow(ll a,ll b){
    if (a <= (inf+b-1)/b) return false;
    return true;
}
void rightdpset(vector<vector<int> >&dp,int n,int m,string s[]){
    int i,j;
    for(j=m-1;j>=0;j--){
        for(i=0;i<n;i++){
            if(j==m-1){
                if(s[i][j]=='@')
                    dp[i][j]=j;
                else dp[i][j]=j+1;
            }
            else{
                if(s[i][j]=='@')
                    dp[i][j]=j;
                else dp[i][j]=dp[i][j+1];
            }
        }
    }
}
void leftdpset(vector<vector<int> >&dp,int n,int m,string s[]){
    int i,j;
    for(j=0;j<m;j++){
        for(i=0;i<n;i++){
            if(j==0){
                if(s[i][j]=='@'){
                    dp[i][j]=0;
                }
                else dp[i][j]=-1;
            }
            else{
                if(s[i][j]=='@')
                    dp[i][j]=j;
                else dp[i][j]=dp[i][j-1];
            }
        }
    }
}

void upset(vector<vector<int> >&dp,int n,int m,string s[]){
    int i,j;
    for(i=0;i<n;i++){
        for(j=0;j<m;j++){
            if(i==0){
                if(s[i][j]=='@')
                    dp[i][j]=i;
                else dp[i][j]=-1;
            }
            else{
                if(s[i][j]=='@')
                    dp[i][j]=i;
                else dp[i][j]=dp[i-1][j];
            }
        }
    }
}
void downset(vector<vector<int> >&dp,int n,int m,string s[]){
    int i,j;
    for(i=n-1;i>=0;i--){
        for(j=0;j<m;j++){
            if(i==n-1){
                if(s[i][j]=='@')
                    dp[i][j]=i;
                else dp[i][j]=i+1;
            }
            else{
                if(s[i][j]=='@')
                    dp[i][j]=i;
                else dp[i][j]=dp[i+1][j];
            }
        }
    }
}
int main(){
FASTIO;
int n,m,k;
int i,j;
cin>>n>>m>>k;
vector<vector<int> > dist,leftdp,rightdp,up,down;
dist.resize(n);
leftdp.resize(n);
rightdp.resize(n);
up.resize(n);
down.resize(n);
for(i=0;i<n;i++){
    dist[i].resize(m);
    leftdp[i].resize(m);
    rightdp[i].resize(m);
    up[i].resize(m);
    down[i].resize(m);
}
int x1,y1,x2,y2;
cin>>x1>>y1>>x2>>y2;
x1--,y1--,x2--,y2--;
string s[n];
for(i=0;i<n;i++){
    cin>>s[i];
}
set<int> rowset[n],colset[m];
leftdpset(leftdp,n,m,s);
rightdpset(rightdp,n,m,s);
downset(down,n,m,s);
upset(up,n,m,s);

for(i=0;i<n;i++){
    for(j=0;j<m;j++){
        if(s[i][j]!='@'){
            rowset[i].insert(j);
            colset[j].insert(i);
        }
    }
}

queue<pair<int,int> > q;
q.push({x1,y1});
rowset[x1].erase(y1);
colset[y1].erase(x1);

while(q.size()){
    int x=q.front().ff;
    int y=q.front().ss;
    int it=y;
    q.pop();
    while(1){
        auto iter=rowset[x].upper_bound(it);
        if(iter==rowset[x].end())
            break;
        if(*iter-y>k||*iter>=rightdp[x][y])
            break;
        it=*iter;
        q.push({x,it});
        dist[x][it]=dist[x][y]+1;
        rowset[x].erase(it);
        colset[it].erase(x);
    }

    it=x;
    while(1){
        auto iter=colset[y].upper_bound(it);
        if(iter==colset[y].end())
            break;
        if(*iter-x>k||*iter>=down[x][y])
            break;
        it=*iter;
        q.push({it,y});
        dist[it][y]=dist[x][y]+1;
        rowset[it].erase(y);
        colset[y].erase(it);
    }

    it=max(up[x][y]+1,x-k);
    if(colset[y].find(it)!=colset[y].end()){
        q.push({it,y});
        dist[it][y]=dist[x][y]+1;
        colset[y].erase(it);
        rowset[it].erase(y);
    }
    while(1){
        auto iter=colset[y].upper_bound(it);
        if(iter==colset[y].end())
            break;
        if(x-*iter>k||*iter>x)
            break;
        it=*iter;
        q.push({it,y});
        dist[it][y]=dist[x][y]+1;
        rowset[it].erase(y);
        colset[y].erase(it);
    }

    it=max(leftdp[x][y]+1,y-k);
    if(rowset[x].find(it)!=rowset[x].end()){
        q.push({x,it});
        dist[x][it]=dist[x][y]+1;
        rowset[x].erase(it);
        colset[it].erase(x);
    }
    while(1){
        auto iter=rowset[x].upper_bound(it);
        if(iter==rowset[x].end())
            break;
        if(y-*iter>k||*iter>y)
            break;
        it=*iter;
        q.push({x,it});
        dist[x][it]=dist[x][y]+1;
        rowset[x].erase(it);
        colset[it].erase(x);
    }
}
if(dist[x2][y2])
    cout<<dist[x2][y2]<<endl;
else cout<<-1<<endl;
}


Measured memory_bytes: 163940