Buggy python submission (verdict: Time Limit Exceeded):
#!/usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys, random, itertools, math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt
def LI(): return map(int, input().split())
def LF(): return list(map(float, input().split()))
def LI_(): return list(map(lambda x: int(x)-1, input().split()))
def II(): return int(input())
def IF(): return float(input())
def S(): return input().rstrip()
def LS(): return S().split()
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = float("INF")

#solve
def solve():
    mxy = [(0, 1), (1, 0), (-1, 0), (0, -1)]
    h, w, K = LI()
    y1, x1, y2, x2 = LI()
    q = deque()
    field = [[False] * (w + 2)] + [None] * h + [[False] * (w + 2)]
    for i in range(1, h + 1):
        field[i] = [False] + [True] * w + [False]
    for i in range(1, h + 1):
        s = input().rstrip()
        for j in range(1, w + 1):
            if s[j-1] == "@":
                field[i][j] = False
    q.append((x1, y1))
    ans = 0
    c = defaultdict(int)
    while q:
        next_q = deque()
        while q:
            x, y = q.pop()
            if (x, y) == (x2, y2):
                print(ans)
                return
            if field[y][x]:
                for i in range(1, K + 1):
                    if field[y + i][x]:
                        if not c[(y + i, x)]:
                            next_q.append((x, y + i))
                            c[(y + i, x)] = 1
                    else:
                        break
                for i in range(1, K + 1):
                    if field[y][x + i]:
                        if not c[(y, x + i)]:
                            next_q.append((x + i, y))
                            c[(y, x + i)] = 1
                    else:
                        break
                for i in range(1, K + 1):
                    if field[y - i][x]:
                        if not c[(y - i, x)]:
                            next_q.append((x, y - i))
                            c[(y - i, x)] = 1
                    else:
                        break
                for i in range(1, K + 1):
                    if field[y][x - i]:
                        if not c[(y, x - i)]:
                            next_q.append((x - i, y))
                            c[(y, x - i)] = 1
                    else:
                        break
                field[y][x] = False
        ans += 1
        q = next_q
    print(-1)
    return


#main
if __name__ == '__main__':
    solve()


Fixed submission:
#!/usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys, random, itertools, math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt
def LI(): return map(int, input().split())
def LF(): return list(map(float, input().split()))
def LI_(): return list(map(lambda x: int(x)-1, input().split()))
def II(): return int(input())
def IF(): return float(input())
def S(): return input().rstrip()
def LS(): return S().split()
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = float("INF")

#solve
def solve():
    mxy = [(0, 1), (1, 0), (-1, 0), (0, -1)]
    h, w, K = LI()
    y1, x1, y2, x2 = LI()
    q = deque()
    field = [[False] * (w + 2)] + [None] * h + [[False] * (w + 2)]
    for i in range(1, h + 1):
        field[i] = [False] + [True] * w + [False]
    for i in range(1, h + 1):
        s = input().rstrip()
        for j in range(1, w + 1):
            if s[j-1] == "@":
                field[i][j] = False
    q.append((x1, y1))
    ans = 0
    c = [[0] * (w + 2) for i in range(h + 2)]
    while q:
        next_q = deque()
        while q:
            x, y = q.pop()
            if (x, y) == (x2, y2):
                print(ans)
                return
            if field[y][x]:
                for i in range(1, K + 1):
                    if field[y + i][x]:
                        if not c[y + i][x]:
                            next_q.append((x, y + i))
                            c[y + i][x] = 1
                    else:
                        break
                for i in range(1, K + 1):
                    if field[y][x + i]:
                        if not c[y][x + i]:
                            next_q.append((x + i, y))
                            c[y][x + i] = 1
                    else:
                        break
                for i in range(1, K + 1):
                    if field[y - i][x]:
                        if not c[y - i][x]:
                            next_q.append((x, y - i))
                            c[y - i][x] = 1
                    else:
                        break
                for i in range(1, K + 1):
                    if field[y][x - i]:
                        if not c[y][x - i]:
                            next_q.append((x - i, y))
                            c[y][x - i] = 1
                    else:
                        break
                field[y][x] = False
        ans += 1
        q = next_q
    print(-1)
    return


#main
if __name__ == '__main__':
    solve()

Buggy python submission (verdict: Time Limit Exceeded):
#!/usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys, random, itertools, math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt
def LI(): return map(int, input().split())
def LF(): return list(map(float, input().split()))
def LI_(): return list(map(lambda x: int(x)-1, input().split()))
def II(): return int(input())
def IF(): return float(input())
def S(): return input().rstrip()
def LS(): return S().split()
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = float("INF")

#solve
def solve():
    mxy = [(0, 1), (1, 0), (-1, 0), (0, -1)]
    h, w, K = LI()
    y1, x1, y2, x2 = LI()
    q = deque()
    field = [[False] * (w + 2)] + [None] * h + [[False] * (w + 2)]
    for i in range(1, h + 1):
        field[i] = [False] + [True] * w + [False]
    for i in range(1, h + 1):
        s = input().rstrip()
        for j in range(1, w + 1):
            if s[j-1] == "@":
                field[i][j] = False
    q.append((x1, y1))
    ans = 0
    c = defaultdict(int)
    while q:
        next_q = deque()
        while q:
            x, y = q.popleft()
            if (x, y) == (x2, y2):
                print(ans)
                return
            if field[y][x]:
                for i in range(1, K + 1):
                    if field[y + i][x]:
                        if not c[(y + i, x)]:
                            next_q.append((x, y + i))
                            c[(y + i, x)] = 1
                    else:
                        break
                for i in range(1, K + 1):
                    if field[y][x + i]:
                        if not c[(y, x + i)]:
                            next_q.append((x + i, y))
                            c[(y, x + i)] = 1
                    else:
                        break
                for i in range(1, K + 1):
                    if field[y - i][x]:
                        if not c[(y - i, x)]:
                            next_q.append((x, y - i))
                            c[(y - i, x)] = 1
                    else:
                        break
                for i in range(1, K + 1):
                    if field[y][x - i]:
                        if not c[(y, x - i)]:
                            next_q.append((x - i, y))
                            c[(y, x - i)] = 1
                    else:
                        break
                field[y][x] = False
        ans += 1
        q = next_q
    print(-1)
    return


#main
if __name__ == '__main__':
    solve()


Fixed submission:
#!/usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys, random, itertools, math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt
def LI(): return map(int, input().split())
def LF(): return list(map(float, input().split()))
def LI_(): return list(map(lambda x: int(x)-1, input().split()))
def II(): return int(input())
def IF(): return float(input())
def S(): return input().rstrip()
def LS(): return S().split()
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = float("INF")

#solve
def solve():
    mxy = [(0, 1), (1, 0), (-1, 0), (0, -1)]
    h, w, K = LI()
    y1, x1, y2, x2 = LI()
    q = deque()
    field = [[False] * (w + 2)] + [None] * h + [[False] * (w + 2)]
    for i in range(1, h + 1):
        field[i] = [False] + [True] * w + [False]
    for i in range(1, h + 1):
        s = input().rstrip()
        for j in range(1, w + 1):
            if s[j-1] == "@":
                field[i][j] = False
    q.append((x1, y1))
    ans = 0
    c = [[0] * (w + 2) for i in range(h + 2)]
    while q:
        next_q = deque()
        while q:
            x, y = q.pop()
            if (x, y) == (x2, y2):
                print(ans)
                return
            if field[y][x]:
                for i in range(1, K + 1):
                    if field[y + i][x]:
                        if not c[y + i][x]:
                            next_q.append((x, y + i))
                            c[y + i][x] = 1
                    else:
                        break
                for i in range(1, K + 1):
                    if field[y][x + i]:
                        if not c[y][x + i]:
                            next_q.append((x + i, y))
                            c[y][x + i] = 1
                    else:
                        break
                for i in range(1, K + 1):
                    if field[y - i][x]:
                        if not c[y - i][x]:
                            next_q.append((x, y - i))
                            c[y - i][x] = 1
                    else:
                        break
                for i in range(1, K + 1):
                    if field[y][x - i]:
                        if not c[y][x - i]:
                            next_q.append((x - i, y))
                            c[y][x - i] = 1
                    else:
                        break
                field[y][x] = False
        ans += 1
        q = next_q
    print(-1)
    return


#main
if __name__ == '__main__':
    solve()

Buggy python submission (verdict: Runtime Error):
#!/usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys, random, itertools, math
sys.setrecursionlimit(10**5)
input = sys.stdin.buffer.readlin
sqrt = math.sqrt
def LI(): return map(int, input().split())
def LF(): return list(map(float, input().split()))
def LI_(): return list(map(lambda x: int(x)-1, input().split()))
def II(): return int(input())
def IF(): return float(input())
def S(): return input().rstrip()
def LS(): return S().split()
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = float("INF")

#solve
def solve():
    mxy = [(0, 1), (1, 0), (-1, 0), (0, -1)]
    h, w, K = LI()
    y1, x1, y2, x2 = LI()
    q = deque()
    field = [[False] * (w + 2)] + [None] * h + [[False] * (w + 2)]
    for i in range(1, h + 1):
        field[i] = [False] + [True] * w + [False]
    for i in range(1, h + 1):
        s = input()
        for j in range(1, w + 1):
            if s[j-1] == "@":
                field[i][j] = False
    q.append((x1, y1))
    ans = 0
    c = defaultdict(int)
    while q:
        next_q = deque()

        while q:
            x, y = q.popleft()
            if (x, y) == (x2, y2):
                print(ans)
                return
            if field[y][x]:
                for i in range(1, K + 1):
                    if field[y + i][x]:
                        if not c[(y + i, x)]:
                            next_q.append((x, y + i))
                            c[(y + i, x)] = 1
                    else:
                        break
                for i in range(1, K + 1):
                    if field[y][x + i]:
                        if not c[(y, x + i)]:
                            next_q.append((x + i, y))
                            c[(y, x + i)] = 1
                    else:
                        break
                for i in range(1, K + 1):
                    if field[y - i][x]:
                        if not c[(y - i, x)]:
                            next_q.append((x, y - i))
                            c[(y - i, x)] = 1
                    else:
                        break
                for i in range(1, K + 1):
                    if field[y][x - i]:
                        if not c[(y, x - i)]:
                            next_q.append((x - i, y))
                            c[(y, x - i)] = 1
                    else:
                        break
                field[y][x] = False
        ans += 1
        q = next_q
    print(-1)
    return


#main
if __name__ == '__main__':
    solve()


Fixed submission:
#!/usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys, random, itertools, math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt
def LI(): return map(int, input().split())
def LF(): return list(map(float, input().split()))
def LI_(): return list(map(lambda x: int(x)-1, input().split()))
def II(): return int(input())
def IF(): return float(input())
def S(): return input().rstrip()
def LS(): return S().split()
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = float("INF")

#solve
def solve():
    mxy = [(0, 1), (1, 0), (-1, 0), (0, -1)]
    h, w, K = LI()
    y1, x1, y2, x2 = LI()
    q = deque()
    field = [[False] * (w + 2)] + [None] * h + [[False] * (w + 2)]
    for i in range(1, h + 1):
        field[i] = [False] + [True] * w + [False]
    for i in range(1, h + 1):
        s = input().rstrip()
        for j in range(1, w + 1):
            if s[j-1] == "@":
                field[i][j] = False
    q.append((x1, y1))
    ans = 0
    c = [[0] * (w + 2) for i in range(h + 2)]
    while q:
        next_q = deque()
        while q:
            x, y = q.pop()
            if (x, y) == (x2, y2):
                print(ans)
                return
            if field[y][x]:
                for i in range(1, K + 1):
                    if field[y + i][x]:
                        if not c[y + i][x]:
                            next_q.append((x, y + i))
                            c[y + i][x] = 1
                    else:
                        break
                for i in range(1, K + 1):
                    if field[y][x + i]:
                        if not c[y][x + i]:
                            next_q.append((x + i, y))
                            c[y][x + i] = 1
                    else:
                        break
                for i in range(1, K + 1):
                    if field[y - i][x]:
                        if not c[y - i][x]:
                            next_q.append((x, y - i))
                            c[y - i][x] = 1
                    else:
                        break
                for i in range(1, K + 1):
                    if field[y][x - i]:
                        if not c[y][x - i]:
                            next_q.append((x - i, y))
                            c[y][x - i] = 1
                    else:
                        break
                field[y][x] = False
        ans += 1
        q = next_q
    print(-1)
    return


#main
if __name__ == '__main__':
    solve()

Buggy python submission (verdict: Time Limit Exceeded):
#!/usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys, random, itertools, math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt
def LI(): return map(int, input().split())
def LF(): return list(map(float, input().split()))
def LI_(): return list(map(lambda x: int(x)-1, input().split()))
def II(): return int(input())
def IF(): return float(input())
def S(): return input().rstrip()
def LS(): return S().split()
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = float("INF")

#solve
def solve():
    mxy = [(0, 1), (1, 0), (-1, 0), (0, -1)]
    h, w, K = LI()
    y1, x1, y2, x2 = LI()
    c = SR(h)
    q = deque()
    field = [[False] * (w + 2)] + [None] * h + [[False] * (w + 2)]
    for i in range(1, h + 1):
        field[i] = [False] + [True] * w + [False]
    for i in range(1, h + 1):
        for j in range(1, w + 1):
            if c[i-1][j-1] == "@":
                field[i][j] = False
    q = set()
    q.add((x1, y1))
    ans = 0
    c = defaultdict(int)
    while q:
        next_q = set()

        while q:
            x, y = q.pop()
            if (x, y) == (x2, y2):
                print(ans)
                return
            if field[y][x]:
                for i in range(1, K + 1):
                    if field[y + i][x]:
                        if not c[(y + i, x)]:
                            next_q.add((x, y + i))
                            c[(y + i, x)] = 1
                    else:
                        break
                for i in range(1, K + 1):
                    if field[y][x + i]:
                        if not c[(y, x + i)]:
                            next_q.add((x + i, y))
                            c[(y, x + i)] = 1
                    else:
                        break
                for i in range(1, K + 1):
                    if field[y - i][x]:
                        if not c[(y - i, x)]:
                            next_q.add((x, y - i))
                            c[(y - i, x)] = 1
                    else:
                        break
                for i in range(1, K + 1):
                    if field[y][x - i]:
                        if not c[(y, x - i)]:
                            next_q.add((x - i, y))
                            c[(y, x - i)] = 1
                    else:
                        break
                field[y][x] = False
        ans += 1
        q = next_q
    print(-1)
    return


#main
if __name__ == '__main__':
    solve()


Fixed submission:
#!/usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys, random, itertools, math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt
def LI(): return map(int, input().split())
def LF(): return list(map(float, input().split()))
def LI_(): return list(map(lambda x: int(x)-1, input().split()))
def II(): return int(input())
def IF(): return float(input())
def S(): return input().rstrip()
def LS(): return S().split()
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = float("INF")

#solve
def solve():
    mxy = [(0, 1), (1, 0), (-1, 0), (0, -1)]
    h, w, K = LI()
    y1, x1, y2, x2 = LI()
    q = deque()
    field = [[False] * (w + 2)] + [None] * h + [[False] * (w + 2)]
    for i in range(1, h + 1):
        field[i] = [False] + [True] * w + [False]
    for i in range(1, h + 1):
        s = input().rstrip()
        for j in range(1, w + 1):
            if s[j-1] == "@":
                field[i][j] = False
    q.append((x1, y1))
    ans = 0
    c = [[0] * (w + 2) for i in range(h + 2)]
    while q:
        next_q = deque()
        while q:
            x, y = q.pop()
            if (x, y) == (x2, y2):
                print(ans)
                return
            if field[y][x]:
                for i in range(1, K + 1):
                    if field[y + i][x]:
                        if not c[y + i][x]:
                            next_q.append((x, y + i))
                            c[y + i][x] = 1
                    else:
                        break
                for i in range(1, K + 1):
                    if field[y][x + i]:
                        if not c[y][x + i]:
                            next_q.append((x + i, y))
                            c[y][x + i] = 1
                    else:
                        break
                for i in range(1, K + 1):
                    if field[y - i][x]:
                        if not c[y - i][x]:
                            next_q.append((x, y - i))
                            c[y - i][x] = 1
                    else:
                        break
                for i in range(1, K + 1):
                    if field[y][x - i]:
                        if not c[y][x - i]:
                            next_q.append((x - i, y))
                            c[y][x - i] = 1
                    else:
                        break
                field[y][x] = False
        ans += 1
        q = next_q
    print(-1)
    return


#main
if __name__ == '__main__':
    solve()

Buggy python submission (verdict: Time Limit Exceeded):
#!/usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys, random, itertools, math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt
def LI(): return map(int, input().split())
def LF(): return list(map(float, input().split()))
def LI_(): return list(map(lambda x: int(x)-1, input().split()))
def II(): return int(input())
def IF(): return float(input())
def S(): return input().rstrip()
def LS(): return S().split()
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = float("INF")

#solve
def solve():
    mxy = [(0, 1), (1, 0), (-1, 0), (0, -1)]
    h, w, K = LI()
    y1, x1, y2, x2 = LI()
    c = SR(h)
    q = deque()
    field = [[False] * (w + 2)] + [None] * h + [[False] * (w + 2)]
    for i in range(1,h+1):
        field[i] = [False] + [True] * w + [False]
    for i in range(1, h + 1):
        for j in range(1, w + 1):
            if c[i-1][j-1] == "@":
                field[i][j] = False
    q.append((x1, y1))
    ans = 0
    c = defaultdict(int)
    while q:
        next_q = deque()

        while q:
            x, y = q.popleft()
            if (x, y) == (x2, y2):
                print(ans)
                return
            if field[y][x]:
                for i in range(1, K + 1):
                    if field[y + i][x]:
                        if not c[(y + i, x)]:
                            next_q.append((x, y + i))
                            c[(y + i, x)] = 1
                    else:
                        break
                for i in range(1, K + 1):
                    if field[y][x + i]:
                        if not c[(y, x + i)]:
                            next_q.append((x + i, y))
                            c[(y, x + i)] = 1
                    else:
                        break
                for i in range(1, K + 1):
                    if field[y - i][x]:
                        if not c[(y - i, x)]:
                            next_q.append((x, y - i))
                            c[(y - i, x)] = 1
                    else:
                        break
                for i in range(1, K + 1):
                    if field[y][x - i]:
                        if not c[(y, x - i)]:
                            next_q.append((x - i, y))
                            c[(y, x - i)] = 1
                    else:
                        break
                field[y][x] = False
        ans += 1
        q = next_q
    print(-1)
    return


#main
if __name__ == '__main__':
    solve()


Fixed submission:
#!/usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys, random, itertools, math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt
def LI(): return map(int, input().split())
def LF(): return list(map(float, input().split()))
def LI_(): return list(map(lambda x: int(x)-1, input().split()))
def II(): return int(input())
def IF(): return float(input())
def S(): return input().rstrip()
def LS(): return S().split()
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = float("INF")

#solve
def solve():
    mxy = [(0, 1), (1, 0), (-1, 0), (0, -1)]
    h, w, K = LI()
    y1, x1, y2, x2 = LI()
    q = deque()
    field = [[False] * (w + 2)] + [None] * h + [[False] * (w + 2)]
    for i in range(1, h + 1):
        field[i] = [False] + [True] * w + [False]
    for i in range(1, h + 1):
        s = input().rstrip()
        for j in range(1, w + 1):
            if s[j-1] == "@":
                field[i][j] = False
    q.append((x1, y1))
    ans = 0
    c = [[0] * (w + 2) for i in range(h + 2)]
    while q:
        next_q = deque()
        while q:
            x, y = q.pop()
            if (x, y) == (x2, y2):
                print(ans)
                return
            if field[y][x]:
                for i in range(1, K + 1):
                    if field[y + i][x]:
                        if not c[y + i][x]:
                            next_q.append((x, y + i))
                            c[y + i][x] = 1
                    else:
                        break
                for i in range(1, K + 1):
                    if field[y][x + i]:
                        if not c[y][x + i]:
                            next_q.append((x + i, y))
                            c[y][x + i] = 1
                    else:
                        break
                for i in range(1, K + 1):
                    if field[y - i][x]:
                        if not c[y - i][x]:
                            next_q.append((x, y - i))
                            c[y - i][x] = 1
                    else:
                        break
                for i in range(1, K + 1):
                    if field[y][x - i]:
                        if not c[y][x - i]:
                            next_q.append((x - i, y))
                            c[y][x - i] = 1
                    else:
                        break
                field[y][x] = False
        ans += 1
        q = next_q
    print(-1)
    return


#main
if __name__ == '__main__':
    solve()

Buggy python submission (verdict: Time Limit Exceeded):
#!/usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys, random, itertools, math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt
def LI(): return map(int, input().split())
def LF(): return list(map(float, input().split()))
def LI_(): return list(map(lambda x: int(x)-1, input().split()))
def II(): return int(input())
def IF(): return float(input())
def S(): return input().rstrip()
def LS(): return S().split()
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = float("INF")

#solve
def solve():
    mxy = [(0, 1), (1, 0), (-1, 0), (0, -1)]
    h, w, K = LI()
    y1, x1, y2, x2 = LI()
    c = SR(h)
    q = deque()
    field = [[False] * (w + 2)] + [None] * h + [[False] * (w + 2)]
    for i in range(1,h+1):
        field[i] = [False] + [True] * w + [False]
    for i in range(1, h + 1):
        for j in range(1, w + 1):
            if c[i-1][j-1] == "@":
                field[i][j] = False
    q.append((x1, y1))
    ans = 0
    while q:
        next_q = deque()

        while q:
            x, y = q.popleft()
            if (x, y) == (x2, y2):
                print(ans)
                return
            if field[y][x]:
                for i in range(1, K + 1):
                    if field[y + i][x]:
                        next_q.append((x, y + i))
                    else:
                        break
                for i in range(1, K + 1):
                    if field[y][x + i]:
                        next_q.append((x + i, y))
                    else:
                        break
                for i in range(1, K + 1):
                    if field[y - i][x]:
                        next_q.append((x, y - i))
                    else:
                        break
                for i in range(1, K + 1):
                    if field[y][x - i]:
                        next_q.append((x - i, y))
                    else:
                        break
                field[y][x] = False
        ans += 1
        q = next_q
    print(-1)
    return


#main
if __name__ == '__main__':
    solve()


Fixed submission:
#!/usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys, random, itertools, math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt
def LI(): return map(int, input().split())
def LF(): return list(map(float, input().split()))
def LI_(): return list(map(lambda x: int(x)-1, input().split()))
def II(): return int(input())
def IF(): return float(input())
def S(): return input().rstrip()
def LS(): return S().split()
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = float("INF")

#solve
def solve():
    mxy = [(0, 1), (1, 0), (-1, 0), (0, -1)]
    h, w, K = LI()
    y1, x1, y2, x2 = LI()
    q = deque()
    field = [[False] * (w + 2)] + [None] * h + [[False] * (w + 2)]
    for i in range(1, h + 1):
        field[i] = [False] + [True] * w + [False]
    for i in range(1, h + 1):
        s = input().rstrip()
        for j in range(1, w + 1):
            if s[j-1] == "@":
                field[i][j] = False
    q.append((x1, y1))
    ans = 0
    c = [[0] * (w + 2) for i in range(h + 2)]
    while q:
        next_q = deque()
        while q:
            x, y = q.pop()
            if (x, y) == (x2, y2):
                print(ans)
                return
            if field[y][x]:
                for i in range(1, K + 1):
                    if field[y + i][x]:
                        if not c[y + i][x]:
                            next_q.append((x, y + i))
                            c[y + i][x] = 1
                    else:
                        break
                for i in range(1, K + 1):
                    if field[y][x + i]:
                        if not c[y][x + i]:
                            next_q.append((x + i, y))
                            c[y][x + i] = 1
                    else:
                        break
                for i in range(1, K + 1):
                    if field[y - i][x]:
                        if not c[y - i][x]:
                            next_q.append((x, y - i))
                            c[y - i][x] = 1
                    else:
                        break
                for i in range(1, K + 1):
                    if field[y][x - i]:
                        if not c[y][x - i]:
                            next_q.append((x - i, y))
                            c[y][x - i] = 1
                    else:
                        break
                field[y][x] = False
        ans += 1
        q = next_q
    print(-1)
    return


#main
if __name__ == '__main__':
    solve()

Buggy python submission (verdict: Time Limit Exceeded):
#!/usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys, random, itertools, math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt
def LI(): return map(int, input().split())
def LF(): return list(map(float, input().split()))
def LI_(): return list(map(lambda x: int(x)-1, input().split()))
def II(): return int(input())
def IF(): return float(input())
def S(): return input().rstrip()
def LS(): return S().split()
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = float("INF")

#solve
def solve():
    mxy = [(0, 1), (1, 0), (-1, 0), (0, -1)]
    h, w, K = LI()
    y1, x1, y2, x2 = LI_()
    c = SR(h)
    q = deque()
    q.append((x1, y1, 0, 0))
    q.append((x1, y1, 1, 0))
    q.append((x1, y1, 2, 0))
    q.append((x1, y1, 3, 0))
    field = [None] * h
    for i in range(h):
        field[i] = [inf] * w
    field[y1][x1] = 0
    while q:
        x, y, d, k = q.popleft()
        mx, my = mxy[d]
        mx += x
        my += y
        if 0 <= my < h and 0 <= mx < w:
            if c[my][mx] == "@": continue
            s = field[y][x] + (k == 0)
            k += 1
            if K == k:
                if field[my][mx] >= s:
                    field[my][mx] = s
                    q.append((mx, my, 0, 0))
                    q.append((mx, my, 1, 0))
                    q.append((mx, my, 2, 0))
                    q.append((mx, my, 3, 0))
            else:
                if field[my][mx] >= s:
                    field[my][mx] = s
                    for i in range(4):
                        if i == d:
                            q.appendleft((mx, my, d, k))
                        else:
                            q.append((mx, my, i, 0))
    print(field[y2][x2] if field[y2][x2] != inf else -1)
    return


#main
if __name__ == '__main__':
    solve()


Fixed submission:
#!/usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys, random, itertools, math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt
def LI(): return map(int, input().split())
def LF(): return list(map(float, input().split()))
def LI_(): return list(map(lambda x: int(x)-1, input().split()))
def II(): return int(input())
def IF(): return float(input())
def S(): return input().rstrip()
def LS(): return S().split()
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = float("INF")

#solve
def solve():
    mxy = [(0, 1), (1, 0), (-1, 0), (0, -1)]
    h, w, K = LI()
    y1, x1, y2, x2 = LI()
    q = deque()
    field = [[False] * (w + 2)] + [None] * h + [[False] * (w + 2)]
    for i in range(1, h + 1):
        field[i] = [False] + [True] * w + [False]
    for i in range(1, h + 1):
        s = input().rstrip()
        for j in range(1, w + 1):
            if s[j-1] == "@":
                field[i][j] = False
    q.append((x1, y1))
    ans = 0
    c = [[0] * (w + 2) for i in range(h + 2)]
    while q:
        next_q = deque()
        while q:
            x, y = q.pop()
            if (x, y) == (x2, y2):
                print(ans)
                return
            if field[y][x]:
                for i in range(1, K + 1):
                    if field[y + i][x]:
                        if not c[y + i][x]:
                            next_q.append((x, y + i))
                            c[y + i][x] = 1
                    else:
                        break
                for i in range(1, K + 1):
                    if field[y][x + i]:
                        if not c[y][x + i]:
                            next_q.append((x + i, y))
                            c[y][x + i] = 1
                    else:
                        break
                for i in range(1, K + 1):
                    if field[y - i][x]:
                        if not c[y - i][x]:
                            next_q.append((x, y - i))
                            c[y - i][x] = 1
                    else:
                        break
                for i in range(1, K + 1):
                    if field[y][x - i]:
                        if not c[y][x - i]:
                            next_q.append((x - i, y))
                            c[y][x - i] = 1
                    else:
                        break
                field[y][x] = False
        ans += 1
        q = next_q
    print(-1)
    return


#main
if __name__ == '__main__':
    solve()