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 Java (OpenJDK 11.0.6) (judge verdict: Time Limit Exceeded)

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.StringTokenizer;
import java.util.TreeSet;
/*
3 5 2
3 2 3 4
.....
.@..@
..@..

 */
public class Main {

	public static void main(String[] args) {
		FastScanner fs=new FastScanner();
		int h=fs.nextInt(), w=fs.nextInt(), jumpDist=fs.nextInt();
		int startY=fs.nextInt()-1, startX=fs.nextInt()-1, endY=fs.nextInt()-1, endX=fs.nextInt()-1;
		TreeSet<Integer>[] onX=new TreeSet[w];
		TreeSet<Integer>[] onY=new TreeSet[h];
//		TreeSet<Integer>[] noX=new TreeSet[w];
//		TreeSet<Integer>[] noY=new TreeSet[h];
		for (int x=0; x<w; x++) onX[x]=new TreeSet<>();
		for (int y=0; y<h; y++) onY[y]=new TreeSet<>();
//		for (int x=0; x<w; x++) noX[x]=new TreeSet<>();
//		for (int y=0; y<h; y++) noY[y]=new TreeSet<>();
		
		
		
		boolean[][] board=new boolean[w][h];
		for (int y=0; y<h; y++) {
			char[] line=fs.next().toCharArray();
			for (int i=0; i<line.length; i++) {
				board[i][y]=line[i]!='@';
				if (board[i][y]) {
					onX[i].add(y);
					onY[y].add(i);
				}
//				else {
//					noX[i].add(y);
//					noY[y].add(i);
//				}
			}
		}
		
		int[][][] maxJump=new int[w][h][4];
		for (int x=0; x<w; x++) {
			for (int y=0; y<h; y++) {
				if (!board[x][y]) maxJump[x][y][0]=-1;
				else {
					if (y==0) maxJump[x][y][0]=0;
					else maxJump[x][y][0]=Math.min(jumpDist, maxJump[x][y-1][0]+1);
				}
			}
			
			for (int y=h-1; y>=0; y--) {
				if (!board[x][y]) maxJump[x][y][1]=-1;
				else {
					if (y==h-1) maxJump[x][y][1]=0;
					else maxJump[x][y][1]=Math.min(jumpDist, maxJump[x][y+1][1]+1);
				}
			}
		}
		
		for (int y=0; y<h; y++) {
			for (int x=0; x<w; x++) {
				if (!board[x][y]) maxJump[x][y][2]=-1;
				else {
					if (x==0) maxJump[x][y][2]=0;
					else maxJump[x][y][2]=Math.min(jumpDist, maxJump[x-1][y][2]+1);
				}
			}
			
			for (int x=w-1; x>=0; x--) {
				if (!board[x][y]) maxJump[x][y][3]=-1;
				else {
					if (x==w-1) maxJump[x][y][3]=0;
					else maxJump[x][y][3]=Math.min(jumpDist, maxJump[x+1][y][3]+1);
				}
			}
		}
		ArrayDeque<Integer> xs=new ArrayDeque<>();
		ArrayDeque<Integer> ys=new ArrayDeque<>();
		xs.addLast(startX);
		ys.addLast(startY);
		onX[startX].remove(startY);
		onY[startY].remove(startX);
		int[][] dist=new int[w][h];
		for (int x=0; x<dist.length; x++) Arrays.fill(dist[x], -1);
		dist[startX][startY]=0;
		while (!xs.isEmpty()) {
			int curX=xs.removeFirst(), curY=ys.removeFirst();
//			System.out.println("At "+curX+" "+curY+" "+dist[curX][curY]);
			//up
			{
				Integer aboveY=onX[curX].floor(curY);
				while (aboveY!=null && curY-aboveY<=maxJump[curX][curY][0]) {
//					Integer block=noX[curX].floor(curY);
//					if (block==null || aboveY>block) {
						onX[curX].remove(aboveY);
						onY[aboveY].remove(curX);
						xs.addLast(curX);
						ys.addLast(aboveY);
						dist[curX][aboveY]=dist[curX][curY]+1;
//					}
//					else break;
					aboveY=onX[curX].floor(curY);
				}
			}
			//down
			{
				Integer belowY=onX[curX].ceiling(curY);
				while (belowY!=null && belowY-curY<=maxJump[curX][curY][1]) {
//					Integer block=noX[curX].ceiling(curY);
//					if (block==null || belowY<block) {
						onX[curX].remove(belowY);
						onY[belowY].remove(curX);
						xs.addLast(curX);
						ys.addLast(belowY);
						dist[curX][belowY]=dist[curX][curY]+1;
//					}
//					else break;
					belowY=onX[curX].ceiling(curY);
				}
			}
			
			//left
			{
				Integer aboveX=onY[curY].floor(curX);
				while (aboveX!=null && curX-aboveX<=maxJump[curX][curY][2]) {
//					Integer block=noY[curY].floor(curX);
//					if (block==null || aboveX>block) {
						onY[curY].remove(aboveX);
						onX[aboveX].remove(curY);
						xs.addLast(aboveX);
						ys.addLast(curY);
						dist[aboveX][curY]=dist[curX][curY]+1;
//					}
//					else break;
					aboveX=onY[curY].floor(curX);
				}
			}
			//right
			{
				Integer belowX=onY[curY].ceiling(curX);
				while (belowX!=null && belowX-curX<=maxJump[curX][curY][3]) {
//					Integer block=noY[curY].ceiling(curX);
//					if (block==null || belowX<block) {
						onY[curY].remove(belowX);
						onX[belowX].remove(curY);
						xs.addLast(belowX);
						ys.addLast(curY);
						dist[belowX][curY]=dist[curX][curY]+1;
//					}
//					else break;
					belowX=onY[curY].ceiling(curX);
				}
			}
		}
		
//		for (int y=0; y<h; y++) {
//			for (int x=0; x<w; x++) {
//				System.out.print(dist[x][y]+" ");
//			}
//			System.out.println();
//		}
		System.out.println(dist[endX][endY]);
	}

	static void sort(int[] a) {
		ArrayList<Integer> l=new ArrayList<>();
		for (int i:a) l.add(i);
		Collections.sort(l);
		for (int i=0; i<a.length; i++) a[i]=l.get(i);
	}
	
	static class FastScanner {
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st=new StringTokenizer("");
		String next() {
			while (!st.hasMoreTokens())
				try {
					st=new StringTokenizer(br.readLine());
				} catch (IOException e) {
					e.printStackTrace();
				}
			return st.nextToken();
		}
		
		int nextInt() {
			return Integer.parseInt(next());
		}
		int[] readArray(int n) {
			int[] a=new int[n];
			for (int i=0; i<n; i++) a[i]=nextInt();
			return a;
		}
		long nextLong() {
			return Long.parseLong(next());
		}
	}

	
}