#include <bits/stdc++.h>
using namespace std;
char a[200][200];
int n, m, dist[200][200];
int fx[8] = {1, 1, -1, -1, 2, 2, -2, -2};
int fy[8] = {2, -2, 2, -2, 1, -1, 1, -1};
int sx, sy, zx, zy;
int bfs(int x, int y) {
queue<pair<int, int>> q;
q.push({x, y});
dist[x][y] = 0;
while (!q.empty()) {
auto current = q.front();
q.pop();
int cx = current.first;
int cy = current.second;
if (cx == zx && cy == zy) {
return dist[cx][cy];
}
for (int i = 0; i < 8; i++) {
int nx = cx + fx[i];
int ny = cy + fy[i];
if (nx >= 1 && nx <= m && ny >= 1 && ny <= n && a[nx][ny] != '*' && dist[nx][ny] == -1) {
dist[nx][ny] = dist[cx][cy] + 1;
q.push({nx, ny});
}
}
}
return -1; // No path found
}
int main() {
cin >> n >> m;
for (int i = 1; i <= m; i++) {
for (int j = 1; j <= n; j++) {
cin >> a[i][j];
if (a[i][j] == 'H') {
zx = i;
zy = j;
}
if (a[i][j] == 'K') {
sx = i;
sy = j;
}
dist[i][j] = -1;
}
}
int result = bfs(sx, sy);
cout << result << endl;
return 0;
}
/**************************************************************
Problem: 1441
User: zhouhongyi
Language: C++
Result: Accepted
Time:16 ms
Memory:2272 kb
****************************************************************/