import java.util.*; import java.io.*; import java.math.*; public class Main { public static void main(String[] args) throws Exception { Scanner sc = new Scanner(System.in); int H = sc.nextInt(); int W = sc.nextInt(); char[][] map = new char[H][]; for(int i = 0; i < H; i++){ map[i] = sc.next().toCharArray(); } System.out.println(bfs(H,W,map)); } public static int bfs(int h, int w, char[][]map){ Point[] D2 = {new Point(1,0), new Point(0,1)}; int sy = 0; int sx = 0; int gy = h-1; int gx = w-1; Point start = new Point(sx, sy), goal = new Point(gx, gy); ArrayDeque dq = new ArrayDeque<>(); dq.add(start); int[][] moves = new int[h][w]; for(int i = 0; i < h; i++){ Arrays.fill(moves[i],Integer.MAX_VALUE); } moves[0][0] = 0; map[start.getY()][start.getX()] = '#'; while (dq.size() > 0) { Point p = dq.removeFirst(); for (int i=0; i<2; i++) { int x = p.getX()+D2[i].getX(); int y = p.getY()+D2[i].getY(); if(!(0 <= y && y < h && 0 <= x && x < w)){ continue; } else if (map[y][x] == '.') { dq.addLast(new Point(x, y)); moves[y][x] = Math.min(moves[y][x],moves[p.getY()][p.getX()] + 1); } else if (map[y][x] == 'k') { dq.addLast(new Point(x, y)); moves[y][x] = Math.min(moves[y][x],(moves[p.getY()][p.getX()] + 1)+y+x); } } } /* for(int i = 0; i < h; i++){ for(int j = 0; j < w; j++){ System.out.print(moves[i][j]+" "); } System.out.println(); } */ return moves[goal.getY()][goal.getX()]; } } class Point{ private int x; private int y; Point(int a, int b) {x=a; y=b;} int getX() {return x;} int getY() {return y;} void setX(int n) {x = n;} void setY(int n) {y = n;} }