Buggy python submission (verdict: Time Limit Exceeded):
# coding: utf-8
# Your code here!

from collections import deque
# from pprint import pprint
moves=[(0,1),(1,0),(0,-1),(-1,0)]
# pp=pprint
INF=float("inf")

h,w,k=map(int,input().split())
x1,y1,x2,y2=map(lambda x:int(x)-1,input().split())
maze=[list(input()) for _ in range(h)]
cost=[[INF]*w for _ in range(h)]

todo=deque()
todo.append((x1,y1))
cost[x1][y1]=0

while todo:
    x,y=todo.popleft()
    for vx,vy in moves:
        for kk in range(1,k+1):
            nx=x+vx*kk
            ny=y+vy*kk
            if not(0<=nx<h) or not(0<=ny<w):
                break
                # 更に伸ばす必要はない
            if maze[nx][ny]=="@":
                break
                # 蓮の向こうにはいけないので
            if cost[nx][ny]<=cost[x][y]:
                break
                # 更新を1回以上受けていて、更に良くならない場合はbreak
            # if cost[nx][ny]==INF:
            cost[nx][ny]=cost[x][y]+1
            todo.append((nx,ny))
                # 更新されてなければ、[x][y]から1マス先を更新、キューに突っ込む


# pp(cost)
ans=cost[x2][y2]
if ans==INF:ans=-1
print(ans)

Fixed submission:
# coding: utf-8
# Your code here!

from collections import deque
from pprint import pprint
moves=[(0,1),(1,0),(0,-1),(-1,0)]
pp=pprint
# INF=float("inf")

h,w,k=map(int,input().split())
x1,y1,x2,y2=map(lambda x:int(x)-1,input().split())
maze=[list(input()) for _ in range(h)]
cost=[[-1]*w for _ in range(h)]

todo=deque()
todo.append((x1,y1))
cost[x1][y1]=0

while todo:
    x,y=todo.popleft()
    for vx,vy in moves:
        for kk in range(1,k+1):
            nx=x+vx*kk
            ny=y+vy*kk
            if not(0<=nx<h) or not(0<=ny<w):
                continue
            if maze[nx][ny]=="@":
                break
            if cost[nx][ny]!=-1 and cost[nx][ny]<=cost[x][y]:
                break
            if cost[nx][ny]!=-1:
                continue
            cost[nx][ny]=cost[x][y]+1
            todo.append((nx,ny))


# pp(cost)
ans=cost[x2][y2]
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
# coding: utf-8
# Your code here!

from collections import deque
from pprint import pprint
moves=[(0,1),(1,0),(0,-1),(-1,0)]
pp=pprint
# INF=float("inf")

h,w,k=map(int,input().split())
x1,y1,x2,y2=map(lambda x:int(x)-1,input().split())
maze=[list(input()) for _ in range(h)]
cost=[[-1]*w for _ in range(h)]

todo=deque()
todo.append((x1,y1))
cost[x1][y1]=0

while todo:
    x,y=todo.popleft()
    for vx,vy in moves:
        for kk in range(1,k+1):
            nx=x+vx*kk
            ny=y+vy*kk
            if not(0<=nx<h) or not(0<=ny<w):
                break
                # 更に伸ばす必要はない
            if maze[nx][ny]=="@":
                break
                # 蓮の向こうにはいけない
            # if cost[nx][ny]!=-1 and cost[nx][ny]<=cost[x][y]:
            #     # 更新を1回以上受けていて、更に良くならない場合はbreak
            #     break
            if cost[nx][ny]==-1:
                cost[nx][ny]=cost[x][y]+1
                todo.append((nx,ny))


# pp(cost)
ans=cost[x2][y2]
print(ans)

Fixed submission:
# coding: utf-8
# Your code here!

from collections import deque
from pprint import pprint
moves=[(0,1),(1,0),(0,-1),(-1,0)]
pp=pprint
# INF=float("inf")

h,w,k=map(int,input().split())
x1,y1,x2,y2=map(lambda x:int(x)-1,input().split())
maze=[list(input()) for _ in range(h)]
cost=[[-1]*w for _ in range(h)]

todo=deque()
todo.append((x1,y1))
cost[x1][y1]=0

while todo:
    x,y=todo.popleft()
    for vx,vy in moves:
        for kk in range(1,k+1):
            nx=x+vx*kk
            ny=y+vy*kk
            if not(0<=nx<h) or not(0<=ny<w):
                continue
            if maze[nx][ny]=="@":
                break
            if cost[nx][ny]!=-1 and cost[nx][ny]<=cost[x][y]:
                break
            if cost[nx][ny]!=-1:
                continue
            cost[nx][ny]=cost[x][y]+1
            todo.append((nx,ny))


# pp(cost)
ans=cost[x2][y2]
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
# coding: utf-8
# Your code here!

from collections import deque
from pprint import pprint
moves=[(0,1),(1,0),(0,-1),(-1,0)]
pp=pprint
# INF=float("inf")

h,w,k=map(int,input().split())
x1,y1,x2,y2=map(lambda x:int(x)-1,input().split())
maze=[list(input()) for _ in range(h)]
cost=[[-1]*w for _ in range(h)]

todo=deque()
todo.append((x1,y1))
cost[x1][y1]=0

while todo:
    x,y=todo.popleft()
    for vx,vy in moves:
        for kk in range(1,k+1):
            nx=x+vx*kk
            ny=y+vy*kk
            if not(0<=nx<h) or not(0<=ny<w):
                continue
            if maze[nx][ny]=="@":
                break
            if cost[nx][ny]!=-1 and cost[nx][ny]<=cost[x][y]:
                continue
            if cost[nx][ny]!=-1:
                continue
            cost[nx][ny]=cost[x][y]+1
            todo.append((nx,ny))


# pp(cost)
ans=cost[x2][y2]
print(ans)

Fixed submission:
# coding: utf-8
# Your code here!

from collections import deque
from pprint import pprint
moves=[(0,1),(1,0),(0,-1),(-1,0)]
pp=pprint
# INF=float("inf")

h,w,k=map(int,input().split())
x1,y1,x2,y2=map(lambda x:int(x)-1,input().split())
maze=[list(input()) for _ in range(h)]
cost=[[-1]*w for _ in range(h)]

todo=deque()
todo.append((x1,y1))
cost[x1][y1]=0

while todo:
    x,y=todo.popleft()
    for vx,vy in moves:
        for kk in range(1,k+1):
            nx=x+vx*kk
            ny=y+vy*kk
            if not(0<=nx<h) or not(0<=ny<w):
                continue
            if maze[nx][ny]=="@":
                break
            if cost[nx][ny]!=-1 and cost[nx][ny]<=cost[x][y]:
                break
            if cost[nx][ny]!=-1:
                continue
            cost[nx][ny]=cost[x][y]+1
            todo.append((nx,ny))


# pp(cost)
ans=cost[x2][y2]
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
# coding: utf-8
# Your code here!

from collections import deque
from pprint import pprint
moves=[(0,1),(1,0),(0,-1),(-1,0)]
pp=pprint
# INF=float("inf")

h,w,k=map(int,input().split())
x1,y1,x2,y2=map(lambda x:int(x)-1,input().split())
maze=[list(input()) for _ in range(h)]
cost=[[-1]*w for _ in range(h)]

todo=deque()
todo.append((x1,y1))
cost[x1][y1]=0

while todo:
    x,y=todo.popleft()
    for vx,vy in moves:
        for kk in range(1,k+1):
            nx=x+vx*kk
            ny=y+vy*kk
            if not(0<=nx<h) or not(0<=ny<w):
                continue
            if maze[nx][ny]=="@":
                break
            # if cost[nx][ny]!=-1 and cost[nx][ny]<=cost[x][y]:
            #     break
            if cost[nx][ny]!=-1:
                continue
            cost[nx][ny]=cost[x][y]+1
            todo.append((nx,ny))


# pp(cost)
ans=cost[x2][y2]
print(ans)

Fixed submission:
# coding: utf-8
# Your code here!

from collections import deque
from pprint import pprint
moves=[(0,1),(1,0),(0,-1),(-1,0)]
pp=pprint
# INF=float("inf")

h,w,k=map(int,input().split())
x1,y1,x2,y2=map(lambda x:int(x)-1,input().split())
maze=[list(input()) for _ in range(h)]
cost=[[-1]*w for _ in range(h)]

todo=deque()
todo.append((x1,y1))
cost[x1][y1]=0

while todo:
    x,y=todo.popleft()
    for vx,vy in moves:
        for kk in range(1,k+1):
            nx=x+vx*kk
            ny=y+vy*kk
            if not(0<=nx<h) or not(0<=ny<w):
                continue
            if maze[nx][ny]=="@":
                break
            if cost[nx][ny]!=-1 and cost[nx][ny]<=cost[x][y]:
                break
            if cost[nx][ny]!=-1:
                continue
            cost[nx][ny]=cost[x][y]+1
            todo.append((nx,ny))


# pp(cost)
ans=cost[x2][y2]
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
def LI():return list(map(int,input().split()))
INF=float("inf")
from collections import deque
def main():
    h,w,k=LI()
    x1,y1,x2,y2=LI()
    x1-=1
    y1-=1
    x2-=1
    y2-=1
    maze=list(list(input()) for _ in range(h))
    dist=[[INF]*(w) for _ in range(h)]
    q=deque([])
    q.append((x1,y1))
    dist[x1][y1]=0
    while q:
        x,y=q.popleft()
        for vx,vy in ([0,1],[0,-1],[1,0],[-1,0]):
            flg=True
            for kk in range(1,k+1):
                if not flg:
                    break
                nx=x+kk*vx
                ny=y+kk*vy
                if not(0<=nx<h) or not(0<=ny<w):
                    break
                if maze[nx][ny]=="@":
                    flg=False
                    break
                if dist[nx][ny]<=dist[x][y]:
                    break
                dist[nx][ny]=dist[x][y]+1
                q.append((nx,ny))
    ans=dist[x2][y2]
    if ans==INF:
        ans=-1
    print(ans)  
    
main()


Fixed submission:
# coding: utf-8
# Your code here!

from collections import deque
from pprint import pprint
moves=[(0,1),(1,0),(0,-1),(-1,0)]
pp=pprint
# INF=float("inf")

h,w,k=map(int,input().split())
x1,y1,x2,y2=map(lambda x:int(x)-1,input().split())
maze=[list(input()) for _ in range(h)]
cost=[[-1]*w for _ in range(h)]

todo=deque()
todo.append((x1,y1))
cost[x1][y1]=0

while todo:
    x,y=todo.popleft()
    for vx,vy in moves:
        for kk in range(1,k+1):
            nx=x+vx*kk
            ny=y+vy*kk
            if not(0<=nx<h) or not(0<=ny<w):
                continue
            if maze[nx][ny]=="@":
                break
            if cost[nx][ny]!=-1 and cost[nx][ny]<=cost[x][y]:
                break
            if cost[nx][ny]!=-1:
                continue
            cost[nx][ny]=cost[x][y]+1
            todo.append((nx,ny))


# pp(cost)
ans=cost[x2][y2]
print(ans)

Buggy python submission (verdict: Runtime Error):
def LI():return list(map(int,input().split()))

INF=float("inf")
# from pprint import*
from collections import deque

def main():
    MM=10**6
    h,w,k=LI()
    x1,y1,x2,y2=LI()
    x1-=1
    y1-=1
    x2-=1
    y2-=1
    maze=[None]*(h*MM+w)
    for i in range(h):
        tmp=list(input())
        for j in range(w):
            maze[i*MM+j]=tmp[j]
    # maze=[list(input()) for _ in range(h)]
    
    # dist=[[INF]*(w) for _ in range(h)]
    dist=[INF]*(h*MM+w)
    q=deque([])
    
    q.append((x1*MM+y1))
    dist[x1*MM+y1]=0
    while q:
        iii=q.popleft()
        xn,yn=divmod(iii,MM)
        # (xn,yn)=q.popleft()
        flg=True
        for kk in range(1,k+1):
            for i,j in [(0,-kk)]:
                if not flg:continue
                xx=xn+i
                yy=yn+j
                if (xx<0 or h<=xx) or (yy<0 or w<=yy):continue
                if maze[xx*MM+yy]=="@":flg=False;break
                if dist[xx*MM+yy]!=INF:continue
                dist[xx*MM+yy]=dist[xn*MM+yn]+1
                q.append((xx*MM+yy))
        flg=True
        for kk in range(1,k+1):
            for i,j in [(0,+kk)]:
                if not flg:continue
                xx=xn+i
                yy=yn+j
                if (xx<0 or h<=xx) or (yy<0 or w<=yy):continue
                if maze[xx*MM+yy]=="@":flg=False;break
                if dist[xx*MM+yy]!=INF:continue
                dist[xx*MM+yy]=dist[xn*MM+yn]+1
                q.append((xx*MM+yy))
        flg=True
        for kk in range(1,k+1):
            for i,j in [(kk,0)]:
                if not flg:continue
                xx=xn+i
                yy=yn+j
                if (xx<0 or h<=xx) or (yy<0 or w<=yy):continue
                if maze[xx*MM+yy]=="@":flg=False;break
                if dist[xx*MM+yy]!=INF:continue
                dist[xx*MM+yy]=dist[xn*MM+yn]+1
                q.append((xx*MM+yy))
        flg=True
        for kk in range(1,k+1):
            for i,j in [(-kk,0)]:
                if not flg:continue
                xx=xn+i
                yy=yn+j
                if (xx<0 or h<=xx) or (yy<0 or w<=yy):continue
                if maze[xx*MM+yy]=="@":flg=False;break
                if dist[xx*MM+yy]!=INF:continue
                dist[xx*MM+yy]=dist[xn*MM+yn]+1
                q.append((xx*MM+yy))
        
        
        
        
    # pprint(maze)
    # for j in dist:print(*j)
    ans=dist[x2*MM+y2]
    if ans==INF:
        ans=-1
    print(ans)  
    
main()

Fixed submission:
# coding: utf-8
# Your code here!

from collections import deque
from pprint import pprint
moves=[(0,1),(1,0),(0,-1),(-1,0)]
pp=pprint
# INF=float("inf")

h,w,k=map(int,input().split())
x1,y1,x2,y2=map(lambda x:int(x)-1,input().split())
maze=[list(input()) for _ in range(h)]
cost=[[-1]*w for _ in range(h)]

todo=deque()
todo.append((x1,y1))
cost[x1][y1]=0

while todo:
    x,y=todo.popleft()
    for vx,vy in moves:
        for kk in range(1,k+1):
            nx=x+vx*kk
            ny=y+vy*kk
            if not(0<=nx<h) or not(0<=ny<w):
                continue
            if maze[nx][ny]=="@":
                break
            if cost[nx][ny]!=-1 and cost[nx][ny]<=cost[x][y]:
                break
            if cost[nx][ny]!=-1:
                continue
            cost[nx][ny]=cost[x][y]+1
            todo.append((nx,ny))


# pp(cost)
ans=cost[x2][y2]
print(ans)

Buggy python submission (verdict: Runtime Error):
def LI():return list(map(int,input().split()))

INF=float("inf")
# from pprint import*
from collections import deque

def main():
    MM=10**7
    h,w,k=LI()
    x1,y1,x2,y2=LI()
    x1-=1
    y1-=1
    x2-=1
    y2-=1
    maze=[list(input()) for _ in range(h)]
    
    # dist=[[INF]*(w) for _ in range(h)]
    dist=[INF]*(h*MM+w)
    q=deque([])
    
    q.append((x1*MM+y1))
    dist[x1*MM+y1]=0
    while q:
        iii=q.popleft()
        xn,yn=divmod(iii,MM)
        # (xn,yn)=q.popleft()
        flg=True
        for kk in range(1,k+1):
            for i,j in [(0,-kk)]:
                if not flg:continue
                xx=xn+i
                yy=yn+j
                if (xx<0 or h<=xx) or (yy<0 or w<=yy):continue
                if maze[xx][yy]=="@":flg=False;break
                if dist[xx*MM+yy]!=INF:continue
                dist[xx*MM+yy]=dist[xn*MM+yn]+1
                q.append((xx*MM+yy))
        flg=True
        for kk in range(1,k+1):
            for i,j in [(0,+kk)]:
                if not flg:continue
                xx=xn+i
                yy=yn+j
                if (xx<0 or h<=xx) or (yy<0 or w<=yy):continue
                if maze[xx][yy]=="@":flg=False;break
                if dist[xx*MM+yy]!=INF:continue
                dist[xx*MM+yy]=dist[xn*MM+yn]+1
                q.append((xx*MM+yy))
        flg=True
        for kk in range(1,k+1):
            for i,j in [(kk,0)]:
                if not flg:continue
                xx=xn+i
                yy=yn+j
                if (xx<0 or h<=xx) or (yy<0 or w<=yy):continue
                if maze[xx][yy]=="@":flg=False;break
                if dist[xx*MM+yy]!=INF:continue
                dist[xx*MM+yy]=dist[xn*MM+yn]+1
                q.append((xx*MM+yy))
        flg=True
        for kk in range(1,k+1):
            for i,j in [(-kk,0)]:
                if not flg:continue
                xx=xn+i
                yy=yn+j
                if (xx<0 or h<=xx) or (yy<0 or w<=yy):continue
                if maze[xx][yy]=="@":flg=False;break
                if dist[xx*MM+yy]!=INF:continue
                dist[xx*MM+yy]=dist[xn*MM+yn]+1
                q.append((xx*MM+yy))
        
        
        
        
    # pprint(maze)
    # for j in dist:print(*j)
    ans=dist[x2*MM+y2]
    if ans==INF:
        ans=-1
    print(ans)  
    
main()

Fixed submission:
# coding: utf-8
# Your code here!

from collections import deque
from pprint import pprint
moves=[(0,1),(1,0),(0,-1),(-1,0)]
pp=pprint
# INF=float("inf")

h,w,k=map(int,input().split())
x1,y1,x2,y2=map(lambda x:int(x)-1,input().split())
maze=[list(input()) for _ in range(h)]
cost=[[-1]*w for _ in range(h)]

todo=deque()
todo.append((x1,y1))
cost[x1][y1]=0

while todo:
    x,y=todo.popleft()
    for vx,vy in moves:
        for kk in range(1,k+1):
            nx=x+vx*kk
            ny=y+vy*kk
            if not(0<=nx<h) or not(0<=ny<w):
                continue
            if maze[nx][ny]=="@":
                break
            if cost[nx][ny]!=-1 and cost[nx][ny]<=cost[x][y]:
                break
            if cost[nx][ny]!=-1:
                continue
            cost[nx][ny]=cost[x][y]+1
            todo.append((nx,ny))


# pp(cost)
ans=cost[x2][y2]
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
def LI():return list(map(int,input().split()))

INF=float("inf")
# from pprint import*
from collections import deque

def main():
    h,w,k=LI()
    x1,y1,x2,y2=LI()
    x1-=1
    y1-=1
    x2-=1
    y2-=1
    maze=[list(input()) for _ in range(h)]
    dist=[[INF]*(w) for _ in range(h)]
    
    q=deque([])
    
    q.append((x1,y1))
    dist[x1][y1]=0
    while q:
        (xn,yn)=q.popleft()
        flg=True
        for kk in range(1,k+1):
            for i,j in [(0,-kk)]:
                if not flg:continue
                xx=xn+i
                yy=yn+j
                if (xx<0 or h<=xx) or (yy<0 or w<=yy):continue
                if maze[xx][yy]=="@":flg=False;break
                if dist[xx][yy]!=INF:continue
                dist[xx][yy]=dist[xn][yn]+1
                q.append((xx,yy))
        flg=True
        for kk in range(1,k+1):
            for i,j in [(0,+kk)]:
                if not flg:continue
                xx=xn+i
                yy=yn+j
                if (xx<0 or h<=xx) or (yy<0 or w<=yy):continue
                if maze[xx][yy]=="@":flg=False;break
                if dist[xx][yy]!=INF:continue
                dist[xx][yy]=dist[xn][yn]+1
                q.append((xx,yy))
        flg=True
        for kk in range(1,k+1):
            for i,j in [(kk,0)]:
                if not flg:continue
                xx=xn+i
                yy=yn+j
                if (xx<0 or h<=xx) or (yy<0 or w<=yy):continue
                if maze[xx][yy]=="@":flg=False;break
                if dist[xx][yy]!=INF:continue
                dist[xx][yy]=dist[xn][yn]+1
                q.append((xx,yy))
        flg=True
        for kk in range(1,k+1):
            for i,j in [(-kk,0)]:
                if not flg:continue
                xx=xn+i
                yy=yn+j
                if (xx<0 or h<=xx) or (yy<0 or w<=yy):continue
                if maze[xx][yy]=="@":flg=False;break
                if dist[xx][yy]!=INF:continue
                dist[xx][yy]=dist[xn][yn]+1
                q.append((xx,yy))
        
        
        
        
    # pprint(maze)
    # for j in dist:print(*j)
    ans=dist[x2][y2]
    if ans==INF:
        ans=-1
    print(ans)  
    
main()

Fixed submission:
# coding: utf-8
# Your code here!

from collections import deque
from pprint import pprint
moves=[(0,1),(1,0),(0,-1),(-1,0)]
pp=pprint
# INF=float("inf")

h,w,k=map(int,input().split())
x1,y1,x2,y2=map(lambda x:int(x)-1,input().split())
maze=[list(input()) for _ in range(h)]
cost=[[-1]*w for _ in range(h)]

todo=deque()
todo.append((x1,y1))
cost[x1][y1]=0

while todo:
    x,y=todo.popleft()
    for vx,vy in moves:
        for kk in range(1,k+1):
            nx=x+vx*kk
            ny=y+vy*kk
            if not(0<=nx<h) or not(0<=ny<w):
                continue
            if maze[nx][ny]=="@":
                break
            if cost[nx][ny]!=-1 and cost[nx][ny]<=cost[x][y]:
                break
            if cost[nx][ny]!=-1:
                continue
            cost[nx][ny]=cost[x][y]+1
            todo.append((nx,ny))


# pp(cost)
ans=cost[x2][y2]
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
def LI():return list(map(int,input().split()))
def MI():return map(int,open(0).read().split())
def I():return int(input())
INF=float("inf")
# from pprint import*
from collections import deque
h,w,k=LI()
x1,y1,x2,y2=LI()
x1-=1
y1-=1
x2-=1
y2-=1
# line=[list("#"*(w+2))]
# maze=line+
maze=[list(input()) for _ in range(h)]
# +line
# pprint(maze)
dist=[[INF]*(w) for _ in range(h)]

q=deque([])


# exit()
q.append((x1,y1))
dist[x1][y1]=0
while q:
    (xn,yn)=q.popleft()
    # print(xn)
    # exit()
    flg=True
    for kk in range(1,k+1):
        for i,j in [(0,-kk)]:
            if not flg:continue
            xx=xn+i
            yy=yn+j
            if (xx<0 or h<=xx) or (yy<0 or w<=yy):continue
            if maze[xx][yy]=="@":flg=False;break
            if dist[xx][yy]!=INF:continue
            dist[xx][yy]=dist[xn][yn]+1
            q.append((xx,yy))
    flg=True
    for kk in range(1,k+1):
        for i,j in [(0,+kk)]:
            if not flg:continue
            xx=xn+i
            yy=yn+j
            if (xx<0 or h<=xx) or (yy<0 or w<=yy):continue
            if maze[xx][yy]=="@":flg=False;break
            if dist[xx][yy]!=INF:continue
            dist[xx][yy]=dist[xn][yn]+1
            q.append((xx,yy))
    flg=True
    for kk in range(1,k+1):
        for i,j in [(kk,0)]:
            if not flg:continue
            xx=xn+i
            yy=yn+j
            if (xx<0 or h<=xx) or (yy<0 or w<=yy):continue
            if maze[xx][yy]=="@":flg=False;break
            if dist[xx][yy]!=INF:continue
            dist[xx][yy]=dist[xn][yn]+1
            q.append((xx,yy))
    flg=True
    for kk in range(1,k+1):
        for i,j in [(-kk,0)]:
            if not flg:continue
            xx=xn+i
            yy=yn+j
            if (xx<0 or h<=xx) or (yy<0 or w<=yy):continue
            if maze[xx][yy]=="@":flg=False;break
            if dist[xx][yy]!=INF:continue
            dist[xx][yy]=dist[xn][yn]+1
            q.append((xx,yy))
    
    
    
    
# pprint(maze)
# for j in dist:print(*j)
ans=dist[x2][y2]
if ans==INF:
    ans=-1
print(ans)

Fixed submission:
# coding: utf-8
# Your code here!

from collections import deque
from pprint import pprint
moves=[(0,1),(1,0),(0,-1),(-1,0)]
pp=pprint
# INF=float("inf")

h,w,k=map(int,input().split())
x1,y1,x2,y2=map(lambda x:int(x)-1,input().split())
maze=[list(input()) for _ in range(h)]
cost=[[-1]*w for _ in range(h)]

todo=deque()
todo.append((x1,y1))
cost[x1][y1]=0

while todo:
    x,y=todo.popleft()
    for vx,vy in moves:
        for kk in range(1,k+1):
            nx=x+vx*kk
            ny=y+vy*kk
            if not(0<=nx<h) or not(0<=ny<w):
                continue
            if maze[nx][ny]=="@":
                break
            if cost[nx][ny]!=-1 and cost[nx][ny]<=cost[x][y]:
                break
            if cost[nx][ny]!=-1:
                continue
            cost[nx][ny]=cost[x][y]+1
            todo.append((nx,ny))


# pp(cost)
ans=cost[x2][y2]
print(ans)