class Dijkstra_Heap: inf=float("inf") def __init__(self,N): """ ダイクストラ専用のヒープを生成する. """ self.N=N self.remain=N self.value=[Dijkstra_Heap.inf]*N self.dist=[-1]*N self.tree=list(range(N)) self.inverse=list(range(N)) def __bool__(self): return self.value[0]=self.N: break if 2*k+2==self.N: if V[k]<=V[2*k+1]: break j=2*k+1 else: u=V[2*k+1]; v=V[2*k+2] if V[k]<=u and V[k]<=v: break if u<=v: j=2*k+1 else: j=2*k+2 self.__swap(k,j) k=j return (p,d) def __setitem__(self, index, value): if self.dist[index]!=-1: return V=self.value i=self.inverse[index] if V[i]<=value: return V[i]=value while i>0 and V[(i-1)>>1]>V[i]: j=(i-1)>>1 self.__swap(i,j) i=j def __getitem__(self, index): return self.dist[index] if self.dist[index]>=0 else self.value[self.inverse[index]] def final_answer(self, index,default): return self.dist[index] if self.dist[index]>=0 else default #================================================== H,W=map(int,input().split()) A=[] for _ in range(H): A.append(list(map(lambda x:1 if x=="k" else 0,input()))+[-1]) A.append([-1]*(W+1)) Q=Dijkstra_Heap(H*W); Q[0]=0 while Q: x,t=Q.pop() i,j=divmod(x,W) if x==H*W-1: break if i!=H-1: if A[i+1][j]==0: Q[(i+1)*W+j]=t+1 else: Q[(i+1)*W+j]=t+1+((i+1)+j) if j!=W-1: if A[i][j+1]==0: Q[i*W+(j+1)]=t+1 else: Q[i*W+(j+1)]=t+1+(i+(j+1)) print(Q[H*W-1])