#include <iostream>
#include <vector>
#include <queue>
#include <cmath>
#include <map>
using namespace std;


int main() {
    int n, m;
    cin >> n >> m;
    
    int si, sj;
    int ei, ej;
    
    char mat[n][m];
    for(int i = 0; i < n; i++) {
        for(int j = 0; j < m; j++) {
            cin >> mat[i][j];
            
            if(mat[i][j] == 'P') {
                si = i;
                sj = j;
            }
            if(mat[i][j] == 'K') {
                ei = i;
                ej = j;
            }
        }
    }
    
    vector<vector<bool>> visited(n, vector<bool>(m, false));
    vector<vector<bool>> visited2(n, vector<bool>(m, false));
    vector<vector<bool>> visited3(n, vector<bool>(m, false));

    queue<int> q;
    q.push(si);
    q.push(sj);
    q.push(0);
    q.push(1);
    
    while(!q.empty()) {
        int ci = q.front();
        q.pop();
        int cj = q.front();
        q.pop();
        int dist = q.front();
        q.pop();
        int cekor = q.front();
        q.pop();
        
        if(ci == ei and cj == ej) {
            cout << dist << endl;
            break;
        }
        
        if(cekor == 1) {
            if(ci + 1 < n and mat[ci + 1][cj] != '#' and !visited[ci + 1][cj]) {
                q.push(ci + 1);
                q.push(cj);
                q.push(dist + 1);
                q.push(2);
                visited[ci + 1][cj] = true;
            }
            if(ci - 1 >= 0 and mat[ci - 1][cj] != '#' and !visited[ci - 1][cj]) {
                q.push(ci - 1);
                q.push(cj);
                q.push(dist + 1);
                q.push(2);
                visited[ci - 1][cj] = true;
            }
            if(cj + 1 < m and mat[ci][cj + 1] != '#' and !visited[ci][cj + 1]) {
                q.push(ci);
                q.push(cj + 1);
                q.push(dist + 1);
                q.push(2);
                visited[ci][cj + 1] = true;
            }
            if(cj - 1 >= 0 and mat[ci][cj - 1] != '#' and !visited[ci][cj - 1]) {
                q.push(ci);
                q.push(cj - 1);
                q.push(dist + 1);
                q.push(2);
                visited[ci][cj - 1] = true;
            }
        }
        else if(cekor == 2) {
            if(ci + 2 < n and mat[ci + 1][cj] != '#' and mat[ci + 2][cj] != '#' and !visited2[ci + 2][cj]) {
                q.push(ci + 2);
                q.push(cj);
                q.push(dist + 1);
                q.push(3);
            }
            if(ci - 2 >= 0 and mat[ci - 1][cj] != '#' and mat[ci - 2][cj] != '#' and !visited2[ci - 2][cj]) {
                q.push(ci - 2);
                q.push(cj);
                q.push(dist + 1);
                q.push(3);
                visited2[ci - 2][cj] = true;
            }
            if(cj + 2 < m and mat[ci][cj + 1] != '#' and mat[ci][cj + 2] != '#' and !visited2[ci][cj + 2]) {
                q.push(ci);
                q.push(cj + 2);
                q.push(dist + 1);
                q.push(3);
                visited2[ci][cj + 2] = true;
            }
            if(cj - 2 >= 0 and mat[ci][cj - 1] != '#' and mat[ci][cj - 2] != '#' and !visited2[ci][cj - 2]) {
                q.push(ci);
                q.push(cj - 2);
                q.push(dist + 1);
                q.push(3);
                visited2[ci][cj - 2] = true;
            }
        }
        else if(cekor == 3) {
            if(ci + 3 < n and mat[ci + 1][cj] != '#' and mat[ci + 2][cj] != '#' and mat[ci + 3][cj] != '#' and !visited3[ci + 3][cj]) {
                q.push(ci + 3);
                q.push(cj);
                q.push(dist + 1);
                q.push(1);
                visited3[ci + 3][cj] = true;
            }
            if(ci - 3 >= 0 and mat[ci - 1][cj] != '#' and mat[ci - 2][cj] != '#' and mat[ci - 3][cj] != '#' and !visited3[ci - 3][cj]) {
                q.push(ci - 3);
                q.push(cj);
                q.push(dist + 1);
                q.push(1);
                visited3[ci - 3][cj] = true;
            }
            if(cj + 3 < m and mat[ci][cj + 1] != '#' and mat[ci][cj + 2] != '#' and mat[ci][cj + 3] != '#' and !visited3[ci][cj + 3]) {
                q.push(ci);
                q.push(cj + 3);
                q.push(dist + 1);
                q.push(1);
                visited3[ci][cj + 3] = true;
            }
            if(cj - 3 >= 0 and mat[ci][cj - 1] != '#' and mat[ci][cj - 2] != '#' and mat[ci][cj - 3] != '#' and !visited3[ci][cj - 3]) {
                q.push(ci);
                q.push(cj - 3);
                q.push(dist + 1);
                q.push(1);
                visited3[ci][cj - 3] = true;
            }
        }
        
    }
    return 0;
}
