import sys sys.setrecursionlimit(10**6) H, W = list(map(int, input().split())) a_list = [[j for j in input()] for i in range(H)] dp_dict = {} def solve(h, w): if h == H - 1 and w == W - 1: return 0 if (h, w) not in dp_dict: if h == H - 1: temp = solve(h, w + 1) + 2 + h + w if a_list[h][w + 1] == "k" else solve(h, w + 1) + 1 dp_dict[(h, w)] = temp return temp elif w == W - 1: temp = solve(h + 1, w) + 2 + h + w if a_list[h + 1][w] == "k" else solve(h + 1, w) + 1 dp_dict[(h, w)] = temp return temp else: temp = min([solve(h + 1, w) + 2 + h + w if a_list[h + 1][w] == "k" else solve(h + 1, w) + 1, solve(h, w + 1) + 2 + h + w if a_list[h][w + 1] == "k" else solve(h, w + 1) + 1]) dp_dict[(h, w)] = temp return temp else: return dp_dict[(h, w)] print(solve(0, 0))