Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque

H, W, K = map(int, input().split())
x1, y1, x2, y2 = map(int, input().split())

c = [input().rstrip() for _ in range(H)]

d = deque([(0, x1-1, y1-1)])

dp = [[False] * W for _ in range(H)]
dp[x1-1][y1-1] = True

while d:
    step, x, y = d.popleft()
    if x == x2-1 and y == y2-1:
        print(step)
        break

    for dx, dy in [(0, 1), (0, -1), (1, 0), (-1, 0)]:
        for i in range(1, K+1):
            nx = x + dx * i
            ny = y + dy * i
            if 0 <= nx < H and 0 <= ny < W:
                if c[nx][ny] == '@':
                    break

                if not dp[nx][ny]:
                    dp[nx][ny] = True
                    d.append((step + 1, nx, ny))
            else:
                break
else:
    print(-1)


Fixed submission:
from collections import deque

H, W, K = map(int, input().split())
x1, y1, x2, y2 = map(int, input().split())

c = [input().rstrip() for _ in range(H)]

d = deque([(x1-1, y1-1)])

dp = [[-1] * W for _ in range(H)]
dp[x1-1][y1-1] = 0

while d:
    x, y = d.popleft()
    if x == x2-1 and y == y2-1:
        print(dp[x][y])
        break

    for dx, dy in [(0, 1), (0, -1), (1, 0), (-1, 0)]:
        for i in range(1, K+1):
            nx = x + dx * i
            ny = y + dy * i

            if 0 > nx or nx >= H or 0 > ny or ny >= W or c[nx][ny] == '@':
                break

            if dp[nx][ny] == -1:
                dp[nx][ny] = dp[x][y] + 1
                d.append((nx, ny))
            elif dp[nx][ny] <= dp[x][y]:
                break
else:
    print(-1)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque

H, W, K = map(int, input().split())
x1, y1, x2, y2 = map(int, input().split())

c = [input().rstrip() for _ in range(H)]

d = deque([(0, x1-1, y1-1)])
dp = [[False] * W for _ in range(H)]

while d:
    step, x, y = d.popleft()
    if x == x2-1 and y == y2-1:
        print(step)
        break

    for dx, dy in [(0, 1), (0, -1), (1, 0), (-1, 0)]:
        for i in range(1, K+1):
            nx = x + dx * i
            ny = y + dy * i
            if 0 <= nx < H and 0 <= ny < W:
                if c[nx][ny] == '@':
                    break

                if not dp[nx][ny]:
                    dp[nx][ny] = True
                    d.append((step + 1, nx, ny))
            else:
                break
else:
    print(-1)


Fixed submission:
from collections import deque

H, W, K = map(int, input().split())
x1, y1, x2, y2 = map(int, input().split())

c = [input().rstrip() for _ in range(H)]

d = deque([(x1-1, y1-1)])

dp = [[-1] * W for _ in range(H)]
dp[x1-1][y1-1] = 0

while d:
    x, y = d.popleft()
    if x == x2-1 and y == y2-1:
        print(dp[x][y])
        break

    for dx, dy in [(0, 1), (0, -1), (1, 0), (-1, 0)]:
        for i in range(1, K+1):
            nx = x + dx * i
            ny = y + dy * i

            if 0 > nx or nx >= H or 0 > ny or ny >= W or c[nx][ny] == '@':
                break

            if dp[nx][ny] == -1:
                dp[nx][ny] = dp[x][y] + 1
                d.append((nx, ny))
            elif dp[nx][ny] <= dp[x][y]:
                break
else:
    print(-1)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque

H, W, K = map(int, input().split())
x1, y1, x2, y2 = map(int, input().split())

c = []
for i in range(H):
    c.append(input().rstrip())

d = deque([(0, x1-1, y1-1)])

dp = [[False] * W for _ in range(H)]


def check(step, x, y):
    if c[x][y] == '@':
        return False

    if not dp[x][y]:
        dp[x][y] = True
        d.append((step + 1, x, y))

    return True


while d:
    step, x, y = d.popleft()
    if x == x2-1 and y == y2-1:
        print(step)
        break

    for i in range(x+1, min(x+K+1, H)):
        if not check(step, i, y):
            break

    for i in range(x-1, max(-1, x-K-1), -1):
        if not check(step, i, y):
            break

    for j in range(y+1, min(y+K+1, W)):
        if not check(step, x, j):
            break

    for j in range(y-1, max(-1, y-K-1), -1):
        if not check(step, x, j):
            break
else:
    print(-1)


Fixed submission:
from collections import deque

H, W, K = map(int, input().split())
x1, y1, x2, y2 = map(int, input().split())

c = [input().rstrip() for _ in range(H)]

d = deque([(x1-1, y1-1)])

dp = [[-1] * W for _ in range(H)]
dp[x1-1][y1-1] = 0

while d:
    x, y = d.popleft()
    if x == x2-1 and y == y2-1:
        print(dp[x][y])
        break

    for dx, dy in [(0, 1), (0, -1), (1, 0), (-1, 0)]:
        for i in range(1, K+1):
            nx = x + dx * i
            ny = y + dy * i

            if 0 > nx or nx >= H or 0 > ny or ny >= W or c[nx][ny] == '@':
                break

            if dp[nx][ny] == -1:
                dp[nx][ny] = dp[x][y] + 1
                d.append((nx, ny))
            elif dp[nx][ny] <= dp[x][y]:
                break
else:
    print(-1)

Buggy python submission (verdict: Time Limit Exceeded):
from heapq import heappop, heappush

H, W, K = map(int, input().split())
x1, y1, x2, y2 = map(int, input().split())

c = []
for i in range(H):
    c.append(input().rstrip())

h = [(0, x1-1, y1-1)]

dp = [[False] * W for _ in range(H)]


def check(step, x, y):
    if c[x][y] == '@':
        return False

    if not dp[x][y]:
        dp[x][y] = True
        heappush(h, (step + 1, x, y))

    return True


while h:
    step, x, y = heappop(h)
    if x == x2-1 and y == y2-1:
        print(step)
        break

    for i in range(x+1, min(x+K+1, H)):
        if not check(step, i, y):
            break

    for i in range(x-1, max(-1, x-K-1), -1):
        if not check(step, i, y):
            break

    for j in range(y+1, min(y+K+1, W)):
        if not check(step, x, j):
            break

    for j in range(y-1, max(-1, y-K-1), -1):
        if not check(step, x, j):
            break
else:
    print(-1)


Fixed submission:
from collections import deque

H, W, K = map(int, input().split())
x1, y1, x2, y2 = map(int, input().split())

c = [input().rstrip() for _ in range(H)]

d = deque([(x1-1, y1-1)])

dp = [[-1] * W for _ in range(H)]
dp[x1-1][y1-1] = 0

while d:
    x, y = d.popleft()
    if x == x2-1 and y == y2-1:
        print(dp[x][y])
        break

    for dx, dy in [(0, 1), (0, -1), (1, 0), (-1, 0)]:
        for i in range(1, K+1):
            nx = x + dx * i
            ny = y + dy * i

            if 0 > nx or nx >= H or 0 > ny or ny >= W or c[nx][ny] == '@':
                break

            if dp[nx][ny] == -1:
                dp[nx][ny] = dp[x][y] + 1
                d.append((nx, ny))
            elif dp[nx][ny] <= dp[x][y]:
                break
else:
    print(-1)