#include <iostream>
#include <queue>
using namespace std;
const int N = 160;
char graph[N][N];
bool st[N][N];
int c, r;
typedef pair<int, int> pii;
pii start, ed;
struct node{
pii loc;
int step;
};
int dic[8][2] = {
{-1, -2}, {-2, -1}, {-2, 1}, {-1, 2},
{1, 2}, {2, 1}, {2, -1}, {1, -2}
};
int bfs(int x, int y){
queue<node> qu;
qu.push({{x, y}, 0});
st[x][y] = true;
while(!qu.empty()){
node now = qu.front();
qu.pop();
for(int i = 0; i < 8; i ++ ){
int xx = now.loc.first + dic[i][0];
int yy = now.loc.second + dic[i][1];
if(xx < 0 || xx >= r || yy < 0 || yy >= c) continue;
if(st[xx][yy]) continue;
if(xx == ed.first && yy == ed.second) return now.step + 1;
qu.push({{xx, yy}, now.step + 1});
st[xx][yy] = true;
}
}
}
int main(){
cin >> c >> r;
for(int i = 0; i < r; i ++ ){
for(int j = 0; j < c; j ++ ){
cin >> graph[i][j];
if(graph[i][j] == 'K') start = {i, j};
else if(graph[i][j] == 'H') ed = {i, j};
}
}
cout << bfs(start.first, start.second) << endl;
return 0;
}