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

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

    G = [['#'] * (W + 2)]
    G_append = G.append
    for _ in range(H):
        tmp = ['#'] + list(input()) + ['#']
        G_append(tmp)

    G.append(['#'] * (W + 2))
    INF = 10 ** 6+ 10
    dp = [[INF] * (W + 2) for _ in range(H + 2)]

    que = deque()
    que_append = que.append
    que_popleft = que.popleft

    que_append((x1, y1))
    dp[x1][y1] = 0
    while que:
        xy = que_popleft()
        x = xy[0]
        y = xy[1]
        tmp = dp[x][y] + 1
        # print ('x, y', x, y)
        if dp[x][y] == dp[x + 1][y] and dp[x][y] != INF:
            pass
        else:
            for k in range(1, K + 1):
                if G[x + k][y] == '.':
                    if dp[x + k][y] > tmp:
                        dp[x + k][y] = tmp
                        que_append((x + k, y))
                else:
                    break
        if dp[x][y] == dp[x - 1][y] and dp[x][y] != INF:
            pass
        else:
            for k in range(1, K + 1):
                if G[x - k][y] == '.':
                    if dp[x - k][y] > tmp:
                        dp[x - k][y] = tmp
                        que_append((x - k, y))
                else:
                    break
        if dp[x][y] == dp[x][y + 1] and dp[x][y] != INF:
            pass
        else:
            for k in range(1, K + 1):
                if G[x][y + k] == '.':
                    if dp[x][y + k] > tmp:
                        dp[x][y + k] = tmp
                        que_append((x, y + k))
                else:
                    break

        if dp[x][y] == dp[x][y - 1] and dp[x][y] != INF:
            pass
        else:
            for k in range(1, K + 1):
                if G[x][y - k] == '.':
                    if dp[x][y - k] > tmp:
                        dp[x][y - k] = tmp
                        que_append((x, y - k))
                else:
                    break

    if dp[x2][y2] != INF:
        print (dp[x2][y2])
    else:
        print (-1)

    # for tmp in dp:
    #     print (tmp)

if __name__ == '__main__':
    main()

Fixed submission:
from collections import deque

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

G = [['#'] * (W + 2)]
for _ in range(H):
    tmp = ['#'] + list(input()) + ['#']
    G.append(tmp)

G.append(['#'] * (W + 2))
INF = 10 ** 6+ 10
dp = [[INF] * (W + 2) for _ in range(H + 2)]

que = deque()
que_append = que.append
que_popleft = que.popleft

que_append((x1, y1))
dp[x1][y1] = 0
while que:
    xy = que_popleft()
    x = xy[0]
    y = xy[1]
    tmp = dp[x][y] + 1
    # print ('x, y', x, y)
    if dp[x][y] == dp[x + 1][y] and dp[x][y] != INF:
        pass
    else:
        for k in range(1, K + 1):
            if G[x + k][y] == '.':
                if dp[x + k][y] > tmp:
                    dp[x + k][y] = tmp
                    que_append((x + k, y))
            else:
                break
    if dp[x][y] == dp[x - 1][y] and dp[x][y] != INF:
        pass
    else:
        for k in range(1, K + 1):
            if G[x - k][y] == '.':
                if dp[x - k][y] > tmp:
                    dp[x - k][y] = tmp
                    que_append((x - k, y))
            else:
                break
    if dp[x][y] == dp[x][y + 1] and dp[x][y] != INF:
        pass
    else:
        for k in range(1, K + 1):
            if G[x][y + k] == '.':
                if dp[x][y + k] > tmp:
                    dp[x][y + k] = tmp
                    que_append((x, y + k))
            else:
                break

    if dp[x][y] == dp[x][y - 1] and dp[x][y] != INF:
        pass
    else:
        for k in range(1, K + 1):
            if G[x][y - k] == '.':
                if dp[x][y - k] > tmp:
                    dp[x][y - k] = tmp
                    que_append((x, y - k))
            else:
                break

if dp[x2][y2] != INF:
    print (dp[x2][y2])
else:
    print (-1)

# for tmp in dp:
#     print (tmp)

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())

G = [['#'] * (W + 2)]
for _ in range(H):
    tmp = ['#'] + list(input()) + ['#']
    G.append(tmp)

G.append(['#'] * (W + 2))
INF = 10 ** 6+ 10
dp = [[INF] * (W + 2) for _ in range(H + 2)]

que = deque()
que_append = que.append
que_popleft = que.popleft

que_append((x1, y1))
dp[x1][y1] = 0
while que:
    xy = que_popleft()
    x = xy[0]
    y = xy[1]
    tmp = dp[x][y] + 1
    # print ('x, y', x, y)
    if dp[x][y] == dp[x + 1][y] and dp[x][y] != INF:
        pass
    else:
        for k in range(1, K + 1):
            if G[x + k][y] == '.':
                if dp[x + k][y] > tmp:
                    dp[x + k][y] = tmp
                    que_append((x + k, y))
            else:
                break
    if dp[x][y] == dp[x - 1][y] and dp[x][y] != INF:
        pass
    else:
        for k in range(1, K + 1):
            if G[x - k][y] == '.':
                if dp[x - k][y] > tmp:
                    dp[x - k][y] = tmp
                    que_append((x - k, y))
            else:
                break
    if dp[x][y] == dp[x][y + 1] and dp[x][y] != INF:
        pass
    else:
        for k in range(1, K + 1):
            if G[x][y + k] == '.':
                if dp[x][y + k] > tmp:
                    dp[x][y + k] = tmp
                    que_append((x, y + k))
            else:
                break

    if dp[x][y] == dp[x][y - 1] and dp[x][y] != INF:
        pass
    else:
        for k in range(1, K + 1):
            if G[x][y - k] == '.':
                if dp[x][y - k] > tmp:
                    dp[x][y - k] = tmp
                    que_append((x, y - k))
            else:
                break

if dp[x2][y2] != INF:
    print (dp[x2][y2])
else:
    print (-1)

# for tmp in dp:
#     print (tmp)

Fixed submission:
from collections import deque

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

G = [['#'] * (W + 2)]
for _ in range(H):
    tmp = ['#'] + list(input()) + ['#']
    G.append(tmp)

G.append(['#'] * (W + 2))
INF = 10 ** 6+ 10
dp = [[INF] * (W + 2) for _ in range(H + 2)]

que = deque()
que_append = que.append
que_popleft = que.popleft

que_append((x1, y1))
dp[x1][y1] = 0
while que:
    xy = que_popleft()
    x = xy[0]
    y = xy[1]
    tmp = dp[x][y] + 1
    # print ('x, y', x, y)
    if dp[x][y] == dp[x + 1][y] and dp[x][y] != INF:
        pass
    else:
        for k in range(1, K + 1):
            if G[x + k][y] == '.':
                if dp[x + k][y] > tmp:
                    dp[x + k][y] = tmp
                    que_append((x + k, y))
            else:
                break
    if dp[x][y] == dp[x - 1][y] and dp[x][y] != INF:
        pass
    else:
        for k in range(1, K + 1):
            if G[x - k][y] == '.':
                if dp[x - k][y] > tmp:
                    dp[x - k][y] = tmp
                    que_append((x - k, y))
            else:
                break
    if dp[x][y] == dp[x][y + 1] and dp[x][y] != INF:
        pass
    else:
        for k in range(1, K + 1):
            if G[x][y + k] == '.':
                if dp[x][y + k] > tmp:
                    dp[x][y + k] = tmp
                    que_append((x, y + k))
            else:
                break

    if dp[x][y] == dp[x][y - 1] and dp[x][y] != INF:
        pass
    else:
        for k in range(1, K + 1):
            if G[x][y - k] == '.':
                if dp[x][y - k] > tmp:
                    dp[x][y - k] = tmp
                    que_append((x, y - k))
            else:
                break

if dp[x2][y2] != INF:
    print (dp[x2][y2])
else:
    print (-1)

# for tmp in dp:
#     print (tmp)

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())

G = [['#'] * (W + 2)]
for _ in range(H):
    tmp = ['#'] + list(input()) + ['#']
    G.append(tmp)

G.append(['#'] * (W + 2))
INF = 10 ** 6+ 10
dp = [[INF] * (W + 2) for _ in range(H + 2)]

que = deque()
que_append = que.append
que_popleft = que.popleft

que_append((x1, y1))
dp[x1][y1] = 0
while que:
    xy = que_popleft()
    x = xy[0]
    y = xy[1]
    tmp = dp[x][y] + 1

    for k in range(1, K + 1):
        if G[x + k][y] == '.':
            if dp[x + k][y] > tmp:
                dp[x + k][y] = tmp
                que_append((x + k, y))
        else:
            break

    for k in range(1, K + 1):
        if G[x - k][y] == '.':
            if dp[x - k][y] > tmp:
                dp[x - k][y] = tmp
                que_append((x - k, y))
        else:
            break

    for k in range(1, K + 1):
        if G[x][y + k] == '.':
            if dp[x][y + k] > tmp:
                dp[x][y + k] = tmp
                que_append((x, y + k))
        else:
            break

    for k in range(1, K + 1):
        if G[x][y - k] == '.':
            if dp[x][y - k] > tmp:
                dp[x][y - k] = tmp
                que_append((x, y - k))
        else:
            break

if dp[x2][y2] != INF:
    print (dp[x2][y2])
else:
    print (-1)

# for tmp in dp:
#     print (tmp)

Fixed submission:
from collections import deque

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

G = [['#'] * (W + 2)]
for _ in range(H):
    tmp = ['#'] + list(input()) + ['#']
    G.append(tmp)

G.append(['#'] * (W + 2))
INF = 10 ** 6+ 10
dp = [[INF] * (W + 2) for _ in range(H + 2)]

que = deque()
que_append = que.append
que_popleft = que.popleft

que_append((x1, y1))
dp[x1][y1] = 0
while que:
    xy = que_popleft()
    x = xy[0]
    y = xy[1]
    tmp = dp[x][y] + 1
    # print ('x, y', x, y)
    if dp[x][y] == dp[x + 1][y] and dp[x][y] != INF:
        pass
    else:
        for k in range(1, K + 1):
            if G[x + k][y] == '.':
                if dp[x + k][y] > tmp:
                    dp[x + k][y] = tmp
                    que_append((x + k, y))
            else:
                break
    if dp[x][y] == dp[x - 1][y] and dp[x][y] != INF:
        pass
    else:
        for k in range(1, K + 1):
            if G[x - k][y] == '.':
                if dp[x - k][y] > tmp:
                    dp[x - k][y] = tmp
                    que_append((x - k, y))
            else:
                break
    if dp[x][y] == dp[x][y + 1] and dp[x][y] != INF:
        pass
    else:
        for k in range(1, K + 1):
            if G[x][y + k] == '.':
                if dp[x][y + k] > tmp:
                    dp[x][y + k] = tmp
                    que_append((x, y + k))
            else:
                break

    if dp[x][y] == dp[x][y - 1] and dp[x][y] != INF:
        pass
    else:
        for k in range(1, K + 1):
            if G[x][y - k] == '.':
                if dp[x][y - k] > tmp:
                    dp[x][y - k] = tmp
                    que_append((x, y - k))
            else:
                break

if dp[x2][y2] != INF:
    print (dp[x2][y2])
else:
    print (-1)

# for tmp in dp:
#     print (tmp)