Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
H, W, K = map(int, input().split())
sh, sw, gh, gw = map(int, input().split())
sh, sw, gh, gw = sh - 1, sw - 1, gh - 1, gw - 1
G = [list(input()) for _ in range(H)]
INF = 10 ** 9


D = [[INF] * W for _ in range(H)]
D[sh][sw] = 0
directions = [(0, 1), (0, -1), (1, 0), (-1, 0)]
que = deque([(sh, sw)])

while que:
    nh, nw = que.pop()
    for dh, dw in directions:
        for k in range(1, K + 1):
            nx_h, nx_w = nh + k * dh, nw + k * dw
            if not (0 <= nx_h < H and 0 <= nx_w < W):
                break

            if G[nx_h][nx_w] == '@':
                break

            if D[nx_h][nx_w] <= D[nh][nw]:
                break

            D[nx_h][nx_w] = D[nh][nw] + 1
            que.appendleft((nx_h, nx_w))

print(D[gh][gw] if D[gh][gw] != INF else -1)


Fixed submission:
from collections import deque
H, W, K = map(int, input().split())
sh, sw, gh, gw = map(int, input().split())
sh, sw, gh, gw = sh - 1, sw - 1, gh - 1, gw - 1
G = [list(input()) for _ in range(H)]
INF = 10 ** 9


D = [[INF] * W for _ in range(H)]
D[sh][sw] = 0
directions = [(0, 1), (0, -1), (1, 0), (-1, 0)]
que = deque([(sh, sw)])

while que:
    nh, nw = que.pop()
    for dh, dw in directions:
        for k in range(1, K + 1):
            nx_h, nx_w = nh + k * dh, nw + k * dw
            if not (0 <= nx_h < H and 0 <= nx_w < W):
                break

            if G[nx_h][nx_w] == '@':
                break

            if D[nx_h][nx_w] <= D[nh][nw]:
                break

            if D[nx_h][nx_w] > D[nh][nw] + 1:
                D[nx_h][nx_w] = D[nh][nw] + 1
                que.appendleft((nx_h, nx_w))

print(D[gh][gw] if D[gh][gw] != INF else -1)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
H, W, K = map(int, input().split())
sh, sw, gh, gw = map(int, input().split())
sh, sw, gh, gw = sh - 1, sw - 1, gh - 1, gw - 1
G = [list(input()) for _ in range(H)]
INF = 10 ** 9


D = [[INF] * W for _ in range(H)]
D[sh][sw] = 0
directions = [(0, 1), (0, -1), (1, 0), (-1, 0)]
que = deque([(sh, sw)])

while que:
    nh, nw = que.pop()
    for dh, dw in directions:
        for k in range(1, K + 1):
            nx_h, nx_w = nh + k * dh, nw + k * dw
            if not (0 <= nx_h < H and 0 <= nx_w < W):
                break

            if G[nx_h][nx_w] == '@':
                break

            if D[nx_h][nx_w] < D[nh][nw] + 1:
                break

            D[nx_h][nx_w] = D[nh][nw] + 1
            que.appendleft((nx_h, nx_w))

print(D[gh][gw] if D[gh][gw] != INF else -1)


Fixed submission:
from collections import deque
H, W, K = map(int, input().split())
sh, sw, gh, gw = map(int, input().split())
sh, sw, gh, gw = sh - 1, sw - 1, gh - 1, gw - 1
G = [list(input()) for _ in range(H)]
INF = 10 ** 9


D = [[INF] * W for _ in range(H)]
D[sh][sw] = 0
directions = [(0, 1), (0, -1), (1, 0), (-1, 0)]
que = deque([(sh, sw)])

while que:
    nh, nw = que.pop()
    for dh, dw in directions:
        for k in range(1, K + 1):
            nx_h, nx_w = nh + k * dh, nw + k * dw
            if not (0 <= nx_h < H and 0 <= nx_w < W):
                break

            if G[nx_h][nx_w] == '@':
                break

            if D[nx_h][nx_w] <= D[nh][nw]:
                break

            if D[nx_h][nx_w] > D[nh][nw] + 1:
                D[nx_h][nx_w] = D[nh][nw] + 1
                que.appendleft((nx_h, nx_w))

print(D[gh][gw] if D[gh][gw] != INF else -1)