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

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

c_array = [input() for _ in range(H)]

ans_array = [-1] * (H * W * 4)

pos = deque()

start_pos = ((x1 - 1) * W + (y1 - 1)) * 4
goal_pos = ((x2 - 1) * W + (y2 - 1)) * 4

for i in range(4):
    pos.append(start_pos + i)
    ans_array[start_pos + i] = 0

while(len(pos)):
    now_pos = pos.popleft()
    d = now_pos % 4
    h = (now_pos - d) // (4 * W) + 1
    w = ((now_pos - d) // 4) % W + 1
    # print(h, w, d, ans_array[now_pos])
    if d == 0:
        for i in range(1, K + 1):
            if h + i > H or c_array[h + i - 1][w - 1] == '@':
                break
            newpos_array = [((h + i - 1) * W + (w - 1)) * 4 + 1, ((h + i - 1)
                                                                  * W + (w - 1)) * 4 + 3, ((h + i - 1) * W + (w - 1)) * 4 + 0, ((h + i - 1) * W + (w - 1)) * 4 + 2]
            for n, newpos in enumerate(newpos_array):
                if newpos >= goal_pos and newpos < goal_pos + 4:
                    print(ans_array[now_pos] + 1)
                    exit()
                if ans_array[newpos] == -1:
                    ans_array[newpos] = ans_array[now_pos] + 1
                    if n < 2 or (n == 2 and i == K):
                        pos.append(newpos)
    if d == 1:
        for i in range(1, K + 1):
            if w + i > W or c_array[h - 1][w + i - 1] == '@':
                break
            newpos_array = [((h - 1) * W + (w + i - 1)) * 4 + 0, ((h - 1) *
                                                                  W + (w + i - 1)) * 4 + 2, ((h - 1) * W + (w + i - 1)) * 4 + 1,((h - 1) * W + (w + i - 1)) * 4 + 3]
            for n, newpos in enumerate(newpos_array):
                if newpos >= goal_pos and newpos < goal_pos + 4:
                    print(ans_array[now_pos] + 1)
                    exit()
                if n == 2 and i != K:
                    break
                if ans_array[newpos] == -1:
                    ans_array[newpos] = ans_array[now_pos] + 1
                    if n < 2 or (n == 2 and i == K):
                        pos.append(newpos)
    if d == 2:
        for i in range(1, K + 1):
            if h - i < 1 or c_array[h - i - 1][w - 1] == '@':
                break
            newpos_array = [((h - i - 1) * W + (w - 1)) * 4 + 1, ((h - i - 1)
                                                                  * W + (w - 1)) * 4 + 3, ((h - i - 1) * W + (w - 1)) * 4 + 2,((h - i - 1) * W + (w - 1)) * 4 + 0]
            for n, newpos in enumerate(newpos_array):
                if newpos >= goal_pos and newpos < goal_pos + 4:
                    print(ans_array[now_pos] + 1)
                    exit()
                if n == 2 and i != K:
                    break
                if ans_array[newpos] == -1:
                    ans_array[newpos] = ans_array[now_pos] + 1
                    if n < 2 or (n == 2 and i == K):
                        pos.append(newpos)
    if d == 3:
        for i in range(1, K + 1):
            if w - i < 1 or c_array[h - 1][w - i - 1] == '@':
                break
            newpos_array = [((h - 1) * W + (w - i - 1)) * 4 + 0, ((h - 1) *
                                                                  W + (w - i - 1)) * 4 + 2, ((h - 1) * W + (w - i - 1)) * 4 + 3,((h - 1) * W + (w - i - 1)) * 4 + 1]
            for n, newpos in enumerate(newpos_array):
                if newpos >= goal_pos and newpos < goal_pos + 4:
                    print(ans_array[now_pos] + 1)
                    exit()
                if n == 2 and i != K:
                    break
                if ans_array[newpos] == -1:
                    ans_array[newpos] = ans_array[now_pos] + 1
                    if n < 2 or (n == 2 and i == K):
                        pos.append(newpos)
print(-1)


Fixed submission:
from collections import deque

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

c_array = [input() for _ in range(H)]

ans_array = [-1] * (H * W * 4)

pos = deque()

start_pos = ((x1 - 1) * W + (y1 - 1)) * 4
goal_pos = ((x2 - 1) * W + (y2 - 1)) * 4

for i in range(4):
    pos.append(start_pos + i)
    ans_array[start_pos + i] = 0

while(len(pos)):
    now_pos = pos.popleft()
    d = now_pos % 4
    h = (now_pos - d) // (4 * W) + 1
    w = ((now_pos - d) // 4) % W + 1
    # print(h, w, d, ans_array[now_pos])
    if d == 0:
        for i in range(1, K + 1):
            if h + i > H or c_array[h + i - 1][w - 1] == '@':
                break
            newpos_array = [((h + i - 1) * W + (w - 1)) * 4 + 1, ((h + i - 1)
                                                                  * W + (w - 1)) * 4 + 3, ((h + i - 1) * W + (w - 1)) * 4 + 0, ((h + i - 1) * W + (w - 1)) * 4 + 2]
            for n, newpos in enumerate(newpos_array):
                if newpos >= goal_pos and newpos < goal_pos + 4:
                    print(ans_array[now_pos] + 1)
                    exit()
                if ans_array[newpos] == -1:
                    ans_array[newpos] = ans_array[now_pos] + 1
                    if n < 2 or (n == 2 and i == K):
                        pos.append(newpos)
    if d == 1:
        for i in range(1, K + 1):
            if w + i > W or c_array[h - 1][w + i - 1] == '@':
                break
            newpos_array = [((h - 1) * W + (w + i - 1)) * 4 + 0, ((h - 1) *
                                                                  W + (w + i - 1)) * 4 + 2, ((h - 1) * W + (w + i - 1)) * 4 + 1, ((h - 1) * W + (w + i - 1)) * 4 + 3]
            for n, newpos in enumerate(newpos_array):
                if newpos >= goal_pos and newpos < goal_pos + 4:
                    print(ans_array[now_pos] + 1)
                    exit()
                if ans_array[newpos] == -1:
                    ans_array[newpos] = ans_array[now_pos] + 1
                    if n < 2 or (n == 2 and i == K):
                        pos.append(newpos)
    if d == 2:
        for i in range(1, K + 1):
            if h - i < 1 or c_array[h - i - 1][w - 1] == '@':
                break
            newpos_array = [((h - i - 1) * W + (w - 1)) * 4 + 1, ((h - i - 1)
                                                                  * W + (w - 1)) * 4 + 3, ((h - i - 1) * W + (w - 1)) * 4 + 2, ((h - i - 1) * W + (w - 1)) * 4 + 0]
            for n, newpos in enumerate(newpos_array):
                if newpos >= goal_pos and newpos < goal_pos + 4:
                    print(ans_array[now_pos] + 1)
                    exit()
                if ans_array[newpos] == -1:
                    ans_array[newpos] = ans_array[now_pos] + 1
                    if n < 2 or (n == 2 and i == K):
                        pos.append(newpos)
    if d == 3:
        for i in range(1, K + 1):
            if w - i < 1 or c_array[h - 1][w - i - 1] == '@':
                break
            newpos_array = [((h - 1) * W + (w - i - 1)) * 4 + 0, ((h - 1) *
                                                                  W + (w - i - 1)) * 4 + 2, ((h - 1) * W + (w - i - 1)) * 4 + 3, ((h - 1) * W + (w - i - 1)) * 4 + 1]
            for n, newpos in enumerate(newpos_array):
                if newpos >= goal_pos and newpos < goal_pos + 4:
                    print(ans_array[now_pos] + 1)
                    exit()
                if ans_array[newpos] == -1:
                    ans_array[newpos] = ans_array[now_pos] + 1
                    if n < 2 or (n == 2 and i == K):
                        pos.append(newpos)
print(-1)

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

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

c_array = [input() for _ in range(H)]

ans_array = [-1] * (H * W * 4)

pos = deque()

start_pos = ((x1 - 1) * W + (y1 - 1)) * 4
goal_pos = ((x2 - 1) * W + (y2 - 1)) * 4

for i in range(4):
    pos.append(start_pos + i)
    ans_array[start_pos + i] = 0

while(len(pos)):
    now_pos = pos.popleft()
    d = now_pos % 4
    h = (now_pos - d) // (4 * W) + 1
    w = ((now_pos - d) // 4) % W + 1
    # print(h, w, d, ans_array[now_pos])
    if d == 0:
        for i in range(1, K + 1):
            if h + i > H or c_array[h + i - 1][w - 1] == '@':
                break
            newpos_array = [((h + i - 1) * W + (w - 1)) * 4 + 1, ((h + i - 1)
                                                                  * W + (w - 1)) * 4 + 3, ((h + i - 1) * W + (w - 1)) * 4 + 0]
            for n, newpos in enumerate(newpos_array):
                if newpos >= goal_pos and newpos < goal_pos + 4:
                    print(ans_array[now_pos] + 1)
                    exit()
                if n == 2 and i != K:
                    break
                if ans_array[newpos] == -1:
                    pos.append(newpos)
                    ans_array[newpos] = ans_array[now_pos] + 1
    if d == 1:
        for i in range(1, K + 1):
            if w + i > W or c_array[h - 1][w + i - 1] == '@':
                break
            newpos_array = [((h - 1) * W + (w + i - 1)) * 4 + 0, ((h - 1) *
                                                                  W + (w + i - 1)) * 4 + 2, ((h - 1) * W + (w + i - 1)) * 4 + 1]
            for n, newpos in enumerate(newpos_array):
                if newpos >= goal_pos and newpos < goal_pos + 4:
                    print(ans_array[now_pos] + 1)
                    exit()
                if n == 2 and i != K:
                    break
                if ans_array[newpos] == -1:
                    pos.append(newpos)
                    ans_array[newpos] = ans_array[now_pos] + 1
    if d == 2:
        for i in range(1, K + 1):
            if h - i < 1 or c_array[h - i - 1][w - 1] == '@':
                break
            newpos_array = [((h - i - 1) * W + (w - 1)) * 4 + 1, ((h - i - 1)
                                                                  * W + (w - 1)) * 4 + 3, ((h - i - 1) * W + (w - 1)) * 4 + 2]
            for n, newpos in enumerate(newpos_array):
                if newpos >= goal_pos and newpos < goal_pos + 4:
                    print(ans_array[now_pos] + 1)
                    exit()
                if n == 2 and i != K:
                    break
                if ans_array[newpos] == -1:
                    pos.append(newpos)
                    ans_array[newpos] = ans_array[now_pos] + 1
    if d == 3:
        for i in range(1, K + 1):
            if w - i < 1 or c_array[h - 1][w - i - 1] == '@':
                break
            newpos_array = [((h - 1) * W + (w - i - 1)) * 4 + 0, ((h - 1) *
                                                                  W + (w - i - 1)) * 4 + 2, ((h - 1) * W + (w - i - 1)) * 4 + 3]
            for n, newpos in enumerate(newpos_array):
                if newpos >= goal_pos and newpos < goal_pos + 4:
                    print(ans_array[now_pos] + 1)
                    exit()
                if n == 2 and i != K:
                    break
                if ans_array[newpos] == -1:
                    pos.append(newpos)
                    ans_array[newpos] = ans_array[now_pos] + 1
print(-1)


Fixed submission:
from collections import deque

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

c_array = [input() for _ in range(H)]

ans_array = [-1] * (H * W * 4)

pos = deque()

start_pos = ((x1 - 1) * W + (y1 - 1)) * 4
goal_pos = ((x2 - 1) * W + (y2 - 1)) * 4

for i in range(4):
    pos.append(start_pos + i)
    ans_array[start_pos + i] = 0

while(len(pos)):
    now_pos = pos.popleft()
    d = now_pos % 4
    h = (now_pos - d) // (4 * W) + 1
    w = ((now_pos - d) // 4) % W + 1
    # print(h, w, d, ans_array[now_pos])
    if d == 0:
        for i in range(1, K + 1):
            if h + i > H or c_array[h + i - 1][w - 1] == '@':
                break
            newpos_array = [((h + i - 1) * W + (w - 1)) * 4 + 1, ((h + i - 1)
                                                                  * W + (w - 1)) * 4 + 3, ((h + i - 1) * W + (w - 1)) * 4 + 0, ((h + i - 1) * W + (w - 1)) * 4 + 2]
            for n, newpos in enumerate(newpos_array):
                if newpos >= goal_pos and newpos < goal_pos + 4:
                    print(ans_array[now_pos] + 1)
                    exit()
                if ans_array[newpos] == -1:
                    ans_array[newpos] = ans_array[now_pos] + 1
                    if n < 2 or (n == 2 and i == K):
                        pos.append(newpos)
    if d == 1:
        for i in range(1, K + 1):
            if w + i > W or c_array[h - 1][w + i - 1] == '@':
                break
            newpos_array = [((h - 1) * W + (w + i - 1)) * 4 + 0, ((h - 1) *
                                                                  W + (w + i - 1)) * 4 + 2, ((h - 1) * W + (w + i - 1)) * 4 + 1, ((h - 1) * W + (w + i - 1)) * 4 + 3]
            for n, newpos in enumerate(newpos_array):
                if newpos >= goal_pos and newpos < goal_pos + 4:
                    print(ans_array[now_pos] + 1)
                    exit()
                if ans_array[newpos] == -1:
                    ans_array[newpos] = ans_array[now_pos] + 1
                    if n < 2 or (n == 2 and i == K):
                        pos.append(newpos)
    if d == 2:
        for i in range(1, K + 1):
            if h - i < 1 or c_array[h - i - 1][w - 1] == '@':
                break
            newpos_array = [((h - i - 1) * W + (w - 1)) * 4 + 1, ((h - i - 1)
                                                                  * W + (w - 1)) * 4 + 3, ((h - i - 1) * W + (w - 1)) * 4 + 2, ((h - i - 1) * W + (w - 1)) * 4 + 0]
            for n, newpos in enumerate(newpos_array):
                if newpos >= goal_pos and newpos < goal_pos + 4:
                    print(ans_array[now_pos] + 1)
                    exit()
                if ans_array[newpos] == -1:
                    ans_array[newpos] = ans_array[now_pos] + 1
                    if n < 2 or (n == 2 and i == K):
                        pos.append(newpos)
    if d == 3:
        for i in range(1, K + 1):
            if w - i < 1 or c_array[h - 1][w - i - 1] == '@':
                break
            newpos_array = [((h - 1) * W + (w - i - 1)) * 4 + 0, ((h - 1) *
                                                                  W + (w - i - 1)) * 4 + 2, ((h - 1) * W + (w - i - 1)) * 4 + 3, ((h - 1) * W + (w - i - 1)) * 4 + 1]
            for n, newpos in enumerate(newpos_array):
                if newpos >= goal_pos and newpos < goal_pos + 4:
                    print(ans_array[now_pos] + 1)
                    exit()
                if ans_array[newpos] == -1:
                    ans_array[newpos] = ans_array[now_pos] + 1
                    if n < 2 or (n == 2 and i == K):
                        pos.append(newpos)
print(-1)