Buggy python submission (verdict: Time Limit Exceeded):
from queue import Queue


def main():
    h, w, k = map(int, input().split())
    sx, sy, gx, gy = map(lambda z: int(z) - 1, input().split())
    c = [list(input().replace('\n', '')) for _ in range(h)]
    c[sx][sy] = 0
    q = Queue()
    q.put((sx, sy))
    while not q.empty():
        x, y = q.get()
        d = c[x][y]
        # 南へ移動
        for i in range(1, k + 1):
            if x + i >= h:
                break
            if c[x + i][y] == '@':
                break
            elif isinstance(c[x + i][y], int) and c[x + i][y] < d + 1:
                break
            elif isinstance(c[x + i][y], int) and c[x + i][y] == d + 1:
                continue
            else:
                c[x + i][y] = d + 1
                q.put((x + i, y))
        # 北へ移動
        for i in range(1, k + 1):
            if x - i < 0:
                break
            if c[x - i][y] == '@':
                break
            elif isinstance(c[x - i][y], int) and c[x - i][y] < d + 1:
                break
            elif isinstance(c[x - i][y], int) and c[x - i][y] == d + 1:
                continue
            else:
                c[x - i][y] = d + 1
                q.put((x - i, y))
        # 東へ移動
        for i in range(1, k + 1):
            if y + i >= w:
                break
            if c[x][y + i] == '@':
                break
            elif isinstance(c[x][y + i], int) and c[x][y + i] < d + 1:
                break
            elif isinstance(c[x][y + i], int) and c[x][y + i] == d + 1:
                continue
            else:
                c[x][y + i] = d + 1
                q.put((x, y + i))
        # 西へ移動
        for i in range(1, k + 1):
            if y - i < 0:
                break
            if c[x][y - i] == '@':
                break
            elif isinstance(c[x][y - i], int) and c[x][y - i] < d + 1:
                break
            elif isinstance(c[x][y - i], int) and c[x][y - i] == d + 1:
                continue
            else:
                c[x][y - i] = d + 1
                q.put((x, y - i))
    print(-1 if c[gx][gy] == '.' else c[gx][gy])


if __name__ == '__main__':
    main()


Fixed submission:
from queue import Queue


def main():
    h, w, k = map(int, input().split())
    sx, sy, gx, gy = map(lambda z: int(z) - 1, input().split())
    c = [list(input().replace('\n', '')) for _ in range(h)]
    c[sx][sy] = 0
    q = Queue()
    q.put((sx, sy))
    while not q.empty():
        x, y = q.get()
        d = c[x][y]
        # 南へ移動
        for i in range(1, k + 1):
            if x + i >= h:
                break
            if c[x + i][y] == '@':
                break
            elif isinstance(c[x + i][y], int) and c[x + i][y] < d + 1:
                break
            elif isinstance(c[x + i][y], int) and c[x + i][y] == d + 1:
                continue
            else:
                c[x + i][y] = d + 1
                q.put((x + i, y))
        # 北へ移動
        for i in range(1, k + 1):
            if x - i < 0:
                break
            if c[x - i][y] == '@':
                break
            elif isinstance(c[x - i][y], int) and c[x - i][y] < d + 1:
                break
            elif isinstance(c[x - i][y], int) and c[x - i][y] == d + 1:
                continue
            else:
                c[x - i][y] = d + 1
                q.put((x - i, y))
        # 東へ移動
        for i in range(1, k + 1):
            if y + i >= w:
                break
            if c[x][y + i] == '@':
                break
            elif isinstance(c[x][y + i], int) and c[x][y + i] < d + 1:
                break
            elif isinstance(c[x][y + i], int) and c[x][y + i] == d + 1:
                continue
            else:
                c[x][y + i] = d + 1
                q.put((x, y + i))
        # 西へ移動
        for i in range(1, k + 1):
            if y - i < 0:
                break
            if c[x][y - i] == '@':
                break
            elif isinstance(c[x][y - i], int) and c[x][y - i] < d + 1:
                break
            elif isinstance(c[x][y - i], int) and c[x][y - i] < d + 1:
                continue
            else:
                c[x][y - i] = d + 1
                q.put((x, y - i))
    print(-1 if c[gx][gy] == '.' else c[gx][gy])


if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
from queue import Queue


def main():
    h, w, k = map(int, input().split())
    sx, sy, gx, gy = map(lambda z: int(z) - 1, input().split())
    c = [list(input().replace('\n', '')) for _ in range(h)]
    c[sx][sy] = 0
    q = Queue()
    q.put((sx, sy))
    while not q.empty():
        x, y = q.get()
        d = c[x][y]
        # 南へ移動
        for i in range(1, k + 1):
            if x + i >= h:
                break
            if c[x + i][y] == '@':
                break
            elif isinstance(c[x + i][y], int) and c[x + i][y] < d + 1:
                break
            else:
                c[x + i][y] = d + 1
                q.put((x + i, y))
        # 北へ移動
        for i in range(1, k + 1):
            if x - i < 0:
                break
            if c[x - i][y] == '@':
                break
            elif isinstance(c[x - i][y], int) and c[x - i][y] < d + 1:
                break
            else:
                c[x - i][y] = d + 1
                q.put((x - i, y))
        # 東へ移動
        for i in range(1, k + 1):
            if y + i >= w:
                break
            if c[x][y + i] == '@':
                break
            elif isinstance(c[x][y + i], int) and c[x][y + i] < d + 1:
                break
            else:
                c[x][y + i] = d + 1
                q.put((x, y + i))
        # 西へ移動
        for i in range(1, k + 1):
            if y - i < 0:
                break
            if c[x][y - i] == '@':
                break
            elif isinstance(c[x][y - i], int) and c[x][y - i] < d + 1:
                break
            else:
                c[x][y - i] = d + 1
                q.put((x, y - i))
    print(-1 if c[gx][gy] == '.' else c[gx][gy])


if __name__ == '__main__':
    main()


Fixed submission:
from queue import Queue


def main():
    h, w, k = map(int, input().split())
    sx, sy, gx, gy = map(lambda z: int(z) - 1, input().split())
    c = [list(input().replace('\n', '')) for _ in range(h)]
    c[sx][sy] = 0
    q = Queue()
    q.put((sx, sy))
    while not q.empty():
        x, y = q.get()
        d = c[x][y]
        # 南へ移動
        for i in range(1, k + 1):
            if x + i >= h:
                break
            if c[x + i][y] == '@':
                break
            elif isinstance(c[x + i][y], int) and c[x + i][y] < d + 1:
                break
            elif isinstance(c[x + i][y], int) and c[x + i][y] == d + 1:
                continue
            else:
                c[x + i][y] = d + 1
                q.put((x + i, y))
        # 北へ移動
        for i in range(1, k + 1):
            if x - i < 0:
                break
            if c[x - i][y] == '@':
                break
            elif isinstance(c[x - i][y], int) and c[x - i][y] < d + 1:
                break
            elif isinstance(c[x - i][y], int) and c[x - i][y] == d + 1:
                continue
            else:
                c[x - i][y] = d + 1
                q.put((x - i, y))
        # 東へ移動
        for i in range(1, k + 1):
            if y + i >= w:
                break
            if c[x][y + i] == '@':
                break
            elif isinstance(c[x][y + i], int) and c[x][y + i] < d + 1:
                break
            elif isinstance(c[x][y + i], int) and c[x][y + i] == d + 1:
                continue
            else:
                c[x][y + i] = d + 1
                q.put((x, y + i))
        # 西へ移動
        for i in range(1, k + 1):
            if y - i < 0:
                break
            if c[x][y - i] == '@':
                break
            elif isinstance(c[x][y - i], int) and c[x][y - i] < d + 1:
                break
            elif isinstance(c[x][y - i], int) and c[x][y - i] < d + 1:
                continue
            else:
                c[x][y - i] = d + 1
                q.put((x, y - i))
    print(-1 if c[gx][gy] == '.' else c[gx][gy])


if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
from queue import Queue


def main():
    h, w, k = map(int, input().split())
    sx, sy, gx, gy = map(lambda z: int(z) - 1, input().split())
    c = [list(input().replace('\n', '')) for _ in range(h)]
    c[sx][sy] = 0
    q = Queue()
    q.put((sx, sy))
    while not q.empty():
        x, y = q.get()
        d = c[x][y]
        # 南へ移動
        for i in range(1, k + 1):
            if x + i >= h:
                break
            if c[x + i][y] == '@':
                break
            elif isinstance(c[x + i][y], int) and c[x + i][y] <= d + 1:
                continue
            else:
                c[x + i][y] = d + 1
                q.put((x + i, y))
        # 北へ移動
        for i in range(1, k + 1):
            if x - i < 0:
                break
            if c[x - i][y] == '@':
                break
            elif isinstance(c[x - i][y], int) and c[x - i][y] <= d + 1:
                continue
            else:
                c[x - i][y] = d + 1
                q.put((x - i, y))
        # 東へ移動
        for i in range(1, k + 1):
            if y + i >= w:
                break
            if c[x][y + i] == '@':
                break
            elif isinstance(c[x][y + i], int) and c[x][y + i] <= d + 1:
                continue
            else:
                c[x][y + i] = d + 1
                q.put((x, y + i))
        # 西へ移動
        for i in range(1, k + 1):
            if y - i < 0:
                break
            if c[x][y - i] == '@':
                break
            elif isinstance(c[x][y - i], int) and c[x][y - i] <= d + 1:
                continue
            else:
                c[x][y - i] = d + 1
                q.put((x, y - i))
    print(-1 if c[gx][gy] == '.' else c[gx][gy])


if __name__ == '__main__':
    main()


Fixed submission:
from queue import Queue


def main():
    h, w, k = map(int, input().split())
    sx, sy, gx, gy = map(lambda z: int(z) - 1, input().split())
    c = [list(input().replace('\n', '')) for _ in range(h)]
    c[sx][sy] = 0
    q = Queue()
    q.put((sx, sy))
    while not q.empty():
        x, y = q.get()
        d = c[x][y]
        # 南へ移動
        for i in range(1, k + 1):
            if x + i >= h:
                break
            if c[x + i][y] == '@':
                break
            elif isinstance(c[x + i][y], int) and c[x + i][y] < d + 1:
                break
            elif isinstance(c[x + i][y], int) and c[x + i][y] == d + 1:
                continue
            else:
                c[x + i][y] = d + 1
                q.put((x + i, y))
        # 北へ移動
        for i in range(1, k + 1):
            if x - i < 0:
                break
            if c[x - i][y] == '@':
                break
            elif isinstance(c[x - i][y], int) and c[x - i][y] < d + 1:
                break
            elif isinstance(c[x - i][y], int) and c[x - i][y] == d + 1:
                continue
            else:
                c[x - i][y] = d + 1
                q.put((x - i, y))
        # 東へ移動
        for i in range(1, k + 1):
            if y + i >= w:
                break
            if c[x][y + i] == '@':
                break
            elif isinstance(c[x][y + i], int) and c[x][y + i] < d + 1:
                break
            elif isinstance(c[x][y + i], int) and c[x][y + i] == d + 1:
                continue
            else:
                c[x][y + i] = d + 1
                q.put((x, y + i))
        # 西へ移動
        for i in range(1, k + 1):
            if y - i < 0:
                break
            if c[x][y - i] == '@':
                break
            elif isinstance(c[x][y - i], int) and c[x][y - i] < d + 1:
                break
            elif isinstance(c[x][y - i], int) and c[x][y - i] < d + 1:
                continue
            else:
                c[x][y - i] = d + 1
                q.put((x, y - i))
    print(-1 if c[gx][gy] == '.' else c[gx][gy])


if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
from queue import Queue


def main():
    h, w, k = map(int, input().split())
    sx, sy, gx, gy = map(lambda z: int(z) - 1, input().split())
    c = [list(input().replace('\n', '')) for _ in range(h)]
    c[sx][sy] = 0
    q = Queue()
    q.put((sx, sy))
    while not q.empty():
        x, y = q.get()
        d = c[x][y]
        # 南へ移動
        for i in range(1, k + 1):
            if x + i >= h:
                break
            if c[x + i][y] == '@':
                break
            elif isinstance(c[x + i][y], int) and c[x + i][y] < d + 1:
                continue
            else:
                c[x + i][y] = d + 1
                q.put((x + i, y))
        # 北へ移動
        for i in range(1, k + 1):
            if x - i < 0:
                break
            if c[x - i][y] == '@':
                break
            elif isinstance(c[x - i][y], int) and c[x - i][y] < d + 1:
                continue
            else:
                c[x - i][y] = d + 1
                q.put((x - i, y))
        # 東へ移動
        for i in range(1, k + 1):
            if y + i >= w:
                break
            if c[x][y + i] == '@':
                break
            elif isinstance(c[x][y + i], int) and c[x][y + i] < d + 1:
                continue
            else:
                c[x][y + i] = d + 1
                q.put((x, y + i))
        # 西へ移動
        for i in range(1, k + 1):
            if y - i < 0:
                break
            if c[x][y - i] == '@':
                break
            elif isinstance(c[x][y - i], int) and c[x][y - i] < d + 1:
                continue
            else:
                c[x][y - i] = d + 1
                q.put((x, y - i))
    print(-1 if c[gx][gy] == '.' else c[gx][gy])


if __name__ == '__main__':
    main()


Fixed submission:
from queue import Queue


def main():
    h, w, k = map(int, input().split())
    sx, sy, gx, gy = map(lambda z: int(z) - 1, input().split())
    c = [list(input().replace('\n', '')) for _ in range(h)]
    c[sx][sy] = 0
    q = Queue()
    q.put((sx, sy))
    while not q.empty():
        x, y = q.get()
        d = c[x][y]
        # 南へ移動
        for i in range(1, k + 1):
            if x + i >= h:
                break
            if c[x + i][y] == '@':
                break
            elif isinstance(c[x + i][y], int) and c[x + i][y] < d + 1:
                break
            elif isinstance(c[x + i][y], int) and c[x + i][y] == d + 1:
                continue
            else:
                c[x + i][y] = d + 1
                q.put((x + i, y))
        # 北へ移動
        for i in range(1, k + 1):
            if x - i < 0:
                break
            if c[x - i][y] == '@':
                break
            elif isinstance(c[x - i][y], int) and c[x - i][y] < d + 1:
                break
            elif isinstance(c[x - i][y], int) and c[x - i][y] == d + 1:
                continue
            else:
                c[x - i][y] = d + 1
                q.put((x - i, y))
        # 東へ移動
        for i in range(1, k + 1):
            if y + i >= w:
                break
            if c[x][y + i] == '@':
                break
            elif isinstance(c[x][y + i], int) and c[x][y + i] < d + 1:
                break
            elif isinstance(c[x][y + i], int) and c[x][y + i] == d + 1:
                continue
            else:
                c[x][y + i] = d + 1
                q.put((x, y + i))
        # 西へ移動
        for i in range(1, k + 1):
            if y - i < 0:
                break
            if c[x][y - i] == '@':
                break
            elif isinstance(c[x][y - i], int) and c[x][y - i] < d + 1:
                break
            elif isinstance(c[x][y - i], int) and c[x][y - i] < d + 1:
                continue
            else:
                c[x][y - i] = d + 1
                q.put((x, y - i))
    print(-1 if c[gx][gy] == '.' else c[gx][gy])


if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
from queue import Queue


def main():
    h, w, k = map(int, input().split())
    sx, sy, gx, gy = map(lambda z: int(z) - 1, input().split())
    c = [list(input().replace('\n', '')) for _ in range(h)]
    c[sx][sy] = 0
    q = Queue()
    q.put((sx, sy))
    while not q.empty():
        x, y = q.get()
        d = c[x][y]
        # 南へ移動
        for i in range(1, k + 1):
            if x + i >= h:
                break
            if c[x + i][y] == '@':
                break
            elif isinstance(c[x + i][y], int) and c[x + i][y] < d + 1:
                continue
            else:
                c[x + i][y] = d + 1
                q.put((x + i, y))
        # 北へ移動
        for i in range(1, k + 1):
            if x - i < 0:
                break
            if c[x - i][y] == '@':
                break
            elif isinstance(c[x - i][y], int) and c[x - i][y] < d + 1:
                continue
            else:
                c[x - i][y] = d + 1
                q.put((x - i, y))
        # 東へ移動
        for i in range(1, k + 1):
            if y + i >= w:
                break
            if c[x][y + i] == '@':
                break
            elif isinstance(c[x][y + i], int) and c[x][y + i] < d + 1:
                continue
            else:
                c[x][y + i] = d + 1
                q.put((x, y + i))
        # 西へ移動
        for i in range(1, k + 1):
            if y - i < 0:
                break
            if c[x][y - i] == '@':
                break
            elif isinstance(c[x][y - i], int) and c[x][y - i] < d + 1:
                continue
            else:
                c[x][y - i] = d + 1
                q.put((x, y - i))
    print(-1 if c[gx][gy] == '.' else c[gx][gy])


if __name__ == '__main__':
    main()


Fixed submission:
from queue import Queue


def main():
    h, w, k = map(int, input().split())
    sx, sy, gx, gy = map(lambda z: int(z) - 1, input().split())
    c = [list(input().replace('\n', '')) for _ in range(h)]
    c[sx][sy] = 0
    q = Queue()
    q.put((sx, sy))
    while not q.empty():
        x, y = q.get()
        d = c[x][y]
        # 南へ移動
        for i in range(1, k + 1):
            if x + i >= h:
                break
            if c[x + i][y] == '@':
                break
            elif isinstance(c[x + i][y], int) and c[x + i][y] < d + 1:
                break
            elif isinstance(c[x + i][y], int) and c[x + i][y] == d + 1:
                continue
            else:
                c[x + i][y] = d + 1
                q.put((x + i, y))
        # 北へ移動
        for i in range(1, k + 1):
            if x - i < 0:
                break
            if c[x - i][y] == '@':
                break
            elif isinstance(c[x - i][y], int) and c[x - i][y] < d + 1:
                break
            elif isinstance(c[x - i][y], int) and c[x - i][y] == d + 1:
                continue
            else:
                c[x - i][y] = d + 1
                q.put((x - i, y))
        # 東へ移動
        for i in range(1, k + 1):
            if y + i >= w:
                break
            if c[x][y + i] == '@':
                break
            elif isinstance(c[x][y + i], int) and c[x][y + i] < d + 1:
                break
            elif isinstance(c[x][y + i], int) and c[x][y + i] == d + 1:
                continue
            else:
                c[x][y + i] = d + 1
                q.put((x, y + i))
        # 西へ移動
        for i in range(1, k + 1):
            if y - i < 0:
                break
            if c[x][y - i] == '@':
                break
            elif isinstance(c[x][y - i], int) and c[x][y - i] < d + 1:
                break
            elif isinstance(c[x][y - i], int) and c[x][y - i] < d + 1:
                continue
            else:
                c[x][y - i] = d + 1
                q.put((x, y - i))
    print(-1 if c[gx][gy] == '.' else c[gx][gy])


if __name__ == '__main__':
    main()