Buggy python submission (verdict: Time Limit Exceeded):
## coding: UTF-8


H,W,K = map(int,input().split())
x1, y1, x2, y2 = map(int,input().split())
x1-=1
y1-=1
x2-=1
y2-=1
#1-index to 0-index


c = []
for i in range(H):
    c.append(input())

fresh = [True] * (H*W)
count = [-1] * (H*W)

queue = [x1*W+y1] #now <- start
count[x1*W+y1] = 0 #start -> step = 0
fresh[x1*W+y1] = False
n = [] #next
for i in range(H):
    for j in range(W):
        if(c[i][j] == '@'):
            fresh[i*W+j] = False

#print(fresh)
#print(count)
#print(queue)

step = 1
status = True
while status:
    if(len(queue) > 0):
        pivot = queue.pop()
        #右へ移動
        #if(pivot%W != W-1):#and fresh[pivot+1]):
        for g in range(1, K+1):
            if((pivot+g)%W != 0 and fresh[pivot+g]):
                n.append(pivot+g)
                count[pivot+g] = step
                #fresh[pivot+g] = False false後回し
            else:
                break
        #左へ移動
        #if(pivot%W != 0): #and fresh[pivot-1]):
        for g in range(1, K+1):
            if((pivot-g)%W != W-1 and fresh[pivot-g]):
                n.append(pivot-g)
                count[pivot-g] = step
                #fresh[pivot-g] = False false後回し 
            else:
                break   
        #下へ移動
        #if( int(pivot/W)<(H-1) and fresh[pivot+W]):
        for g in range(1, K+1):
            if( (pivot+g*W)//W < H and fresh[pivot+g*W]):
                n.append(pivot+g*W)
                count[pivot+g*W] = step
                #fresh[pivot+g * W] = False false後回し
            else:
                break    
        #上へ移動
        #if( int(pivot/W)>0 and fresh[pivot-W] ):
        for g in range(1, K+1):
            if( (pivot-g*W)>= 0 and fresh[pivot-g*W] ):
                n.append(pivot-g*W)
                count[pivot-g*W] = step
                #fresh[pivot-g*W] = False
            else:
                break
    elif(len(n)>0):
        for e in n:
            fresh[e] = False
        queue = n
        n = []
        #print('step{}end, queue:{}, n:{}, fresh:{}, count:{}'.format(step, queue, n, fresh, count))
        step += 1
    else:
        status = False            


print(count[x2*W+y2])

Fixed submission:
## coding: UTF-8


H,W,K = map(int,input().split())
x1, y1, x2, y2 = map(int,input().split())
x1-=1
y1-=1
x2-=1
y2-=1
#1-index to 0-index


c = []
for i in range(H):
    c.append(input())

fresh = [True] * (H*W)
count = [-1] * (H*W)

queue = [x1*W+y1] #now <- start
count[x1*W+y1] = 0 #start -> step = 0
fresh[x1*W+y1] = False
n = [] #next
for i in range(H):
    for j in range(W):
        if(c[i][j] == '@'):
            fresh[i*W+j] = False

#print(fresh)
#print(count)
#print(queue)

step = 1
status = True
while status:
    if(len(queue) > 0):
        pivot = queue.pop()
        #右へ移動
        #if(pivot%W != W-1):#and fresh[pivot+1]):
        for g in range(1, K+1):
            if((pivot+g)%W != 0 and fresh[pivot+g]):
                n.append(pivot+g)
                count[pivot+g] = step
                #fresh[pivot+g] = False false後回し
            else:
                break
        #左へ移動
        #if(pivot%W != 0): #and fresh[pivot-1]):
        for g in range(1, K+1):
            if((pivot-g)%W != W-1 and fresh[pivot-g]):
                n.append(pivot-g)
                count[pivot-g] = step
                #fresh[pivot-g] = False false後回し 
            else:
                break   
        #下へ移動
        #if( int(pivot/W)<(H-1) and fresh[pivot+W]):
        for g in range(1, K+1):
            if( (pivot+g*W)//W < H and fresh[pivot+g*W]):
                n.append(pivot+g*W)
                count[pivot+g*W] = step
                #fresh[pivot+g * W] = False false後回し
            else:
                break    
        #上へ移動
        #if( int(pivot/W)>0 and fresh[pivot-W] ):
        for g in range(1, K+1):
            if( (pivot-g*W)>= 0 and fresh[pivot-g*W] ):
                n.append(pivot-g*W)
                count[pivot-g*W] = step
                #fresh[pivot-g*W] = False
            else:
                break
    elif(len(n)>0): 
        n = sorted(n)
        last = -1
        l = []
        for i in range(len(n)):
            if last != n[i]:
                l.append(n[i])
                last = n[i]
        for e in l:
            fresh[e] = False
        queue = l
        n = []
        #print('step{}end, queue:{}, n:{}, fresh:{}, count:{}'.format(step, queue, n, fresh, count))
        step += 1
    else:
        status = False            


print(count[x2*W+y2])

Buggy python submission (verdict: Time Limit Exceeded):
## coding: UTF-8


H,W,K = map(int,input().split())
x1, y1, x2, y2 = map(int,input().split())
x1-=1
y1-=1
x2-=1
y2-=1
#1-index to 0-index


c = []
for i in range(H):
    c.append(input())

fresh = [True] * (H*W)
count = [-1] * (H*W)

queue = [x1*W+y1] #now <- start
count[x1*W+y1] = 0 #start -> step = 0
fresh[x1*W+y1] = False
n = [] #next
for i in range(H):
    for j in range(W):
        if(c[i][j] == '@'):
            fresh[i*W+j] = False

#print(fresh)
#print(count)
#print(queue)

step = 1
status = True
while status:
    if(len(queue) > 0):
        pivot = queue.pop()
        #右へ移動
        #if(pivot%W != W-1):#and fresh[pivot+1]):
        for g in range(1, K+1):
            if((pivot+g)%W != 0 and fresh[pivot+g]):
                n.append(pivot+g)
                count[pivot+g] = step
                #fresh[pivot+g] = False false後回し
            else:
                break
        #左へ移動
        #if(pivot%W != 0): #and fresh[pivot-1]):
        for g in range(1, K+1):
            if((pivot-g)%W != W-1 and fresh[pivot-g]):
                n.append(pivot-g)
                count[pivot-g] = step
                #fresh[pivot-g] = False false後回し 
            else:
                break   
        #下へ移動
        #if( int(pivot/W)<(H-1) and fresh[pivot+W]):
        for g in range(1, K+1):
            if( (pivot+g*W)//W < H and fresh[pivot+g*W]):
                n.append(pivot+g*W)
                count[pivot+g*W] = step
                #fresh[pivot+g * W] = False false後回し
            else:
                break    
        #上へ移動
        #if( int(pivot/W)>0 and fresh[pivot-W] ):
        for g in range(1, K+1):
            if( (pivot-g*W)>= 0 and fresh[pivot-g*W] ):
                n.append(pivot-g*W)
                count[pivot-g*W] = step
                #fresh[pivot-g*W] = False
            else:
                break
    elif(len(n)>0):
        for e in n:
            fresh[e] = False
        queue = n
        n = []
        #print('step{}end, queue:{}, n:{}, fresh:{}, count:{}'.format(step, queue, n, fresh, count))
        step += 1
    else:
        status = False            


print(count[x2*W+y2])

Fixed submission:
## coding: UTF-8


H,W,K = map(int,input().split())
x1, y1, x2, y2 = map(int,input().split())
x1-=1
y1-=1
x2-=1
y2-=1
#1-index to 0-index


c = []
for i in range(H):
    c.append(input())

fresh = [True] * (H*W)
count = [-1] * (H*W)

queue = [x1*W+y1] #now <- start
count[x1*W+y1] = 0 #start -> step = 0
fresh[x1*W+y1] = False
n = [] #next
for i in range(H):
    for j in range(W):
        if(c[i][j] == '@'):
            fresh[i*W+j] = False

#print(fresh)
#print(count)
#print(queue)

step = 1
status = True
while status:
    if(len(queue) > 0):
        pivot = queue.pop()
        #右へ移動
        #if(pivot%W != W-1):#and fresh[pivot+1]):
        for g in range(1, K+1):
            if((pivot+g)%W != 0 and fresh[pivot+g]):
                n.append(pivot+g)
                count[pivot+g] = step
                #fresh[pivot+g] = False false後回し
            else:
                break
        #左へ移動
        #if(pivot%W != 0): #and fresh[pivot-1]):
        for g in range(1, K+1):
            if((pivot-g)%W != W-1 and fresh[pivot-g]):
                n.append(pivot-g)
                count[pivot-g] = step
                #fresh[pivot-g] = False false後回し 
            else:
                break   
        #下へ移動
        #if( int(pivot/W)<(H-1) and fresh[pivot+W]):
        for g in range(1, K+1):
            if( (pivot+g*W)//W < H and fresh[pivot+g*W]):
                n.append(pivot+g*W)
                count[pivot+g*W] = step
                #fresh[pivot+g * W] = False false後回し
            else:
                break    
        #上へ移動
        #if( int(pivot/W)>0 and fresh[pivot-W] ):
        for g in range(1, K+1):
            if( (pivot-g*W)>= 0 and fresh[pivot-g*W] ):
                n.append(pivot-g*W)
                count[pivot-g*W] = step
                #fresh[pivot-g*W] = False
            else:
                break
    elif(len(n)>0): 
        n = sorted(n)
        last = -1
        l = []
        for i in range(len(n)):
            if last != n[i]:
                l.append(n[i])
                last = n[i]
        for e in l:
            fresh[e] = False
        queue = l
        n = []
        #print('step{}end, queue:{}, n:{}, fresh:{}, count:{}'.format(step, queue, n, fresh, count))
        step += 1
    else:
        status = False            


print(count[x2*W+y2])