Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
import sys
input = sys.stdin.readline


def main():
    H, W, K = map(int, input().split())
    sx, sy, gx, gy = map(int, input().split())
    S = [[False]*W for _ in range(H)]
    for i in range(H):
        s = input()
        for j in range(W):
            if s[j] == ".":
                S[i][j] = True

    cost = [[-1]*W for _ in range(H)]

    # 深さ優先探索
    def bfs(x, y, gx, gy):
        stack = deque([[x, y]])
        cost[x][y] = 0
        while stack:
            x, y = stack[0]
            if x == gx and y == gy:
                return
            point = cost[x][y] + 1
            # up
            for i in range(1, K+1):
                if x-i >= 0:
                    if not S[x-i][y]:
                        break
                    elif S[x-i][y] and cost[x-i][y] == -1:
                        cost[x-i][y] = point
                        stack.append([x-i, y])
                    
                else:
                    break
            # down
            for i in range(1, K+1):
                if x+i < H:
                    if not S[x+i][y]:
                        break
                    elif S[x+i][y] and cost[x+i][y] == -1:
                        cost[x+i][y] = point
                        stack.append([x+i, y])
                    
                else:
                    break
            # left
            for i in range(1, K+1):
                if y-i >= 0:
                    if not S[x][y-i]:
                        break
                    elif cost[x][y-i] == -1:
                        cost[x][y-i] = point
                        stack.append([x, y-i])
                else:
                    break
            # right
            for i in range(1, K+1):
                if y+i < W:
                    if not S[x][y+i]:
                        break
                    elif S[x][y+i] and cost[x][y+i] == -1:
                        cost[x][y+i] = point
                        stack.append([x, y+i])
                    
                else:
                    break

            stack.popleft()        

    bfs(sx-1, sy-1, gx-1, gy-1)
    print(cost[gx-1][gy-1])

if __name__ == "__main__":
    main()

Fixed submission:
from collections import deque
import sys
input = sys.stdin.readline


def main():
    H, W, K = map(int, input().split())
    sx, sy, gx, gy = map(int, input().split())
    S = [[False]*W for _ in range(H)]
    for i in range(H):
        s = input()
        for j in range(W):
            if s[j] == ".":
                S[i][j] = True

    cost = [[-1]*W for _ in range(H)]

    # 深さ優先探索
    def bfs(x, y, gx, gy):
        stack = deque([[x, y]])
        cost[x][y] = 0
        while stack:
            x, y = stack[0]
            if x == gx and y == gy:
                return
            point = cost[x][y] + 1
            # up
            for i in range(1, K+1):
                if x-i >= 0:
                    if not S[x-i][y]:
                        break
                    elif cost[x-i][y] == -1:
                        cost[x-i][y] = point
                        stack.append([x-i, y])
                    elif cost[x-i][y] < point:
                        break
                else:
                    break
            # down
            for i in range(1, K+1):
                if x+i < H:
                    if not S[x+i][y]:
                        break
                    elif cost[x+i][y] == -1:
                        cost[x+i][y] = point
                        stack.append([x+i, y])
                    elif cost[x+i][y] < point:
                        break
                else:
                    break
            # left
            for i in range(1, K+1):
                if y-i >= 0:
                    if not S[x][y-i]:
                        break
                    elif cost[x][y-i] == -1:
                        cost[x][y-i] = point
                        stack.append([x, y-i])
                    elif cost[x][y-i] < point:
                        break
                else:
                    break
            # right
            for i in range(1, K+1):
                if y+i < W:
                    if not S[x][y+i]:
                        break
                    elif cost[x][y+i] == -1:
                        cost[x][y+i] = point
                        stack.append([x, y+i])
                    elif cost[x][y+i] < point:
                        break
                else:
                    break

            stack.popleft()        

    bfs(sx-1, sy-1, gx-1, gy-1)
    print(cost[gx-1][gy-1])

if __name__ == "__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
import sys
input = sys.stdin.readline


def main():
    H, W, K = map(int, input().split())
    sx, sy, gx, gy = map(int, input().split())
    S = [[False]*W for _ in range(H)]
    for i in range(H):
        s = input()
        for j in range(W):
            if s[j] == ".":
                S[i][j] = True

    cost = [[-1]*W for _ in range(H)]

    # 深さ優先探索
    def bfs(x, y, gx, gy):
        stack = deque([[x, y]])
        cost[x][y] = 0
        while stack:
            x, y = stack[0]
            if x == gx and y == gy:
                return
            point = cost[x][y] + 1
            # up
            for i in range(1, K+1):
                if x-i >= 0:
                    if S[x-i][y] and cost[x-i][y] == -1:
                        cost[x-i][y] = point
                        stack.append([x-i, y])
                    elif not S[x-i][y]:
                        break
                else:
                    break
            # down
            for i in range(1, K+1):
                if x+i < H:
                    if S[x+i][y] and cost[x+i][y] == -1:
                        cost[x+i][y] = point
                        stack.append([x+i, y])
                    elif not S[x+i][y]:
                        break
                else:
                    break
            # left
            for i in range(1, K+1):
                if y-i >= 0:
                    if S[x][y-i] and cost[x][y-i] == -1:
                        cost[x][y-i] = point
                        stack.append([x, y-i])
                    elif not S[x][y-i]:
                        break
                else:
                    break
            # right
            for i in range(1, K+1):
                if y+i < W:
                    if S[x][y+i] and cost[x][y+i] == -1:
                        cost[x][y+i] = point
                        stack.append([x, y+i])
                    elif not S[x][y+i]:
                        break
                else:
                    break

            stack.popleft()        

    bfs(sx-1, sy-1, gx-1, gy-1)
    print(cost[gx-1][gy-1])

if __name__ == "__main__":
    main()

Fixed submission:
from collections import deque
import sys
input = sys.stdin.readline


def main():
    H, W, K = map(int, input().split())
    sx, sy, gx, gy = map(int, input().split())
    S = [[False]*W for _ in range(H)]
    for i in range(H):
        s = input()
        for j in range(W):
            if s[j] == ".":
                S[i][j] = True

    cost = [[-1]*W for _ in range(H)]

    # 深さ優先探索
    def bfs(x, y, gx, gy):
        stack = deque([[x, y]])
        cost[x][y] = 0
        while stack:
            x, y = stack[0]
            if x == gx and y == gy:
                return
            point = cost[x][y] + 1
            # up
            for i in range(1, K+1):
                if x-i >= 0:
                    if not S[x-i][y]:
                        break
                    elif cost[x-i][y] == -1:
                        cost[x-i][y] = point
                        stack.append([x-i, y])
                    elif cost[x-i][y] < point:
                        break
                else:
                    break
            # down
            for i in range(1, K+1):
                if x+i < H:
                    if not S[x+i][y]:
                        break
                    elif cost[x+i][y] == -1:
                        cost[x+i][y] = point
                        stack.append([x+i, y])
                    elif cost[x+i][y] < point:
                        break
                else:
                    break
            # left
            for i in range(1, K+1):
                if y-i >= 0:
                    if not S[x][y-i]:
                        break
                    elif cost[x][y-i] == -1:
                        cost[x][y-i] = point
                        stack.append([x, y-i])
                    elif cost[x][y-i] < point:
                        break
                else:
                    break
            # right
            for i in range(1, K+1):
                if y+i < W:
                    if not S[x][y+i]:
                        break
                    elif cost[x][y+i] == -1:
                        cost[x][y+i] = point
                        stack.append([x, y+i])
                    elif cost[x][y+i] < point:
                        break
                else:
                    break

            stack.popleft()        

    bfs(sx-1, sy-1, gx-1, gy-1)
    print(cost[gx-1][gy-1])

if __name__ == "__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
import sys
input = sys.stdin.readline


def main():
    H, W, K = map(int, input().split())
    sx, sy, gx, gy = map(int, input().split())
    S = [[False]*W for _ in range(H)]
    for i in range(H):
        s = input()
        for j in range(W):
            if s[j] == ".":
                S[i][j] = True

    cost = [[-1]*W for _ in range(H)]

    # 深さ優先探索
    def bfs(x, y):
        stack = deque([[x, y]])
        cost[x][y] = 0
        while stack:
            x, y = stack[0]
            point = cost[x][y] + 1
            # up
            for i in range(1, K+1):
                if x-i >= 0:
                    if S[x-i][y] and cost[x-i][y] == -1:
                        cost[x-i][y] = point
                        stack.append([x-i, y])
                    elif not S[x-i][y]:
                        break
                else:
                    break
            # down
            for i in range(1, K+1):
                if x+i < H:
                    if S[x+i][y] and cost[x+i][y] == -1:
                        cost[x+i][y] = point
                        stack.append([x+i, y])
                    elif not S[x+i][y]:
                        break
                else:
                    break
            # left
            for i in range(1, K+1):
                if y-i >= 0:
                    if S[x][y-i] and cost[x][y-i] == -1:
                        cost[x][y-i] = point
                        stack.append([x, y-i])
                    elif not S[x][y-i]:
                        break
                else:
                    break
            # right
            for i in range(1, K+1):
                if y+i < W:
                    if S[x][y+i] and cost[x][y+i] == -1:
                        cost[x][y+i] = point
                        stack.append([x, y+i])
                    elif not S[x][y+i]:
                        break
                else:
                    break

            stack.popleft()        

    bfs(sx-1, sy-1)
    print(cost[gx-1][gy-1])

if __name__ == "__main__":
    main()

Fixed submission:
from collections import deque
import sys
input = sys.stdin.readline


def main():
    H, W, K = map(int, input().split())
    sx, sy, gx, gy = map(int, input().split())
    S = [[False]*W for _ in range(H)]
    for i in range(H):
        s = input()
        for j in range(W):
            if s[j] == ".":
                S[i][j] = True

    cost = [[-1]*W for _ in range(H)]

    # 深さ優先探索
    def bfs(x, y, gx, gy):
        stack = deque([[x, y]])
        cost[x][y] = 0
        while stack:
            x, y = stack[0]
            if x == gx and y == gy:
                return
            point = cost[x][y] + 1
            # up
            for i in range(1, K+1):
                if x-i >= 0:
                    if not S[x-i][y]:
                        break
                    elif cost[x-i][y] == -1:
                        cost[x-i][y] = point
                        stack.append([x-i, y])
                    elif cost[x-i][y] < point:
                        break
                else:
                    break
            # down
            for i in range(1, K+1):
                if x+i < H:
                    if not S[x+i][y]:
                        break
                    elif cost[x+i][y] == -1:
                        cost[x+i][y] = point
                        stack.append([x+i, y])
                    elif cost[x+i][y] < point:
                        break
                else:
                    break
            # left
            for i in range(1, K+1):
                if y-i >= 0:
                    if not S[x][y-i]:
                        break
                    elif cost[x][y-i] == -1:
                        cost[x][y-i] = point
                        stack.append([x, y-i])
                    elif cost[x][y-i] < point:
                        break
                else:
                    break
            # right
            for i in range(1, K+1):
                if y+i < W:
                    if not S[x][y+i]:
                        break
                    elif cost[x][y+i] == -1:
                        cost[x][y+i] = point
                        stack.append([x, y+i])
                    elif cost[x][y+i] < point:
                        break
                else:
                    break

            stack.popleft()        

    bfs(sx-1, sy-1, gx-1, gy-1)
    print(cost[gx-1][gy-1])

if __name__ == "__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
import sys
input = sys.stdin.readline


def main():
    H, W, K = map(int, input().split())
    sx, sy, gx, gy = map(int, input().split())
    S = [""]*H
    for i in range(H):
        S[i] = input()

    cost = [[-1]*W for _ in range(H)]

    # 深さ優先探索
    def bfs(x, y):
        stack = deque([[x, y]])
        cost[x][y] = 0
        while stack:
            x, y = stack[0]
            point = cost[x][y] + 1
            # up
            for i in range(1, K+1):
                if x-i >= 0:
                    if S[x-i][y] == "." and cost[x-i][y] == -1:
                        cost[x-i][y] = point
                        stack.append([x-i, y])
                    elif S[x-i][y] == "@":
                        break
                else:
                    break
            # down
            for i in range(1, K+1):
                if x+i < H:
                    if S[x+i][y] == "." and cost[x+i][y] == -1:
                        cost[x+i][y] = point
                        stack.append([x+i, y])
                    elif S[x+i][y] == "@":
                        break
                else:
                    break
            # left
            for i in range(1, K+1):
                if y-i >= 0:
                    if S[x][y-i] == "." and cost[x][y-i] == -1:
                        cost[x][y-i] = point
                        stack.append([x, y-i])
                    elif S[x][y-i] == "@":
                        break
                else:
                    break
            # right
            for i in range(1, K+1):
                if y+i < W:
                    if S[x][y+i] == "." and cost[x][y+i] == -1:
                        cost[x][y+i] = point
                        stack.append([x, y+i])
                    elif S[x][y+i] == "@":
                        break
                else:
                    break

            stack.popleft()        

    bfs(sx-1, sy-1)
    print(cost[gx-1][gy-1])

if __name__ == "__main__":
    main()

Fixed submission:
from collections import deque
import sys
input = sys.stdin.readline


def main():
    H, W, K = map(int, input().split())
    sx, sy, gx, gy = map(int, input().split())
    S = [[False]*W for _ in range(H)]
    for i in range(H):
        s = input()
        for j in range(W):
            if s[j] == ".":
                S[i][j] = True

    cost = [[-1]*W for _ in range(H)]

    # 深さ優先探索
    def bfs(x, y, gx, gy):
        stack = deque([[x, y]])
        cost[x][y] = 0
        while stack:
            x, y = stack[0]
            if x == gx and y == gy:
                return
            point = cost[x][y] + 1
            # up
            for i in range(1, K+1):
                if x-i >= 0:
                    if not S[x-i][y]:
                        break
                    elif cost[x-i][y] == -1:
                        cost[x-i][y] = point
                        stack.append([x-i, y])
                    elif cost[x-i][y] < point:
                        break
                else:
                    break
            # down
            for i in range(1, K+1):
                if x+i < H:
                    if not S[x+i][y]:
                        break
                    elif cost[x+i][y] == -1:
                        cost[x+i][y] = point
                        stack.append([x+i, y])
                    elif cost[x+i][y] < point:
                        break
                else:
                    break
            # left
            for i in range(1, K+1):
                if y-i >= 0:
                    if not S[x][y-i]:
                        break
                    elif cost[x][y-i] == -1:
                        cost[x][y-i] = point
                        stack.append([x, y-i])
                    elif cost[x][y-i] < point:
                        break
                else:
                    break
            # right
            for i in range(1, K+1):
                if y+i < W:
                    if not S[x][y+i]:
                        break
                    elif cost[x][y+i] == -1:
                        cost[x][y+i] = point
                        stack.append([x, y+i])
                    elif cost[x][y+i] < point:
                        break
                else:
                    break

            stack.popleft()        

    bfs(sx-1, sy-1, gx-1, gy-1)
    print(cost[gx-1][gy-1])

if __name__ == "__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline

H, W, K = map(int, input().split())
sx, sy, gx, gy = map(int, input().split())
S = [input() for _ in range(H)]
from collections import deque

cost = [[-1]*W for _ in range(H)]

# 深さ優先探索
def bfs(x, y):
    stack = deque([[x, y]])
    cost[x][y] = 0
    while stack:
        x, y = stack[0]
        point = cost[x][y]
        # up
        for i in range(1, K+1):
            if x-i >= 0:
                if S[x-i][y] == "." and cost[x-i][y] == -1:
                    cost[x-i][y] = point+1
                    stack.append([x-i, y])
                elif S[x-i][y] == "@":
                    break
            else:
                break
        # down
        for i in range(1, K+1):
            if x+i < H:
                if S[x+i][y] == "." and cost[x+i][y] == -1:
                    cost[x+i][y] = point+1
                    stack.append([x+i, y])
                elif S[x+i][y] == "@":
                    break
            else:
                break
        # left
        for i in range(1, K+1):
            if y-i >= 0:
                if S[x][y-i] == "." and cost[x][y-i] == -1:
                    cost[x][y-i] = point+1
                    stack.append([x, y-i])
                elif S[x][y-i] == "@":
                    break
            else:
                break
        # right
        for i in range(1, K+1):
            if y+i < W:
                if S[x][y+i] == "." and cost[x][y+i] == -1:
                    cost[x][y+i] = point+1
                    stack.append([x, y+i])
                elif S[x][y+i] == "@":
                    break
            else:
                break

        stack.popleft()        

bfs(sx-1, sy-1)
print(cost[gx-1][gy-1])

Fixed submission:
from collections import deque
import sys
input = sys.stdin.readline


def main():
    H, W, K = map(int, input().split())
    sx, sy, gx, gy = map(int, input().split())
    S = [[False]*W for _ in range(H)]
    for i in range(H):
        s = input()
        for j in range(W):
            if s[j] == ".":
                S[i][j] = True

    cost = [[-1]*W for _ in range(H)]

    # 深さ優先探索
    def bfs(x, y, gx, gy):
        stack = deque([[x, y]])
        cost[x][y] = 0
        while stack:
            x, y = stack[0]
            if x == gx and y == gy:
                return
            point = cost[x][y] + 1
            # up
            for i in range(1, K+1):
                if x-i >= 0:
                    if not S[x-i][y]:
                        break
                    elif cost[x-i][y] == -1:
                        cost[x-i][y] = point
                        stack.append([x-i, y])
                    elif cost[x-i][y] < point:
                        break
                else:
                    break
            # down
            for i in range(1, K+1):
                if x+i < H:
                    if not S[x+i][y]:
                        break
                    elif cost[x+i][y] == -1:
                        cost[x+i][y] = point
                        stack.append([x+i, y])
                    elif cost[x+i][y] < point:
                        break
                else:
                    break
            # left
            for i in range(1, K+1):
                if y-i >= 0:
                    if not S[x][y-i]:
                        break
                    elif cost[x][y-i] == -1:
                        cost[x][y-i] = point
                        stack.append([x, y-i])
                    elif cost[x][y-i] < point:
                        break
                else:
                    break
            # right
            for i in range(1, K+1):
                if y+i < W:
                    if not S[x][y+i]:
                        break
                    elif cost[x][y+i] == -1:
                        cost[x][y+i] = point
                        stack.append([x, y+i])
                    elif cost[x][y+i] < point:
                        break
                else:
                    break

            stack.popleft()        

    bfs(sx-1, sy-1, gx-1, gy-1)
    print(cost[gx-1][gy-1])

if __name__ == "__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):
H, W, K = map(int, input().split())
sx, sy, gx, gy = map(int, input().split())
S = [input() for _ in range(H)]
from collections import deque

cost = [[-1]*W for _ in range(H)]

# 深さ優先探索
def bfs(x, y):
    stack = deque([[x, y]])
    cost[x][y] = 0
    while stack:
        x, y = stack[0]
        point = cost[x][y]
        # up
        for i in range(1, K+1):
            if x-i >= 0:
                if S[x-i][y] == "." and cost[x-i][y] == -1:
                    cost[x-i][y] = point+1
                    stack.append([x-i, y])
                elif S[x-i][y] == "@":
                    break
            else:
                break
        # down
        for i in range(1, K+1):
            if x+i < H:
                if S[x+i][y] == "." and cost[x+i][y] == -1:
                    cost[x+i][y] = point+1
                    stack.append([x+i, y])
                elif S[x+i][y] == "@":
                    break
            else:
                break
        # left
        for i in range(1, K+1):
            if y-i >= 0:
                if S[x][y-i] == "." and cost[x][y-i] == -1:
                    cost[x][y-i] = point+1
                    stack.append([x, y-i])
                elif S[x][y-i] == "@":
                    break
            else:
                break
        # right
        for i in range(1, K+1):
            if y+i < W:
                if S[x][y+i] == "." and cost[x][y+i] == -1:
                    cost[x][y+i] = point+1
                    stack.append([x, y+i])
                elif S[x][y+i] == "@":
                    break
            else:
                break

        stack.popleft()        

bfs(sx-1, sy-1)
print(cost[gx-1][gy-1])

Fixed submission:
from collections import deque
import sys
input = sys.stdin.readline


def main():
    H, W, K = map(int, input().split())
    sx, sy, gx, gy = map(int, input().split())
    S = [[False]*W for _ in range(H)]
    for i in range(H):
        s = input()
        for j in range(W):
            if s[j] == ".":
                S[i][j] = True

    cost = [[-1]*W for _ in range(H)]

    # 深さ優先探索
    def bfs(x, y, gx, gy):
        stack = deque([[x, y]])
        cost[x][y] = 0
        while stack:
            x, y = stack[0]
            if x == gx and y == gy:
                return
            point = cost[x][y] + 1
            # up
            for i in range(1, K+1):
                if x-i >= 0:
                    if not S[x-i][y]:
                        break
                    elif cost[x-i][y] == -1:
                        cost[x-i][y] = point
                        stack.append([x-i, y])
                    elif cost[x-i][y] < point:
                        break
                else:
                    break
            # down
            for i in range(1, K+1):
                if x+i < H:
                    if not S[x+i][y]:
                        break
                    elif cost[x+i][y] == -1:
                        cost[x+i][y] = point
                        stack.append([x+i, y])
                    elif cost[x+i][y] < point:
                        break
                else:
                    break
            # left
            for i in range(1, K+1):
                if y-i >= 0:
                    if not S[x][y-i]:
                        break
                    elif cost[x][y-i] == -1:
                        cost[x][y-i] = point
                        stack.append([x, y-i])
                    elif cost[x][y-i] < point:
                        break
                else:
                    break
            # right
            for i in range(1, K+1):
                if y+i < W:
                    if not S[x][y+i]:
                        break
                    elif cost[x][y+i] == -1:
                        cost[x][y+i] = point
                        stack.append([x, y+i])
                    elif cost[x][y+i] < point:
                        break
                else:
                    break

            stack.popleft()        

    bfs(sx-1, sy-1, gx-1, gy-1)
    print(cost[gx-1][gy-1])

if __name__ == "__main__":
    main()