#include <iostream>
#include <cstring>
#include <algorithm>
#define x first
#define y second
using namespace std;
typedef pair<int, int> PII;
const int N = 160,M=N*N;
PII q[M];
int n,m,dist[N][N];
char g[N][N];
bool st[N][N];
int dx[] = {-2, -1, 1, 2, 2, 1, -1, -2};
int dy[] = {1, 2, 2, 1, -1, -2, -2, -1};
int bfs(int sx,int sy){
int hh=0,tt=0;
q[0]={sx,sy};
dist[sx][sy] = 0;
st[sx][sy] = true;
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];
if(x<0||x>=n||y<0||y>=m) continue;
if(st[x][y]||g[x][y]=='*')continue;
dist[x][y] = dist[t.x][t.y]+1;
st[x][y] = true;
q[++tt]={x,y};
if(g[x][y]=='H'){
return dist[x][y];
}
}
}
return -1;
}
int main()
{
cin>>m>>n;
for (int i = 0; i < n; i ++ ) cin >> g[i];
PII star,end;
for (int i = 0; i < n; i ++ ){
for (int j = 0; j < n; j ++ ){
if(g[i][j]=='K') star={i,j};
if(g[i][j]=='H') end = {i,j};
}
}
cout<<bfs(star.x,star.y);
return 0;
}
好耶!!码风赞