#include<bits/stdc++.h>
using namespace std;
int m,n,x,y;
char a[400][400];
bool f[400][400];
int dx[8]={-2,-1,1,2,2,1,-1,-2},dy[8]={1,2,2,1,-1,-2,-2,-1};
struct node
{
int hx,hy,c;
};
queue<node> q;
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]=='K') x=i,y=j;
}
}
q.push({x,y,0});
while(!q.empty())
{
node l=q.front();
f[l.hx][l.hy]=true;
if(a[l.hx][l.hy]=='H')
{
cout<<l.c;
return 0;
}
q.pop();
for(int i=0;i<8;i++)
{
if(a[l.hx+dx[i]][l.hy+dy[i]]!='*' && !f[l.hx+dx[i]][l.hy+dy[i]]) q.push({l.hx+dx[i],l.hy+dy[i],l.c+1});
}
}
return 0;
}