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 Python (3.8.2) (judge verdict: Time Limit Exceeded)

import queue
def solve(s, t):
    global G
    fixed = {}
    q = queue.PriorityQueue()
    q.put((0, s)) # 0は累積コスト
    while q.empty() == False:
        w, x = q.get()
        if x == t: 
            return w
        if x in fixed: 
            continue;
        fixed[x] = w
        for y in G[x]:
              if y not in fixed:
                    q.put((w + G[x][y], y))
    return None

H, W, K = map(int, input().split())
x1, y1, x2, y2 = map(int, input().split())
x1 -= 1
y1 -= 1
x2 -= 1
y2 -= 1
C = []
for h in range(H):
    c = input()
    C.extend([s for s in c])

INF = 10**9
G = {i:{} for i in range(H*W)}
for h in range(H):
    for w in range(W):
        d = h * W + w
        #print(d)
        if C[d] == '@':
            flag_west = True
            flag_east = True
            flag_north = True
            flag_south = True
        else:
            flag_west = False
            flag_east = False
            flag_north = False
            flag_south = False
        for k in range(1, K+1):
            # west
            pos = d - k
            if h*W <= pos < (h+1)*W:
                #print("\t west: {} {}".format(d - k, C[pos]))
                if C[pos] == '@' or flag_west:
                    G[d][pos] = INF
                    flag_west = True
                else:
                    G[d][pos] = 1
                    
            # east
            pos = d + k
            if h*W <= pos  < (h+1)*W:
                #print("\t east: {} {}".format(d + k, C[pos]))
                if C[pos] == '@' or flag_east:
                    G[d][pos] = INF
                    flag_east = True
                else:
                    G[d][pos] = 1
                    
            # north
            pos = d - k*W
            if 0 <= pos  < H*W:
                #print("\t north: {} {}".format(d - k*W, C[pos]))
                if C[pos] == '@' or flag_north:
                    G[d][pos] = INF
                    flag_north = True
                else:
                    G[d][pos] = 1
                    
            # south
            pos =  d + k*W
            if 0 <= pos  < H*W:
                #print("\t south: {} {}".format(d + k*W, C[pos]))
                if C[pos] == '@' or flag_south:
                    G[d][pos] = INF
                    flag_south = True
                else:
                    G[d][pos] = 1
        
s = W*x1 + y1
t = W*x2 + y2
ans = solve(s, t)
if ans >= INF:
    print(-1)
else:
    print(ans)