#include <iostream>
#include <tuple>
#include <sstream>
#include <vector>
#include <cmath>
#include <ctime>
#include <bitset>
#include <cassert>
#include <cstdio>
#include <queue>
#include <set>
#include <map>
#include <fstream>
#include <cstdlib>
#include <string>
#include <cstring>
#include <algorithm>
#include <numeric>

#define mp make_pair
#define mt make_tuple
#define fi first
#define se second
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define forn(i, n) for (int i = 0; i < (int)(n); ++i)
#define for1(i, n) for (int i = 1; i <= (int)(n); ++i)
#define ford(i, n) for (int i = (int)(n) - 1; i >= 0; --i)
#define fore(i, a, b) for (int i = (int)(a); i <= (int)(b); ++i)

using namespace std;

typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vpi;
typedef vector<vi> vvi;
typedef long long i64;
typedef vector<i64> vi64;
typedef vector<vi64> vvi64;
typedef pair<i64, i64> pi64;
typedef double ld;

template<class T> bool uin(T &a, T b) { return a > b ? (a = b, true) : false; }
template<class T> bool uax(T &a, T b) { return a < b ? (a = b, true) : false; }

const int maxn = 51;
string f[maxn];
int sum[51][51];
int dp[51][51][51][51];

int rect_sum(int lx, int rx, int ly, int ry) {
    return sum[rx][ry] - sum[lx][ry] - sum[rx][ly] + sum[lx][ly];
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.precision(10);
    cout << fixed;
#ifdef LOCAL_DEFINE
    freopen("input.txt", "rt", stdin);
#endif

    int h, w;
    cin >> h >> w;
    forn(i, h) cin >> f[i];
    int x = -1, y = -1;
    forn(i, h) forn(j, w) if (f[i][j] == 'P') x = i, y = j;
    forn(i, h) forn(j, w) sum[i + 1][j + 1] = (f[i][j] == '#') + sum[i + 1][j] + sum[i][j + 1] - sum[i][j];
    forn(lx, h + 1) forn(rx, h + 1) forn(ly, w + 1) forn(ry, w + 1) dp[lx][rx][ly][ry] = 1e9;
    dp[0][h][0][w] = 0;
    int ans = 1e9;
    forn(lx, x + 1) for (int rx = h; rx > x; --rx) forn(ly, w + 1) for (int ry = w; ry > y; --ry) {
        for (int bx = lx + 1; bx < rx; ++bx) {
            bool fx = bx <= x;
            int llx = lx, lrx = bx, rlx = bx, rrx = rx;
            if (fx) swap(llx, rlx), swap(lrx, rrx);
            uin(dp[llx][lrx][ly][ry], dp[lx][rx][ly][ry] + rect_sum(rlx, rrx, ly, ry));
        }
        for (int by = ly + 1; by < ry; ++by) {
            bool fy = by <= y;
            int lly = ly, lry = by, rly = by, rry = ry;
            if (fy) swap(lly, rly), swap(lry, rry);
            uin(dp[lx][rx][lly][lry], dp[lx][rx][ly][ry] + rect_sum(lx, rx, rly, rry));
        }
        for (int bx = lx + 1; bx < rx; ++bx) for (int by = ly + 1; by < ry; ++by) {
            bool fx = bx <= x, fy = by <= y;
            int llx = lx, lrx = bx, rlx = bx, rrx = rx;
            int lly = ly, lry = by, rly = by, rry = ry;
            if (fx) swap(llx, rlx), swap(lrx, rrx);
            if (fy) swap(lly, rly), swap(lry, rry);
            uin(dp[llx][lrx][lly][lry], dp[lx][rx][ly][ry] + rect_sum(rlx, rrx, lly, lry) + rect_sum(llx, lrx, rly, rry));
        }
        forn(fx, 2) forn(fy, 2) {
            int llx = lx, lrx = x + 1, rlx = x, rrx = rx;
            int lly = ly, lry = y + 1, rly = y, rry = ry;
            if (fx) swap(llx, rlx), swap(lrx, rrx);
            if (fy) swap(lly, rly), swap(lry, rry);
            uin(ans, dp[lx][rx][ly][ry] + rect_sum(llx, lrx, lly, lry));
        }
    }

    cout << ans << '\n';

#ifdef LOCAL_DEFINE
    cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
    return 0;
}


Measured memory_bytes: 22400