Buggy python submission (verdict: Runtime Error):
import sys

sys.setrecursionlimit(500005)
stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()  # ignore trailing spaces

h, w, K = na()
x1, y1, x2, y2 = na()
x1 -= 1
y1 -= 1
x2 -= 1
y2 -= 1

bd = [ns() for _ in range(h)]

q = deque()
ds = [9999999999999999] * (h*w)

q.append((x1, y1))
ds[x1*w+y1] = 0
dr = (1, 0, -1, 0)
dc = (0, 1, 0, -1)
while len(q) > 0:
    r, c = q.popleft()
    for i in range(4):
        for j in range(1, K+1):
            nr, nc = r + dr[i] * j, c + dc[i] * j
            if 0 <= nr < h and 0 <= nc < w and bd[nr][nc] != "@":
                nd = ds[r*w+c] + 1
                if nd < ds[nr*w+nc]:
                    ds[nr*w+nc] = nd
                    q.append((nr, nc))
                elif nd <= ds[nr*w+nc]:
                    continue
                else:
                    break
            else:
                break
ans = ds[x2*w+y2]
if ans < 999999999999:
    print(ans)
else:
    print(-1)


Fixed submission:
from collections import deque
import sys

sys.setrecursionlimit(500005)
stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()  # ignore trailing spaces

def rights(bd):
    n = len(bd)
    m = len(bd[0])
    rs = [[0] * m for _ in range(n)]
    for i in range(n):
        for j in range(m-1, -1, -1):
            if bd[i][j] == "@":
                rs[i][j] = 0
            elif j == m-1:
                rs[i][j] = 1
            else:
                rs[i][j] = rs[i][j+1] + 1
    return rs

def rotc(bd):
    n = len(bd)
    m = len(bd[0])
    rs = [[0] * n for _ in range(m)]
    for i in range(n):
        for j in range(m):
            rs[j][n-1-i] = bd[i][j]
    return rs


h, w, k = na()
x1, y1, x2, y2 = na()
x1 -= 1
y1 -= 1
x2 -= 1
y2 -= 1

bd = [ns() for _ in range(h)]

right = rights(bd)
bd = rotc(bd)
up = rotc(rotc(rotc(rights(bd))))
bd = rotc(bd)
left = rotc(rotc(rights(bd)))
bd = rotc(bd)
down = rotc(rights(bd))
bd = rotc(bd)


q = deque()
rh = 1
while rh < w:
    rh *= 2
ch = 1
while ch < h:
    ch *= 2
ds = [9999999999] * (h*w+h*2*rh+w*2*ch)
q.append(x1*w+y1)
ds[x1*w+y1] = 0

# dr = (1, 0, -1, 0)
# dc = (0, 1, 0, -1)

# h*w
# h*2*rh
# w*2*ch
o0 = h*w
o1 = h*w + h*2*rh
while len(q) > 0:
    cur = q.popleft()
    curd = ds[cur]
    if cur < o0:
        r = cur // w
        c = cur % w

        rr = min(c + right[r][c] - 1, c + k) + 1
        ll = max(c - left[r][c] + 1, c - k)
        while ll != 0:
            f = ll & -ll
            if ll + f > rr:
                break
            tar = o0 + r*2*rh + (rh+ll)//f
            if ds[tar] > ds[cur] + 1:
                ds[tar] = ds[cur] + 1
                q.append(tar)
            ll += f
        while ll < rr:
            f = rr & -rr
            tar = o0 + r*2*rh + (rh+rr)//f-1
            if ds[tar] > ds[cur] + 1:
                ds[tar] = ds[cur] + 1
                q.append(tar)
            rr -= f

        rr = min(r + down[r][c] - 1, r + k) + 1
        ll = max(r - up[r][c] + 1, r - k)
        while ll != 0:
            f = ll & -ll
            if ll + f > rr:
                break
            tar = o1 + c * 2*ch + (ch + ll) // f
            if ds[tar] > ds[cur] + 1:
                ds[tar] = ds[cur] + 1
                q.append(tar)
            ll += f
        while ll < rr:
            f = rr & -rr
            tar = o1 + c * 2*ch + (ch + rr) // f - 1
            if ds[tar] > ds[cur] + 1:
                ds[tar] = ds[cur] + 1
                q.append(tar)
            rr -= f
    elif cur < o1:
        cur -= o0
        r = cur // (2*rh)
        v = cur % (2*rh)
        if v >= rh:
            tar = r*w + v-rh
            if ds[tar] > curd and bd[r][v-rh] == ".":
                ds[tar] = curd
                q.appendleft(tar)
        else:
            for j in range(2*v, 2*v+2):
                tar = o0 + r*2*rh + j
                if ds[tar] > curd:
                    ds[tar] = curd
                    q.appendleft(tar)
    else:
        cur -= o1
        c = cur // (2 * ch)
        v = cur % (2 * ch)
        if v >= ch:
            tar = (v-ch) * w + c
            if ds[tar] > curd and bd[v-ch][c] == ".":
                ds[tar] = curd
                q.appendleft(tar)
        else:
            for j in range(2 * v, 2 * v + 2):
                tar = o1 + c * 2 * ch + j
                if ds[tar] > curd:
                    ds[tar] = curd
                    q.appendleft(tar)

if ds[x2*w+y2] > 9999999:
    print(-1)
else:
    print(ds[x2*w+y2])

Buggy python submission (verdict: Time Limit Exceeded):
from heapq import heappop, heappush
import sys

sys.setrecursionlimit(500005)
stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()  # ignore trailing spaces

h, w, K = na()
x1, y1, x2, y2 = na()
x1 -= 1
y1 -= 1
x2 -= 1
y2 -= 1

bd = [ns() for _ in range(h)]

q = []
ds = [9999999999999999] * (h*w*4)

for i in range(4):
    ds[(x1*w+y1)*4+i] = 0
    heappush(q, ds[(x1*w+y1)*4+i]<<60|x1<<40|y1<<20|i)

dr = (1, 0, -1, 0)
dc = (0, 1, 0, -1)
while len(q) > 0:
    cur = heappop(q)
    d = cur>>60
    r = cur>>40&(1<<20)-1
    c = cur>>20&(1<<20)-1
    di = cur&(1<<20)-1
    # d, r, c, di = heappop(q)
    for i in range(4):
        nr, nc = r + dr[i], c + dc[i]
        if 0 <= nr < h and 0 <= nc < w and bd[nr][nc] != "@":
            nd = d + 1 if di == i else (d + K-1) // K * K + 1
            nex = (nr*w+nc)*4+i
            if nd < ds[nex]:
                ds[nex] = nd
                heappush(q, nd<<60|nr<<40|nc<<20|i)

ans = min([ds[(x2*w+y2)*4+i] for i in range(4)])
if ans < 999999999999:
    print((ans+K-1)//K)
else:
    print(-1)


Fixed submission:
from collections import deque
import sys

sys.setrecursionlimit(500005)
stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()  # ignore trailing spaces

def rights(bd):
    n = len(bd)
    m = len(bd[0])
    rs = [[0] * m for _ in range(n)]
    for i in range(n):
        for j in range(m-1, -1, -1):
            if bd[i][j] == "@":
                rs[i][j] = 0
            elif j == m-1:
                rs[i][j] = 1
            else:
                rs[i][j] = rs[i][j+1] + 1
    return rs

def rotc(bd):
    n = len(bd)
    m = len(bd[0])
    rs = [[0] * n for _ in range(m)]
    for i in range(n):
        for j in range(m):
            rs[j][n-1-i] = bd[i][j]
    return rs


h, w, k = na()
x1, y1, x2, y2 = na()
x1 -= 1
y1 -= 1
x2 -= 1
y2 -= 1

bd = [ns() for _ in range(h)]

right = rights(bd)
bd = rotc(bd)
up = rotc(rotc(rotc(rights(bd))))
bd = rotc(bd)
left = rotc(rotc(rights(bd)))
bd = rotc(bd)
down = rotc(rights(bd))
bd = rotc(bd)


q = deque()
rh = 1
while rh < w:
    rh *= 2
ch = 1
while ch < h:
    ch *= 2
ds = [9999999999] * (h*w+h*2*rh+w*2*ch)
q.append(x1*w+y1)
ds[x1*w+y1] = 0

# dr = (1, 0, -1, 0)
# dc = (0, 1, 0, -1)

# h*w
# h*2*rh
# w*2*ch
o0 = h*w
o1 = h*w + h*2*rh
while len(q) > 0:
    cur = q.popleft()
    curd = ds[cur]
    if cur < o0:
        r = cur // w
        c = cur % w

        rr = min(c + right[r][c] - 1, c + k) + 1
        ll = max(c - left[r][c] + 1, c - k)
        while ll != 0:
            f = ll & -ll
            if ll + f > rr:
                break
            tar = o0 + r*2*rh + (rh+ll)//f
            if ds[tar] > ds[cur] + 1:
                ds[tar] = ds[cur] + 1
                q.append(tar)
            ll += f
        while ll < rr:
            f = rr & -rr
            tar = o0 + r*2*rh + (rh+rr)//f-1
            if ds[tar] > ds[cur] + 1:
                ds[tar] = ds[cur] + 1
                q.append(tar)
            rr -= f

        rr = min(r + down[r][c] - 1, r + k) + 1
        ll = max(r - up[r][c] + 1, r - k)
        while ll != 0:
            f = ll & -ll
            if ll + f > rr:
                break
            tar = o1 + c * 2*ch + (ch + ll) // f
            if ds[tar] > ds[cur] + 1:
                ds[tar] = ds[cur] + 1
                q.append(tar)
            ll += f
        while ll < rr:
            f = rr & -rr
            tar = o1 + c * 2*ch + (ch + rr) // f - 1
            if ds[tar] > ds[cur] + 1:
                ds[tar] = ds[cur] + 1
                q.append(tar)
            rr -= f
    elif cur < o1:
        cur -= o0
        r = cur // (2*rh)
        v = cur % (2*rh)
        if v >= rh:
            tar = r*w + v-rh
            if ds[tar] > curd and bd[r][v-rh] == ".":
                ds[tar] = curd
                q.appendleft(tar)
        else:
            for j in range(2*v, 2*v+2):
                tar = o0 + r*2*rh + j
                if ds[tar] > curd:
                    ds[tar] = curd
                    q.appendleft(tar)
    else:
        cur -= o1
        c = cur // (2 * ch)
        v = cur % (2 * ch)
        if v >= ch:
            tar = (v-ch) * w + c
            if ds[tar] > curd and bd[v-ch][c] == ".":
                ds[tar] = curd
                q.appendleft(tar)
        else:
            for j in range(2 * v, 2 * v + 2):
                tar = o1 + c * 2 * ch + j
                if ds[tar] > curd:
                    ds[tar] = curd
                    q.appendleft(tar)

if ds[x2*w+y2] > 9999999:
    print(-1)
else:
    print(ds[x2*w+y2])

Buggy python submission (verdict: Time Limit Exceeded):
from heapq import heappop, heappush
import sys

sys.setrecursionlimit(500005)
stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()  # ignore trailing spaces

h, w, K = na()
x1, y1, x2, y2 = na()
x1 -= 1
y1 -= 1
x2 -= 1
y2 -= 1

bd = [ns() for _ in range(h)]

q = []
ds = [9999999999999999] * (h*w*4)

# 0:53
for i in range(4):
    ds[(x1*w+y1)*4+i] = 0
    heappush(q, (ds[(x1*w+y1)*4+i], x1, y1, i))

dr = (1, 0, -1, 0)
dc = (0, 1, 0, -1)
while len(q) > 0:
    d, r, c, di = heappop(q)
    for i in range(4):
        nr, nc = r + dr[i], c + dc[i]
        if 0 <= nr < h and 0 <= nc < w and bd[nr][nc] != "@":
            if di == i:
                nd = d + 1
            else:
                nd = (d + K-1) // K * K + 1
            if nd < ds[(nr*w+nc)*4+i] :
                ds[(nr * w + nc) * 4 + i] = nd
                heappush(q, (nd, nr, nc, i))

ans = min([ds[(x2*w+y2)*4+i] for i in range(4)])
if ans < 999999999999:
    print((ans+K-1)//K)
else:
    print(-1)


Fixed submission:
from collections import deque
import sys

sys.setrecursionlimit(500005)
stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()  # ignore trailing spaces

def rights(bd):
    n = len(bd)
    m = len(bd[0])
    rs = [[0] * m for _ in range(n)]
    for i in range(n):
        for j in range(m-1, -1, -1):
            if bd[i][j] == "@":
                rs[i][j] = 0
            elif j == m-1:
                rs[i][j] = 1
            else:
                rs[i][j] = rs[i][j+1] + 1
    return rs

def rotc(bd):
    n = len(bd)
    m = len(bd[0])
    rs = [[0] * n for _ in range(m)]
    for i in range(n):
        for j in range(m):
            rs[j][n-1-i] = bd[i][j]
    return rs


h, w, k = na()
x1, y1, x2, y2 = na()
x1 -= 1
y1 -= 1
x2 -= 1
y2 -= 1

bd = [ns() for _ in range(h)]

right = rights(bd)
bd = rotc(bd)
up = rotc(rotc(rotc(rights(bd))))
bd = rotc(bd)
left = rotc(rotc(rights(bd)))
bd = rotc(bd)
down = rotc(rights(bd))
bd = rotc(bd)


q = deque()
rh = 1
while rh < w:
    rh *= 2
ch = 1
while ch < h:
    ch *= 2
ds = [9999999999] * (h*w+h*2*rh+w*2*ch)
q.append(x1*w+y1)
ds[x1*w+y1] = 0

# dr = (1, 0, -1, 0)
# dc = (0, 1, 0, -1)

# h*w
# h*2*rh
# w*2*ch
o0 = h*w
o1 = h*w + h*2*rh
while len(q) > 0:
    cur = q.popleft()
    curd = ds[cur]
    if cur < o0:
        r = cur // w
        c = cur % w

        rr = min(c + right[r][c] - 1, c + k) + 1
        ll = max(c - left[r][c] + 1, c - k)
        while ll != 0:
            f = ll & -ll
            if ll + f > rr:
                break
            tar = o0 + r*2*rh + (rh+ll)//f
            if ds[tar] > ds[cur] + 1:
                ds[tar] = ds[cur] + 1
                q.append(tar)
            ll += f
        while ll < rr:
            f = rr & -rr
            tar = o0 + r*2*rh + (rh+rr)//f-1
            if ds[tar] > ds[cur] + 1:
                ds[tar] = ds[cur] + 1
                q.append(tar)
            rr -= f

        rr = min(r + down[r][c] - 1, r + k) + 1
        ll = max(r - up[r][c] + 1, r - k)
        while ll != 0:
            f = ll & -ll
            if ll + f > rr:
                break
            tar = o1 + c * 2*ch + (ch + ll) // f
            if ds[tar] > ds[cur] + 1:
                ds[tar] = ds[cur] + 1
                q.append(tar)
            ll += f
        while ll < rr:
            f = rr & -rr
            tar = o1 + c * 2*ch + (ch + rr) // f - 1
            if ds[tar] > ds[cur] + 1:
                ds[tar] = ds[cur] + 1
                q.append(tar)
            rr -= f
    elif cur < o1:
        cur -= o0
        r = cur // (2*rh)
        v = cur % (2*rh)
        if v >= rh:
            tar = r*w + v-rh
            if ds[tar] > curd and bd[r][v-rh] == ".":
                ds[tar] = curd
                q.appendleft(tar)
        else:
            for j in range(2*v, 2*v+2):
                tar = o0 + r*2*rh + j
                if ds[tar] > curd:
                    ds[tar] = curd
                    q.appendleft(tar)
    else:
        cur -= o1
        c = cur // (2 * ch)
        v = cur % (2 * ch)
        if v >= ch:
            tar = (v-ch) * w + c
            if ds[tar] > curd and bd[v-ch][c] == ".":
                ds[tar] = curd
                q.appendleft(tar)
        else:
            for j in range(2 * v, 2 * v + 2):
                tar = o1 + c * 2 * ch + j
                if ds[tar] > curd:
                    ds[tar] = curd
                    q.appendleft(tar)

if ds[x2*w+y2] > 9999999:
    print(-1)
else:
    print(ds[x2*w+y2])