from collections import deque
n, m = map(int, input().split())
si, sj = -1, -1
ei, ej = -1, -1
grid = []
for i in range(m):
s = input()
grid.append( [ch for ch in s] )
try:
idx = s.index('K')
si, sj = i, idx
except:
pass
try:
idx = s.index('H')
ei, ej = i, idx
except:
pass
que = deque()
que.append((si, sj))
visited = {(si, sj)}
step = 0
ans = 0
flag = False
while len(que) > 0:
nodes_num = len(que)
for _ in range(nodes_num):
i, j = que.popleft()
for ii, jj in [ [i-1, j-2], [i-1, j+2], [i+1, j-2], [i+1, j+2], [i-2, j-1], [i-2, j+1], [i+2, j-1], [i+2, j+1] ]:
#print(ii, jj)
if ii >= 0 and ii < m and jj >= 0 and jj < n and grid[ii][jj] != '*' and (ii, jj) not in visited:
if ii == ei and jj == ej:
ans = step + 1
flag = True
break
visited.add((ii, jj))
que.append((ii, jj))
if flag:
break
if flag:
break
step += 1
print(ans)