Buggy python submission (verdict: Time Limit Exceeded):
H,W,K=map(int,input().split())
x1,y1,x2,y2=map(int,input().split())
C=[]
nx=[]

for _ in range(H):
    C.append(list(input()))

def keiro(x,y,k):
    i=1
    while True:
        if y+i>W-1 or C[x][y+i]!='.' or i>k:
            break
        else:
            nx.append([x,y+i])
            i+=1
    
    i=1
    while True:
        if y-i<0 or C[x][y-i]!='.' or i>k:
            break
        else:
            nx.append([x,y-i])
            i+=1
    
    i=1
    while True:
        if x+i>H-1 or C[x+i][y]!='.' or i>k:
            break
        else:
            nx.append([x+i,y])
            i+=1
    
    i=1
    while True:
        if x-i<0 or C[x-i][y]!='.' or i>k:
            break
        else:
            nx.append([x-i,y])
            i+=1

    return 0

C[x1-1][y1-1]=str(0)
keiro(x1-1,y1-1,K)
taisho=nx.copy()
result=1
for j in nx:
    C[j[0]][j[1]]=result

while C[x2-1][y2-1]=='.':
    nx=[]
    if len(taisho)==0 or result>10**6:
        result=-1
        break
    for l in taisho:
        keiro(l[0],l[1],K)
    result+=1
    for j in nx:
        C[j[0]][j[1]]=result
    taisho=nx.copy()

print(result)


Fixed submission:
H,W,K=map(int,input().split())
x1,y1,x2,y2=map(int,input().split())
C=[]
nx=set([])

for _ in range(H):
    C.append(list(input()))

def keiro(x,y,k):
    i=1
    while True:
        if y+i>W-1 or C[x][y+i]!='.' or i>k:
            break
        else:
            if (x,y+i) not in nx:
                nx.add((x,y+i))
            i+=1
    
    i=1
    while True:
        if y-i<0 or C[x][y-i]!='.' or i>k:
            break
        else:
            if (x,y-i) not in nx:
                nx.add((x,y-i))
            i+=1
    
    i=1
    while True:
        if x+i>H-1 or C[x+i][y]!='.' or i>k:
            break
        else:
            if (x+i,y) not in nx:
                nx.add((x+i,y))
            i+=1
    
    i=1
    while True:
        if x-i<0 or C[x-i][y]!='.' or i>k:
            break
        else:
            if (x-i,y) not in nx:
                nx.add((x-i,y))
            i+=1

    return 0

C[x1-1][y1-1]=0
keiro(x1-1,y1-1,K)
taisho=nx.copy()
result=1
for j in nx:
    C[j[0]][j[1]]=result

while C[x2-1][y2-1]=='.':
    nx=set([])
    if len(taisho)==0 or result>10**6:
        result=-1
        break
    for l in taisho:
        keiro(l[0],l[1],K)
    result+=1
    for j in nx:
        C[j[0]][j[1]]=result
    taisho=nx.copy()

print(result)

Buggy python submission (verdict: Time Limit Exceeded):
H,W,K=map(int,input().split())
x1,y1,x2,y2=map(int,input().split())
C=[]
nx=[]

for _ in range(H):
    C.append(list(input()))

def keiro(x,y,k):
    i=1
    while True:
        if y+i>W-1 or C[x][y+i]!='.' or i>k:
            break
        else:
            nx.append([x,y+i])
            i+=1
    
    i=1
    while True:
        if y-i<0 or C[x][y-i]!='.' or i>k:
            break
        else:
            nx.append([x,y-i])
            i+=1
    
    i=1
    while True:
        if x+i>H-1 or C[x+i][y]!='.' or i>k:
            break
        else:
            nx.append([x+i,y])
            i+=1
    
    i=1
    while True:
        if x-i<0 or C[x-i][y]!='.' or i>k:
            break
        else:
            nx.append([x-i,y])
            i+=1

    return 0

C[x1-1][y1-1]=str(0)
keiro(x1-1,y1-1,K)
taisho=nx.copy()
result=1
for j in nx:
    C[j[0]][j[1]]=result

while C[x2-1][y2-1]=='.':
    nx=[]
    if len(taisho)==0:
        result=-1
        break
    for l in taisho:
        keiro(l[0],l[1],K)
    result+=1
    for j in nx:
        C[j[0]][j[1]]=result
    taisho=nx.copy()

print(result)


Fixed submission:
H,W,K=map(int,input().split())
x1,y1,x2,y2=map(int,input().split())
C=[]
nx=set([])

for _ in range(H):
    C.append(list(input()))

def keiro(x,y,k):
    i=1
    while True:
        if y+i>W-1 or C[x][y+i]!='.' or i>k:
            break
        else:
            if (x,y+i) not in nx:
                nx.add((x,y+i))
            i+=1
    
    i=1
    while True:
        if y-i<0 or C[x][y-i]!='.' or i>k:
            break
        else:
            if (x,y-i) not in nx:
                nx.add((x,y-i))
            i+=1
    
    i=1
    while True:
        if x+i>H-1 or C[x+i][y]!='.' or i>k:
            break
        else:
            if (x+i,y) not in nx:
                nx.add((x+i,y))
            i+=1
    
    i=1
    while True:
        if x-i<0 or C[x-i][y]!='.' or i>k:
            break
        else:
            if (x-i,y) not in nx:
                nx.add((x-i,y))
            i+=1

    return 0

C[x1-1][y1-1]=0
keiro(x1-1,y1-1,K)
taisho=nx.copy()
result=1
for j in nx:
    C[j[0]][j[1]]=result

while C[x2-1][y2-1]=='.':
    nx=set([])
    if len(taisho)==0 or result>10**6:
        result=-1
        break
    for l in taisho:
        keiro(l[0],l[1],K)
    result+=1
    for j in nx:
        C[j[0]][j[1]]=result
    taisho=nx.copy()

print(result)

Buggy python submission (verdict: Time Limit Exceeded):
H,W,K=map(int,input().split())
x1,y1,x2,y2=map(int,input().split())
C=[]

for _ in range(H):
    C.append(list(input()))

def keiro(x,y,cn,k):
    nx=[]

    i=1
    while y+i<=W-1 and i<=k:
        A=C[x][y+i]
        if A=='.' or A==str(cn+1):
            C[x][y+i]=str(cn+1)
            nx.append([x,y+i])
            i+=1
        else:
            break
    
    i=1
    while y-i>=0 and i<=k:
        A=C[x][y-i]
        if A=='.' or A==str(cn+1):
            C[x][y-i]=str(cn+1)
            nx.append([x,y-i])
            i+=1
        else:
            break
    
    i=1
    while x+i<=H-1 and i<=k:
        A=C[x+i][y]
        if A=='.' or A==str(cn+1):
            C[x+i][y]=str(cn+1)
            nx.append([x+i,y])
            i+=1
        else:
            break
    
    i=1
    while x-i>=0 and i<=k:
        A=C[x-i][y]
        if A=='.' or A==str(cn+1):
            C[x-i][y]=str(cn+1)
            nx.append([x-i,y])
            i+=1
        else:
            break

    return nx

C[x1-1][y1-1]=str(0)
taisho=keiro(x1-1,y1-1,0,K)
result=1

while C[x2-1][y2-1]=='.':
    next_taisho=[]
    if len(taisho)==0:
        result=-1
        break
    for l in taisho:
        m=keiro(l[0],l[1],result,K)

        for n in m:
            next_taisho.append(n)
    result+=1
    taisho=next_taisho.copy()

print(result)

Fixed submission:
H,W,K=map(int,input().split())
x1,y1,x2,y2=map(int,input().split())
C=[]
nx=set([])

for _ in range(H):
    C.append(list(input()))

def keiro(x,y,k):
    i=1
    while True:
        if y+i>W-1 or C[x][y+i]!='.' or i>k:
            break
        else:
            if (x,y+i) not in nx:
                nx.add((x,y+i))
            i+=1
    
    i=1
    while True:
        if y-i<0 or C[x][y-i]!='.' or i>k:
            break
        else:
            if (x,y-i) not in nx:
                nx.add((x,y-i))
            i+=1
    
    i=1
    while True:
        if x+i>H-1 or C[x+i][y]!='.' or i>k:
            break
        else:
            if (x+i,y) not in nx:
                nx.add((x+i,y))
            i+=1
    
    i=1
    while True:
        if x-i<0 or C[x-i][y]!='.' or i>k:
            break
        else:
            if (x-i,y) not in nx:
                nx.add((x-i,y))
            i+=1

    return 0

C[x1-1][y1-1]=0
keiro(x1-1,y1-1,K)
taisho=nx.copy()
result=1
for j in nx:
    C[j[0]][j[1]]=result

while C[x2-1][y2-1]=='.':
    nx=set([])
    if len(taisho)==0 or result>10**6:
        result=-1
        break
    for l in taisho:
        keiro(l[0],l[1],K)
    result+=1
    for j in nx:
        C[j[0]][j[1]]=result
    taisho=nx.copy()

print(result)

Buggy python submission (verdict: Time Limit Exceeded):
H,W,K=map(int,input().split())
x1,y1,x2,y2=map(int,input().split())
C=[]

for _ in range(H):
    C.append(list(input()))

def keiro(x,y,cn,k):
    nx=[]

    i=1
    while True:
        if y+i>W-1 or i>k:
            break
        elif C[x][y+i]=='.' or C[x][y+i]==str(cn+1):
            C[x][y+i]=str(cn+1)
            nx.append([x,y+i])
            i+=1
        else:
            break
    
    i=1
    while True:
        if y-i<0 or i>k:
            break
        elif C[x][y-i]=='.' or C[x][y-i]==str(cn+1):
            C[x][y-i]=str(cn+1)
            nx.append([x,y-i])
            i+=1
        else:
            break
    
    i=1
    while True:
        if x+i>H-1 or i>k:
            break
        elif C[x+i][y]=='.' or C[x+i][y]==str(cn+1):
            C[x+i][y]=str(cn+1)
            nx.append([x+i,y])
            i+=1
        else:
            break
    
    i=1
    while True:
        if x-i<0 or i>k:
            break
        elif C[x-i][y]=='.' or C[x-i][y]==str(cn+1):
            C[x-i][y]=str(cn+1)
            nx.append([x-i,y])
            i+=1
        else:
            break

    return nx

C[x1-1][y1-1]=str(0)
taisho=keiro(x1-1,y1-1,0,K)
result=1

while C[x2-1][y2-1]=='.':
    next_taisho=[]
    if len(taisho)==0:
        result=-1
        break
    for l in taisho:
        m=keiro(l[0],l[1],result,K)

        for n in m:
            next_taisho.append(n)
    result+=1
    taisho=next_taisho.copy()

print(result)

Fixed submission:
H,W,K=map(int,input().split())
x1,y1,x2,y2=map(int,input().split())
C=[]
nx=set([])

for _ in range(H):
    C.append(list(input()))

def keiro(x,y,k):
    i=1
    while True:
        if y+i>W-1 or C[x][y+i]!='.' or i>k:
            break
        else:
            if (x,y+i) not in nx:
                nx.add((x,y+i))
            i+=1
    
    i=1
    while True:
        if y-i<0 or C[x][y-i]!='.' or i>k:
            break
        else:
            if (x,y-i) not in nx:
                nx.add((x,y-i))
            i+=1
    
    i=1
    while True:
        if x+i>H-1 or C[x+i][y]!='.' or i>k:
            break
        else:
            if (x+i,y) not in nx:
                nx.add((x+i,y))
            i+=1
    
    i=1
    while True:
        if x-i<0 or C[x-i][y]!='.' or i>k:
            break
        else:
            if (x-i,y) not in nx:
                nx.add((x-i,y))
            i+=1

    return 0

C[x1-1][y1-1]=0
keiro(x1-1,y1-1,K)
taisho=nx.copy()
result=1
for j in nx:
    C[j[0]][j[1]]=result

while C[x2-1][y2-1]=='.':
    nx=set([])
    if len(taisho)==0 or result>10**6:
        result=-1
        break
    for l in taisho:
        keiro(l[0],l[1],K)
    result+=1
    for j in nx:
        C[j[0]][j[1]]=result
    taisho=nx.copy()

print(result)

Buggy python submission (verdict: Time Limit Exceeded):
H,W,K=map(int,input().split())
x1,y1,x2,y2=map(int,input().split())
C=[]

for _ in range(H):
    C.append(list(input()))

def keiro(x,y,cn,k):
    nx=[]

    i=1
    while True:
        if y+i>W-1 or i>k:
            break
        elif C[x][y+i]=='.' or C[x][y+i]==str(cn+1):
            C[x][y+i]=str(cn+1)
            nx.append([x,y+i])
            i+=1
        else:
            break
    
    i=1
    while True:
        if y-i<0 or i>k:
            break
        elif C[x][y-i]=='.' or C[x][y-i]==str(cn+1):
            C[x][y-i]=str(cn+1)
            nx.append([x,y-i])
            i+=1
        else:
            break
    
    i=1
    while True:
        if x+i>H-1 or i>k:
            break
        elif C[x+i][y]=='.' or C[x+i][y]==str(cn+1):
            C[x+i][y]=str(cn+1)
            nx.append([x+i,y])
            i+=1
        else:
            break
    
    i=1
    while True:
        if x-i<0 or i>k:
            break
        elif C[x-i][y]=='.' or C[x-i][y]==str(cn+1):
            C[x-i][y]=str(cn+1)
            nx.append([x-i,y])
            i+=1
        else:
            break

    return nx

C[x1-1][y1-1]=str(0)
taisho=keiro(x1-1,y1-1,0,K)
result=1

while C[x2-1][y2-1]=='.':
    next_taisho=[]
    if len(taisho)==0:
        result=-1
        break
    for l in taisho:
        m=keiro(l[0],l[1],result,K)

        for n in m:
            next_taisho.append(n)
    result+=1
    taisho=next_taisho.copy()

print(result)

Fixed submission:
H,W,K=map(int,input().split())
x1,y1,x2,y2=map(int,input().split())
C=[]
nx=set([])

for _ in range(H):
    C.append(list(input()))

def keiro(x,y,k):
    i=1
    while True:
        if y+i>W-1 or C[x][y+i]!='.' or i>k:
            break
        else:
            if (x,y+i) not in nx:
                nx.add((x,y+i))
            i+=1
    
    i=1
    while True:
        if y-i<0 or C[x][y-i]!='.' or i>k:
            break
        else:
            if (x,y-i) not in nx:
                nx.add((x,y-i))
            i+=1
    
    i=1
    while True:
        if x+i>H-1 or C[x+i][y]!='.' or i>k:
            break
        else:
            if (x+i,y) not in nx:
                nx.add((x+i,y))
            i+=1
    
    i=1
    while True:
        if x-i<0 or C[x-i][y]!='.' or i>k:
            break
        else:
            if (x-i,y) not in nx:
                nx.add((x-i,y))
            i+=1

    return 0

C[x1-1][y1-1]=0
keiro(x1-1,y1-1,K)
taisho=nx.copy()
result=1
for j in nx:
    C[j[0]][j[1]]=result

while C[x2-1][y2-1]=='.':
    nx=set([])
    if len(taisho)==0 or result>10**6:
        result=-1
        break
    for l in taisho:
        keiro(l[0],l[1],K)
    result+=1
    for j in nx:
        C[j[0]][j[1]]=result
    taisho=nx.copy()

print(result)

Buggy python submission (verdict: Time Limit Exceeded):
H,W,K=map(int,input().split())
x1,y1,x2,y2=map(int,input().split())
C=[]

for _ in range(H):
    C.append(list(input()))

def keiro(x,y,cn,k):
    nx=[]

    i=1
    while True:
        if y+i>W-1 or C[x][y+i]=='@' or i>k:
            break
        elif C[x][y+i]=='.' or int(C[x][y+i])==cn+1:
            C[x][y+i]=str(cn+1)
            nx.append([x,y+i])
            i+=1
        else:
            break
    
    i=1
    while True:
        if y-i<0 or C[x][y-i]=='@' or i>k:
            break
        elif C[x][y-i]=='.' or int(C[x][y-i])==cn+1:
            C[x][y-i]=str(cn+1)
            nx.append([x,y-i])
            i+=1
        else:
            break
    
    i=1
    while True:
        if x+i>H-1 or C[x+i][y]=='@' or i>k:
            break
        elif C[x+i][y]=='.' or int(C[x+i][y])==cn+1:
            C[x+i][y]=str(cn+1)
            nx.append([x+i,y])
            i+=1
        else:
            break
    
    i=1
    while True:
        if x-i<0 or C[x-i][y]=='@' or i>k:
            break
        elif C[x-i][y]=='.' or int(C[x-i][y])==cn+1:
            C[x-i][y]=str(cn+1)
            nx.append([x-i,y])
            i+=1
        else:
            break

    return nx

C[x1-1][y1-1]=str(0)
taisho=keiro(x1-1,y1-1,0,K)
result=1

while C[x2-1][y2-1]=='.':
    next_taisho=[]
    if len(taisho)==0:
        result=-1
        break
    for l in taisho:
        m=keiro(l[0],l[1],result,K)

        for n in m:
            next_taisho.append(n)
    result+=1
    taisho=next_taisho.copy()

print(result)

Fixed submission:
H,W,K=map(int,input().split())
x1,y1,x2,y2=map(int,input().split())
C=[]
nx=set([])

for _ in range(H):
    C.append(list(input()))

def keiro(x,y,k):
    i=1
    while True:
        if y+i>W-1 or C[x][y+i]!='.' or i>k:
            break
        else:
            if (x,y+i) not in nx:
                nx.add((x,y+i))
            i+=1
    
    i=1
    while True:
        if y-i<0 or C[x][y-i]!='.' or i>k:
            break
        else:
            if (x,y-i) not in nx:
                nx.add((x,y-i))
            i+=1
    
    i=1
    while True:
        if x+i>H-1 or C[x+i][y]!='.' or i>k:
            break
        else:
            if (x+i,y) not in nx:
                nx.add((x+i,y))
            i+=1
    
    i=1
    while True:
        if x-i<0 or C[x-i][y]!='.' or i>k:
            break
        else:
            if (x-i,y) not in nx:
                nx.add((x-i,y))
            i+=1

    return 0

C[x1-1][y1-1]=0
keiro(x1-1,y1-1,K)
taisho=nx.copy()
result=1
for j in nx:
    C[j[0]][j[1]]=result

while C[x2-1][y2-1]=='.':
    nx=set([])
    if len(taisho)==0 or result>10**6:
        result=-1
        break
    for l in taisho:
        keiro(l[0],l[1],K)
    result+=1
    for j in nx:
        C[j[0]][j[1]]=result
    taisho=nx.copy()

print(result)

Buggy python submission (verdict: Time Limit Exceeded):
H,W,K=map(int,input().split())
x1,y1,x2,y2=map(int,input().split())
C=[]

for _ in range(H):
    C.append(list(input()))

def keiro(x,y,cn,k):
    nx=[]

    i=1
    while True:
        if y+i>W-1 or C[x][y+i]=='@' or i>k:
            break
        elif C[x][y+i]=='.' or int(C[x][y+i])==cn+1:
            C[x][y+i]=str(cn+1)
            nx.append([x,y+i])
            i+=1
        else:
            break
    
    i=1
    while True:
        if y-i<0 or C[x][y-i]=='@' or i>k:
            break
        elif C[x][y-i]=='.' or int(C[x][y-i])==cn+1:
            C[x][y-i]=str(cn+1)
            nx.append([x,y-i])
            i+=1
        else:
            break
    
    i=1
    while True:
        if x+i>H-1 or C[x+i][y]=='@' or i>k:
            break
        elif C[x+i][y]=='.' or int(C[x+i][y])==cn+1:
            C[x+i][y]=str(cn+1)
            nx.append([x+i,y])
            i+=1
        else:
            break
    
    i=1
    while True:
        if x-i<0 or C[x-i][y]=='@' or i>k:
            break
        elif C[x-i][y]=='.' or int(C[x-i][y])==cn+1:
            C[x-i][y]=str(cn+1)
            nx.append([x-i,y])
            i+=1
        else:
            break

    return nx

C[x1-1][y1-1]=str(0)
taisho=keiro(x1-1,y1-1,0,K)
result=1

while C[x2-1][y2-1]=='.':
    next_taisho=[]
    if len(taisho)==0:
        result=-1
        break
    for l in taisho:
        m=keiro(l[0],l[1],result,K)

        for n in m:
            next_taisho.append(n)
    result+=1
    taisho=next_taisho.copy()

print(result)

Fixed submission:
H,W,K=map(int,input().split())
x1,y1,x2,y2=map(int,input().split())
C=[]
nx=set([])

for _ in range(H):
    C.append(list(input()))

def keiro(x,y,k):
    i=1
    while True:
        if y+i>W-1 or C[x][y+i]!='.' or i>k:
            break
        else:
            if (x,y+i) not in nx:
                nx.add((x,y+i))
            i+=1
    
    i=1
    while True:
        if y-i<0 or C[x][y-i]!='.' or i>k:
            break
        else:
            if (x,y-i) not in nx:
                nx.add((x,y-i))
            i+=1
    
    i=1
    while True:
        if x+i>H-1 or C[x+i][y]!='.' or i>k:
            break
        else:
            if (x+i,y) not in nx:
                nx.add((x+i,y))
            i+=1
    
    i=1
    while True:
        if x-i<0 or C[x-i][y]!='.' or i>k:
            break
        else:
            if (x-i,y) not in nx:
                nx.add((x-i,y))
            i+=1

    return 0

C[x1-1][y1-1]=0
keiro(x1-1,y1-1,K)
taisho=nx.copy()
result=1
for j in nx:
    C[j[0]][j[1]]=result

while C[x2-1][y2-1]=='.':
    nx=set([])
    if len(taisho)==0 or result>10**6:
        result=-1
        break
    for l in taisho:
        keiro(l[0],l[1],K)
    result+=1
    for j in nx:
        C[j[0]][j[1]]=result
    taisho=nx.copy()

print(result)