Problem:
Score : 600 points

Problem Statement

Snuke, a water strider, lives in a rectangular pond that can be seen as a grid with H east-west rows and W north-south columns. Let (i,j) be the square at the i-th row from the north and j-th column from the west.

Some of the squares have a lotus leaf on it and cannot be entered.
The square (i,j) has a lotus leaf on it if c_{ij} is @, and it does not if c_{ij} is ..

In one stroke, Snuke can move between 1 and K squares (inclusive) toward one of the four directions: north, east, south, and west.
The move may not pass through a square with a lotus leaf. Moving to such a square or out of the pond is also forbidden.

Find the minimum number of strokes Snuke takes to travel from the square (x_1,y_1) to (x_2,y_2).
If the travel from (x_1,y_1) to (x_2,y_2) is impossible, point out that fact.

Constraints

1 \leq H,W,K \leq 10^6

H \times W \leq 10^6

1 \leq x_1,x_2 \leq H

1 \leq y_1,y_2 \leq W

x_1 \neq x_2 or y_1 \neq y_2.

c_{i,j} is . or @.

c_{x_1,y_1} = .

c_{x_2,y_2} = .

All numbers in input are integers.

Input

Input is given from Standard Input in the following format:

H W K
x_1 y_1 x_2 y_2
c_{1,1}c_{1,2} .. c_{1,W}
c_{2,1}c_{2,2} .. c_{2,W}
:
c_{H,1}c_{H,2} .. c_{H,W}

Output

Print the minimum number of strokes Snuke takes to travel from the square (x_1,y_1) to (x_2,y_2), or print -1 if the travel is impossible.

Sample Input 1

3 5 2
3 2 3 4
.....
.@..@
..@..

Sample Output 1

5

Initially, Snuke is at the square (3,2).
He can reach the square (3, 4) by making five strokes as follows:

From (3, 2), go west one square to (3, 1).

From (3, 1), go north two squares to (1, 1).

From (1, 1), go east two squares to (1, 3).

From (1, 3), go east one square to (1, 4).

From (1, 4), go south two squares to (3, 4).

Sample Input 2

1 6 4
1 1 1 6
......

Sample Output 2

2

Sample Input 3

3 3 1
2 1 2 3
.@.
.@.
.@.

Sample Output 3

-1

Solution in C++ (GCC 9.2.1) (judge verdict: Accepted)

#include <ext/pb_ds/assoc_container.hpp>
#include <bits/stdc++.h>
using namespace std;
using namespace __gnu_pbds;
template <class c, class cmp = less<c> > using ordered_set = tree<c, null_type, cmp, rb_tree_tag, tree_order_statistics_node_update>;
#define IOS ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
  cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
  const char* comma = strchr(names + 1, ',');cerr.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}
#else
#define trace(...) 
#endif
template<class T> ostream& operator<<(ostream &os, vector<T> V) {os << "[ "; for(auto v : V) os << v << " "; return os << "]";}
template<class L, class R> ostream& operator<<(ostream &os, pair<L,R> P) {return os << "(" << P.first << "," << P.second << ")";}
template <typename T,typename U>pair<T,U> operator+(const pair<T,U> & l,const std::pair<T,U> & r) { return {l.first+r.first,l.second+r.second};}
typedef long long int ll;
const ll mod =1e9+7;
const int maxn = 200002;
#define endl '\n'
#define int ll
#define ld long double
// #define all(x) (x).begin(),(x).end()

queue<int> q;
bool vis[2001001];
int dist[2001010];
string s[2001001];
int n,m,K;
int x1,Y1,x2,y2;
int get(int i,int j){
  return (i*m+j);
}
set<int> col[1000001],row[1000001];
int di[4] = {1,0,-1,0};
int dj[4] = {0,1,0,-1};
bool isvalid(int i,int j){
  return (i>=0 and j>=0 and i<n and j<m and s[i][j]!='@');
}
void bfs(){

  q.push(get(x1,Y1));
  dist[get(x1,Y1)] = 0;
  row[x1].erase(Y1);
  col[Y1].erase(x1);
  int cnt = 0;
  while(!q.empty()){
    int  u = q.front();
    q.pop();
    int i = u/m;
    int j = u%m;
    cnt++;
    assert(cnt<100000000);
    int cur = dist[get(i,j)];
    if(!isvalid(i,j)) continue;
    for(int k = 0;k<4;k++){
      
      int ni = i+di[k];
      int nj = j+dj[k];
      if(!isvalid(ni,nj)) continue;
      if(di[k] == 1){
        auto it = col[nj].lower_bound(i);
        while(it != col[nj].end() and abs(*it - i)<=K){
          int val = *it;
          if(!isvalid(val,nj)) break;
          dist[get(val,nj)] = cur +1;
          col[nj].erase(val);
          row[val].erase(nj);
          q.push(get(val,nj));
          it = col[nj].lower_bound(i);
        }
      }      
      if(di[k] == -1){
        auto it = col[nj].lower_bound(i);
        while(it!=col[nj].begin() and abs(*(--it) - i)<=K){
          int val = *it;
          if(!isvalid(val,nj)) break;
          dist[get(val,nj)] = cur +1 ;
          col[nj].erase(val);
          row[val].erase(nj);
          q.push(get(val,nj));
          it = col[nj].lower_bound(i);
        }
      }      
      if(dj[k] == 1){
        auto it = row[ni].lower_bound(j);
        while(it != row[ni].end() and abs(*it - j)<=K){
          int val = *it;
          if(!isvalid(ni,val)) break;
          dist[get(ni,val)] = cur +1 ;
          row[ni].erase(val);
          col[val].erase(ni);
          q.push(get(ni,val));
          it = row[ni].lower_bound(j);
        }
      }      
      if(dj[k] == -1){
        auto it = row[ni].lower_bound(j);        
        while(it != row[ni].begin() and abs(*(--it) - j)<=K){
          int val = *it;
          if(!isvalid(ni,val)) break;
          dist[get(ni,val)] = cur +1 ;
          row[ni].erase(val);
          col[val].erase(ni);
          q.push(get(ni,val));
          it = row[ni].lower_bound(j);
        }
      }      
      
    }
  }
}
int32_t main()
{ 
  IOS

  
  cin>>n>>m>>K;
  cin>>x1>>Y1>>x2>>y2;
  x1--;
  Y1--;
  x2--;
  y2--; 
  for(int i = 0;i<n;i++){
    cin>>s[i];
  }

  for(int i = 0;i<n;i++){
    for(int j = 0;j<m;j++){
      row[i].insert(j);
      col[j].insert(i);
    }
  }

  for(int i = 0;i<n;i++){
    for(int j = 0;j<m;j++)
      dist[get(i,j)] = 1e9;
  }
  bfs();
  if(dist[get(x2,y2)]>1e7){
    cout<<-1<<endl;
  }
  else cout<<dist[get(x2,y2)];



}