//这个题意感觉不太清楚,说了和马一样的规则,但是没有马的“拌脚”。。
#include <iostream>
#include <algorithm>
#include <cstring>
#define x first
#define y second
using namespace std;
typedef pair<int, int> PII;
const int N = 155;
int n, m, dx[8] = {-1, -1, -2, -2, 1, 1, 2, 2}, dy[8] = {2, -2, 1, -1, 2, -2, 1, -1};
char a[N][N];
PII q[N * N];
int layer[N][N];
PII st = {-1, -1}, ed = {-1, -1};
void bfs(int sx, int sy)
{
int cnt = 0;
q[0] = {sx, sy};
int hh = 0, tt = 0;
memset(layer, -1, sizeof layer);
layer[sx][sy] = 0;
while (hh <= tt)
{
auto t = q[hh ++ ];
for (int i = 0; i < 8; i ++ )
{
int _x = t.x + dx[i], _y = t.y + dy[i];
// int mid_x = _x + (dx[i] >> 1), mid_y = t.y + (dy[i] >> 1);
if (_x < 1 || _x > n || _y < 1 || _y > m || layer[_x][_y] != -1 || a[_x][_y] == '*') continue;
q[++ tt] = {_x, _y};
layer[_x][_y] = layer[t.x][t.y] + 1;
if (_x == ed.x && _y == ed.y) return;
}
}
}
int main()
{
cin >> m >> n;
for (int i = 1; i <= n; i ++ ) {
cin >> a[i] + 1;
for (int j = 1; j <= m; j ++ )
if (a[i][j] == 'K')
st = {i, j};
else if (a[i][j] == 'H')
ed = {i, j};
}
bfs(st.x, st.y);
cout << layer[ed.x][ed.y] << endl;
// for (int i = 1; i <= n; i ++ ) {
// for (int j = 1; j <= m; j ++ ) {
// cout << layer[i][j] << " ";
// }
// cout << endl;
// }
return 0;
}