#include<bits/stdc++.h>
using namespace std;
const int MAX = 150+5;
typedef pair<int, int> PII;
int r, c;
char g[MAX][MAX];
int dis[MAX][MAX];
PII q[MAX*MAX];
int d[8][2] = {-2, 1, -1, 2, 1, 2, 2, 1, 2, -1, 1, -2, -1, -2, -2, -1};
int bfs(int x, int y){
int hh = 0, tt = 0;
memset(dis, -1, sizeof dis);
dis[x][y] = 0;
q[hh] = {x, y};
while(hh<=tt){
PII t = q[hh++];
for(int i=0; i<8; i++){
x = t.first+d[i][0], y = t.second+d[i][1];
if(x<0 || x>=r || y<0 || y>=c) continue;
if(dis[x][y]!=-1) continue;
if(g[x][y]=='*') continue;
dis[x][y] = dis[t.first][t.second] + 1;
if(g[x][y]=='H') return dis[x][y];
q[++tt] = {x, y};
}
}
return -1;
}
int main(){
scanf("%d%d", &c, &r);
for(int i=0; i<r; i++) scanf("%s", g[i]);
int x, y;
for(int i=0; i<r; i++){
for(int j=0; j<c; j++){
if(g[i][j]=='K'){
x = i, y = j;
break;
}
}
}
cout<< bfs(x, y)<< endl;
return 0;
}