#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using pii = pair<int, int>;
template <class T>
using V = vector<T>;
template <class T>
using VV = V<V<T>>;

#define pb push_back
#define eb emplace_back
#define mp make_pair
#define fi first
#define se second
#define rep(i, n) rep2(i, 0, n)
#define rep2(i, m, n) for (int i = m; i < (n); i++)
#define per(i, b) per2(i, 0, b)
#define per2(i, a, b) for (int i = int(b) - 1; i >= int(a); i--)
#define ALL(c) (c).begin(), (c).end()
#define SZ(x) ((int)(x).size())

constexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n - 1); }

template <class T, class U>
void chmin(T& t, const U& u) {
    if (t > u) t = u;
}
template <class T, class U>
void chmax(T& t, const U& u) {
    if (t < u) t = u;
}

template <class T, class U>
ostream& operator<<(ostream& os, const pair<T, U>& p) {
    os << "(" << p.first << "," << p.second << ")";
    return os;
}

template <class T>
ostream& operator<<(ostream& os, const vector<T>& v) {
    os << "{";
    rep(i, v.size()) {
        if (i) os << ",";
        os << v[i];
    }
    os << "}";
    return os;
}

#ifdef LOCAL
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
    cerr << " " << H;
    debug_out(T...);
}
#define debug(...) \
    cerr << __LINE__ << " [" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#define dump(x) cerr << __LINE__ << " " << #x << " = " << (x) << endl
#else
#define debug(...) (void(0))
#define dump(x) (void(0))
#endif

const int MX = 42;
const int INF = TEN(9);
int dp[MX][MX][MX][MX];
int pf[MX][MX];

int get(int a, int b, int c, int d) {  //[a,b]*[c,d]
    return max(0, pf[b + 1][d + 1] - pf[b + 1][c] - pf[a][d + 1] + pf[a][c]);
}

int main() {
    int H, W;
    cin >> H >> W;
    V<string> s(H);
    int pr, pc;

    rep(i, H) {
        cin >> s[i];
        rep(j, W) {
            if (s[i][j] == 'P') {
                pr = i, pc = j, s[i][j] = '+';
            }
            pf[i + 1][j + 1] =
                pf[i + 1][j] + pf[i][j + 1] - pf[i][j] + (s[i][j] == '#');
        }
    }

    rep(i, MX) rep(j, MX) rep(k, MX) rep(l, MX) dp[i][j][k][l] = INF;
    dp[0][H - 1][0][W - 1] = 0;

    rep(a, H) {
        for (int b = H - 1; b >= a; --b) {
            rep(c, W) {
                for (int d = W - 1; d >= c; --d) {
                    for (int e = a; e <= b; ++e) {
                        for (int f = c; f <= d; ++f) {
                            {
                                int nx = dp[a][b][c][d] + get(a, e, c, f) +
                                         get(e + 1, b, f + 1, d);
                                if (f < d) chmin(dp[a][e][f + 1][d], nx);
                                if (e < b) chmin(dp[e + 1][b][c][f], nx);
                            }
                            {
                                int nx = dp[a][b][c][d] + get(a, e, f + 1, d) +
                                         get(e + 1, b, c, f);
                                chmin(dp[a][e][c][f], nx);
                                if (f < d && e < b)
                                    chmin(dp[e + 1][b][f + 1][d], nx);
                            }
                        }
                    }
                }
            }
        }
    }

    int ans = INF;
    for (int a = 0; a <= pr; ++a) {
        for (int b = pr; b < H; ++b) {
            for (int c = 0; c <= pc; ++c) {
                for (int d = pc; d < W; ++d) {
                    int mini = min({get(pr, b, pc, d), get(pr, b, c, pc),
                                    get(a, pr, c, pc), get(a, pr, pc, d)});
                    chmin(ans, dp[a][b][c][d] + mini);
                }
            }
        }
    }
    cout << ans << endl;
    return 0;
}

Measured memory_bytes: 15756