/************************************************
 * Au: Hany01
 * Prob: haner
 * Email: hany01dxx@gmail.com & hany01@foxmail.com
 * Inst: Yali High School
************************************************/

#include<bits/stdc++.h>

using namespace std;

typedef long long LL;
typedef long double LD;
typedef pair<int, int> PII;
#define Rep(i, j) for (register int i = 0, i##_end_ = (j); i < i##_end_; ++ i)
#define For(i, j, k) for (register int i = (j), i##_end_ = (k); i <= i##_end_; ++ i)
#define Fordown(i, j, k) for (register int i = (j), i##_end_ = (k); i >= i##_end_; -- i)
#define Set(a, b) memset(a, b, sizeof(a))
#define Cpy(a, b) memcpy(a, b, sizeof(a))
#define X first
#define Y second
#define PB(a) push_back(a)
#define MP(a, b) make_pair(a, b)
#define SZ(a) ((int)(a).size())
#define ALL(a) a.begin(), a.end()
#define INF (0x3f3f3f3f)
#define INF1 (2139062143)
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define y1 wozenmezhemecaia

template <typename T> inline bool chkmax(T &a, T b) { return a < b ? a = b, 1 : 0; }
template <typename T> inline bool chkmin(T &a, T b) { return b < a ? a = b, 1 : 0; }

template <typename T> inline T read() {
    static T _, __; static char c_;
    for (_ = 0, __ = 1, c_ = getchar(); c_ < '0' || c_ > '9'; c_ = getchar()) if (c_ == '-') __ = -1;
    for ( ; c_ >= '0' && c_ <= '9'; c_ = getchar()) _ = (_ << 1) + (_ << 3) + (c_ ^ 48);
    return _ * __;
}
//EOT


const int maxn = 45;

int n, m;
int sum[maxn][maxn], dp[maxn][maxn][maxn][maxn], x, y, vis[maxn][maxn][maxn][maxn];

inline void Init() {
    static char str[maxn];
    n = read<int>(), m = read<int>();
    For(i, 1, n) {
        scanf("%s", str + 1);
        For(j, 1, m)
            if (str[j] == 'P') x = i, y = j;
            else if (str[j] == '#') sum[i][j] = 1;
    }
    For(i, 1, n) For(j, 1, m)
        sum[i][j] += sum[i - 1][j] + sum[i][j - 1] - sum[i - 1][j - 1];
}

inline int getsum(int x1, int y1, int x2, int y2) {
    //if (sum[x2][y2] - sum[x2][y1 - 1] - sum[x1 - 1][y2] + sum[x1 - 1][y1 - 1] < 0)
        //cout << x1 << ' ' << y1 << ' ' << x2 << ' ' << y2 << endl;
    return sum[x2][y2] - sum[x2][y1 - 1] - sum[x1 - 1][y2] + sum[x1 - 1][y1 - 1];
}

int DFS(int x1, int y1, int x2, int y2) {
    if (!chkmax(vis[x1][y1][x2][y2], 1)) return dp[x1][y1][x2][y2];
    int &ans = dp[x1][y1][x2][y2] = INF;
    chkmin(ans, getsum(x1, y1, x, y));
    chkmin(ans, getsum(x1, y, x, y2));
    chkmin(ans, getsum(x, y1, x2, y));
    chkmin(ans, getsum(x, y, x2, y2));
    For(i, x1 + 1, x) For(j, y1 + 1, y) if (i != x1 || j != y1)
        chkmin(ans, DFS(i, j, x2, y2) + getsum(i, y1, x2, j - 1) + getsum(x1, j, i - 1, y2));
    For(i, x, x2 - 1) For(j, y, y2 - 1) if (i != x2 || j != y2)
        chkmin(ans, DFS(x1, y1, i, j) + getsum(i + 1, y1, x2, j) + getsum(x1, j + 1, i, y2));
    For(i, x, x2 - 1) For(j, y1 + 1, y) if (i != x2 || j != y1)
        chkmin(ans, DFS(x1, j, i, y2) + getsum(x1, y1, i, j - 1) + getsum(i + 1, j, x2, y2));
    For(i, x1 + 1, x) For(j, y, y2 - 1) if (i != x1 || j != y2)
        chkmin(ans, DFS(i, y1, x2, j) + getsum(x1, y1, i - 1, j) + getsum(i, j + 1, x2, y2));
    return ans;
}

int main() {


    Init();
    printf("%d\n", DFS(1, 1, n, m));

    return 0;
}

Measured memory_bytes: 29312