#include <iostream>
#include <cstring>
#include <queue>
using namespace std;
#define x first
#define y second
typedef pair<int,int> PII;
int dx[] = {-2,-2,-1,-1,1,1,2,2};
int dy[] = {-1,1,-2,2,-2,2,-1,1};
const int N = 160;
char s[N][N];
PII st[N][N];
int n, m;
void bfs(int sx, int sy)
{
queue<PII> q;
q.push({sx,sy});
memset(st, -1, sizeof st);
st[sx][sy] = {0,0};
while( q.size() )
{
PII t = q.front();
q.pop();
for(int i = 0 ; i < 8 ; i ++ )
{
int xx = t.x + dx[i], yy = t.y + dy[i];
if( xx >= 1 && xx <= n && yy >=1 && yy <= m && st[xx][yy].x == -1 && ( s[xx][yy] == '.' || s[xx][yy] == 'H') )
q.push({xx,yy}),st[xx][yy] = t;
}
}
}
int main()
{
int a,b,c,d;
cin >> m >> n;
for(int i = 1; i <= n; i ++ )
{
for(int j = 1; j <= m ; j ++ )
{
cin >> s[i][j];
if( s[i][j] == 'K')
a = i , b = j;
else if( s[i][j] == 'H')
c = i, d = j;
}
}
// cout << a << ' ' << b << endl;
// cout << c << ' ' << d << endl;
bfs(a,b);
PII end(c,d);
int cnt = 1;
while(1)
{
PII o = st[end.x][end.y];
end = o;
// cout << end.x << ' ' << end.y << endl;
if( o.x == a && o.y == b )
break;
cnt++;
}
cout << cnt << endl;
return 0;
}