#include <iostream>
#include <queue>
#include <cstring>
using namespace std;
const int N = 155;
int n, m;
char g[N][N];
bool st[N][N];
int dist[N][N];
int dx[] = {-1, -2, -2, -1, 1, 2, 2, 1};
int dy[] = {-2, -1, 1, 2, 2, 1, -1, -2};
int bfs(int x, int y)
{
queue<pair<int,int>> q;
memset(st, false, sizeof st);
memset(dist, -1, sizeof dist);
q.push({x, y});
st[x][y] = true;
dist[x][y] = 0;
while(!q.empty())
{
auto t = q.front();
q.pop();
for(int i=0; i<8; i++)
{
int a = t.first + dx[i], b = t.second + dy[i];
if(a < 0 || a >= n || b < 0 || b >= m) continue;
if(g[a][b] == '*') continue;
if(st[a][b]) continue;
q.push({a, b});
st[a][b] = true;
dist[a][b] = dist[t.first][t.second] + 1;
if(g[a][b] == 'H') return dist[a][b];
}
}
return -1;
}
int main()
{
cin >> m >> n;
for(int i=0; i<n; i++)
for(int j=0; j<m; j++)
cin >> g[i][j];
int x, y;
for(int i=0; i<n; i++)
for(int j=0; j<m; j++)
if(g[i][j] == 'K')
{
x = i;
y = j;
}
cout << bfs(x, y) << endl;
return 0;
}