#include<iostream>
#include<queue>
#include<cstring>
using namespace std;
typedef pair<int, int> PII;
const int N = 160;
char a[N][N];
bool s[N][N];
int d[N][N], n, m;
queue<PII> q;
void Bfs(int fx, int fy, int ex, int ey)
{
q.push({fx, fy});
s[fx][fy] = true;
d[fx][fy] = 0;
int dx[8] = {-2, -1, 1, 2, 2, 1, -1, -2};
int dy[8] = {1, 2, 2, 1, -1, -2, -2, -1};
while(!q.empty())
{
PII it = q.front();
q.pop();
for(int i = 0; i < 8; i++)
{
int x = it.first + dx[i];
int y = it.second + dy[i];
if(x > 0 && x <= n && y > 0 && y <= m && a[x][y] != '*' && !s[x][y])
{
q.push({x, y});
s[x][y] = true;
d[x][y] = d[it.first][it.second] + 1;
}
if(x == ex && x == ey) break;
}
}
}
int main()
{
cin.tie(0);
cout.tie(0);
ios::sync_with_stdio;
cin >> m >> n;
int fx, fy, ex, ey;
memset(d, -1, sizeof(d));
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= m; j++)
{
cin >> a[i][j];
if(a[i][j] == 'K') fx = i, fy = j;
if(a[i][j] == 'H') ex = i, ey = j;
}
}
Bfs(fx, fy, ex, ey);
cout << d[ex][ey] << endl;
return 0;
}