Buggy python submission (verdict: Time Limit Exceeded):
h, w, k = map(int, input().split())
x1, y1, x2, y2 = map(int, input().split())
x1, y1, x2, y2 = x1-1, y1-1, x2-1, y2-1

m = [[-1] * w for _ in range(h)]
for hh in range(h):
    c = input()
    for ww, cc in enumerate(c):
        if cc == '.':
            m[hh][ww] = h*w

from collections import deque
q = deque()

q.append((0, x1, y1))
while q:
    s, x, y = q.popleft()

    for i in range(1, k+1):
        xx, yy = x + i, y
        if xx >= h or m[xx][yy] == -1:
            break
        if m[xx][yy] <= s+1:
            continue
        if xx == x2 and yy == y2:
            print(s+1)
            exit(0)
        m[xx][yy] = s+1
        q.append((s+1, xx, yy))

    for i in range(1, k+1):
        xx, yy = x - i, y
        if xx < 0 or m[xx][yy] == -1:
            break
        if m[xx][yy] <= s+1:
            continue
        if xx == x2 and yy == y2:
            print(s+1)
            exit(0)
        m[xx][yy] = s+1
        q.append((s+1, xx, yy))

    for i in range(1, k+1):
        xx, yy = x, y + i
        if yy >= w or m[xx][yy] == -1:
            break
        if m[xx][yy] <= s+1:
            continue
        if xx == x2 and yy == y2:
            print(s+1)
            exit(0)
        m[xx][yy] = s+1
        q.append((s+1, xx, yy))

    for i in range(1, k+1):
        xx, yy = x, y - i
        if yy < 0 or m[xx][yy] == -1:
            break
        if m[xx][yy] <= s+1:
            continue
        if xx == x2 and yy == y2:
            print(s+1)
            exit(0)
        m[xx][yy] = s+1
        q.append((s+1, xx, yy))

print(-1)


Fixed submission:
h, w, k = map(int, input().split())
x1, y1, x2, y2 = map(int, input().split())
x1, y1, x2, y2 = x1-1, y1-1, x2-1, y2-1

m = [[-1] * w for _ in range(h)]
for hh in range(h):
    c = input()
    for ww, cc in enumerate(c):
        if cc == '.':
            m[hh][ww] = h*w

from collections import deque
q = deque()

q.append((0, x1, y1))
while q:
    s, x, y = q.popleft()
    s1 = s + 1

    for i in range(1, k+1):
        xx, yy = x + i, y
        if xx >= h or m[xx][yy] <= s:
            break
        if m[xx][yy] == s1:
            continue
        if xx == x2 and yy == y2:
            print(s1)
            exit(0)
        m[xx][yy] = s1
        q.append((s1, xx, yy))

    for i in range(1, k+1):
        xx, yy = x - i, y
        if xx < 0 or m[xx][yy] <= s:
            break
        if m[xx][yy] == s1:
            continue
        if xx == x2 and yy == y2:
            print(s1)
            exit(0)
        m[xx][yy] = s1
        q.append((s1, xx, yy))

    for i in range(1, k+1):
        xx, yy = x, y + i
        if yy >= w or m[xx][yy] <= s:
            break
        if m[xx][yy] == s1:
            continue
        if xx == x2 and yy == y2:
            print(s1)
            exit(0)
        m[xx][yy] = s1
        q.append((s1, xx, yy))

    for i in range(1, k+1):
        xx, yy = x, y - i
        if yy < 0 or m[xx][yy] <= s:
            break
        if m[xx][yy] == s1:
            continue
        if xx == x2 and yy == y2:
            print(s1)
            exit(0)
        m[xx][yy] = s1
        q.append((s1, xx, yy))

print(-1)

Buggy python submission (verdict: Time Limit Exceeded):
h, w, k = map(int, input().split())
x1, y1, x2, y2 = map(int, input().split())
x1, y1, x2, y2 = x1-1, y1-1, x2-1, y2-1

xxx = set()
for hh in range(h):
    c = input()
    for ww, cc in enumerate(c):
        if cc == '@':
            xxx.add((hh, ww))

from collections import deque
q = deque()

q.append((0, x1, y1))
d = {}
d[(x1, y1)] = 0
while q:
    s, x, y = q.popleft()

    for i in range(1, k+1):
        xx, yy = x + i, y
        if xx >= h or (xx, yy) in xxx:
            break
        if (xx, yy) in d and d[(xx, yy)] <= s+1:
            continue
        if xx == x2 and yy == y2:
            print(s+1)
            exit(0)
        d[(xx, yy)] = s+1
        q.append((s+1, xx, yy))

    for i in range(1, k+1):
        xx, yy = x - i, y
        if xx < 0 or (xx, yy) in xxx:
            break
        if (xx, yy) in d and d[(xx, yy)] <= s+1:
            continue
        if xx == x2 and yy == y2:
            print(s+1)
            exit(0)
        d[(xx, yy)] = s+1
        q.append((s+1, xx, yy))

    for i in range(1, k+1):
        xx, yy = x, y + i
        if yy >= w or (xx, yy) in xxx:
            break
        if (xx, yy) in d and d[(xx, yy)] <= s+1:
            continue
        if xx == x2 and yy == y2:
            print(s+1)
            exit(0)
        d[(xx, yy)] = s+1
        q.append((s+1, xx, yy))

    for i in range(1, k+1):
        xx, yy = x, y - i
        if yy < 0 or (xx, yy) in xxx:
            break
        if (xx, yy) in d and d[(xx, yy)] <= s+1:
            continue
        if xx == x2 and yy == y2:
            print(s+1)
            exit(0)
        d[(xx, yy)] = s+1
        q.append((s+1, xx, yy))

print(-1)


Fixed submission:
h, w, k = map(int, input().split())
x1, y1, x2, y2 = map(int, input().split())
x1, y1, x2, y2 = x1-1, y1-1, x2-1, y2-1

m = [[-1] * w for _ in range(h)]
for hh in range(h):
    c = input()
    for ww, cc in enumerate(c):
        if cc == '.':
            m[hh][ww] = h*w

from collections import deque
q = deque()

q.append((0, x1, y1))
while q:
    s, x, y = q.popleft()
    s1 = s + 1

    for i in range(1, k+1):
        xx, yy = x + i, y
        if xx >= h or m[xx][yy] <= s:
            break
        if m[xx][yy] == s1:
            continue
        if xx == x2 and yy == y2:
            print(s1)
            exit(0)
        m[xx][yy] = s1
        q.append((s1, xx, yy))

    for i in range(1, k+1):
        xx, yy = x - i, y
        if xx < 0 or m[xx][yy] <= s:
            break
        if m[xx][yy] == s1:
            continue
        if xx == x2 and yy == y2:
            print(s1)
            exit(0)
        m[xx][yy] = s1
        q.append((s1, xx, yy))

    for i in range(1, k+1):
        xx, yy = x, y + i
        if yy >= w or m[xx][yy] <= s:
            break
        if m[xx][yy] == s1:
            continue
        if xx == x2 and yy == y2:
            print(s1)
            exit(0)
        m[xx][yy] = s1
        q.append((s1, xx, yy))

    for i in range(1, k+1):
        xx, yy = x, y - i
        if yy < 0 or m[xx][yy] <= s:
            break
        if m[xx][yy] == s1:
            continue
        if xx == x2 and yy == y2:
            print(s1)
            exit(0)
        m[xx][yy] = s1
        q.append((s1, xx, yy))

print(-1)

Buggy python submission (verdict: Time Limit Exceeded):
h, w, k = map(int, input().split())
x1, y1, x2, y2 = map(int, input().split())
x1, y1, x2, y2 = x1-1, y1-1, x2-1, y2-1

xxx = set()
for hh in range(h):
    c = input()
    for ww, cc in enumerate(c):
        if cc == '@':
            xxx.add((hh, ww))

from heapq import heappush, heappop
q = []
heappush(q, (0, x1, y1))
d = {}
while q:
    s, x, y = heappop(q)

    for i in range(1, k+1):
        xx, yy = x + i, y
        if xx >= h or (xx, yy) in xxx:
            break
        if (xx, yy) in d and d[(xx, yy)] <= s+1:
            continue
        if xx == x2 and yy == y2:
            print(s+1)
            exit(0)
        d[(xx, yy)] = s+1
        heappush(q, (s+1, xx, yy))

    for i in range(1, k+1):
        xx, yy = x - i, y
        if xx < 0 or (xx, yy) in xxx:
            break
        if (xx, yy) in d and d[(xx, yy)] <= s+1:
            continue
        if xx == x2 and yy == y2:
            print(s+1)
            exit(0)
        d[(xx, yy)] = s+1
        heappush(q, (s+1, xx, yy))

    for i in range(1, k+1):
        xx, yy = x, y + i
        if yy >= w or (xx, yy) in xxx:
            break
        if (xx, yy) in d and d[(xx, yy)] <= s+1:
            continue
        if xx == x2 and yy == y2:
            print(s+1)
            exit(0)
        d[(xx, yy)] = s+1
        heappush(q, (s+1, xx, yy))

    for i in range(1, k+1):
        xx, yy = x, y - i
        if yy < 0 or (xx, yy) in xxx:
            break
        if (xx, yy) in d and d[(xx, yy)] <= s+1:
            continue
        if xx == x2 and yy == y2:
            print(s+1)
            exit(0)
        d[(xx, yy)] = s+1
        heappush(q, (s+1, xx, yy))

print(-1)


Fixed submission:
h, w, k = map(int, input().split())
x1, y1, x2, y2 = map(int, input().split())
x1, y1, x2, y2 = x1-1, y1-1, x2-1, y2-1

m = [[-1] * w for _ in range(h)]
for hh in range(h):
    c = input()
    for ww, cc in enumerate(c):
        if cc == '.':
            m[hh][ww] = h*w

from collections import deque
q = deque()

q.append((0, x1, y1))
while q:
    s, x, y = q.popleft()
    s1 = s + 1

    for i in range(1, k+1):
        xx, yy = x + i, y
        if xx >= h or m[xx][yy] <= s:
            break
        if m[xx][yy] == s1:
            continue
        if xx == x2 and yy == y2:
            print(s1)
            exit(0)
        m[xx][yy] = s1
        q.append((s1, xx, yy))

    for i in range(1, k+1):
        xx, yy = x - i, y
        if xx < 0 or m[xx][yy] <= s:
            break
        if m[xx][yy] == s1:
            continue
        if xx == x2 and yy == y2:
            print(s1)
            exit(0)
        m[xx][yy] = s1
        q.append((s1, xx, yy))

    for i in range(1, k+1):
        xx, yy = x, y + i
        if yy >= w or m[xx][yy] <= s:
            break
        if m[xx][yy] == s1:
            continue
        if xx == x2 and yy == y2:
            print(s1)
            exit(0)
        m[xx][yy] = s1
        q.append((s1, xx, yy))

    for i in range(1, k+1):
        xx, yy = x, y - i
        if yy < 0 or m[xx][yy] <= s:
            break
        if m[xx][yy] == s1:
            continue
        if xx == x2 and yy == y2:
            print(s1)
            exit(0)
        m[xx][yy] = s1
        q.append((s1, xx, yy))

print(-1)