#include <bits/stdc++.h>
// iostream is too mainstream
#include <cstdio>
// bitch please
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <list>
#include <cmath>
#include <iomanip>
#include <time.h>
#define dibs reserve
#define OVER9000 1234567890
#define ALL_THE(CAKE,LIE) for(auto LIE =CAKE.begin(); LIE != CAKE.end(); LIE++)
#define tisic 47
#define soclose 1e-8
#define chocolate win
// so much chocolate
#define patkan 9
#define ff first
#define ss second
#define abs(x) ((x < 0)?-(x):x)
#define uint unsigned int
#define dbl long double
#define pi 3.14159265358979323846
using namespace std;
// mylittledoge

typedef long long cat;

#ifdef DONLINE_JUDGE
	// palindromic tree is better than splay tree!
	#define lld I64d
#endif

int main() {
	cin.sync_with_stdio(0);
	cin.tie(0);
	cout << fixed << setprecision(10);
	int R, C, MX = 42;
	cin >> R >> C;
	vector<string> V(R);
	int P[2] = {-1, -1};
	vector< pair<int, int> > I;
	for(int i = 0; i < R; i++) {
		cin >> V[i];
		for(int j = 0; j < C; j++)
			if(V[i][j] == 'P') P[0] = i, P[1] = j;
		for(int j = 0; j < C; j++)
			if(V[i][j] == '#') I.push_back({i, j});
	}
	int ans = R*C;

	vector< vector<int> > sum(R+1, vector<int>(C+1, 0));
	ALL_THE(I, it) sum[it->ff+1][it->ss+1] = 1;
	for(int i = 0; i < R; i++) for(int j = 0; j < C; j++)
		sum[i+1][j+1] += sum[i][j+1]+sum[i+1][j]-sum[i][j];

	int dp[MX][MX][MX][MX];
	for(int r = 0; r < MX*MX; r++)
		for(int c = 0; c < MX*MX; c++) dp[r/MX][r%MX][c/MX][c%MX] = R*C+1;
	dp[0][R][0][C] = 0;

	vector< vector< pair<int, int> > > states((R+1)*(C+1));
	for(int r = 0; r < (R+1)*(R+1); r++) for(int c = 0; c < (C+1)*(C+1); c++) {
		int rl = r/(R+1), rr = r%(R+1);
		int cl = c/(C+1), cr = c%(C+1);
		if(rl > P[0] || rr <= P[0] || cl > P[1] || cr <= P[1]) continue;
		states[(rr-rl)*(C+1)+(cr-cl)].push_back({r, c});
	}
	for(int sumd = (R+1)*(C+1)-1; sumd >= 0; sumd--) ALL_THE(states[sumd], it) {
		int r = it->ff, c = it->ss;
		int rl = r/(R+1), rr = r%(R+1);
		int cl = c/(C+1), cr = c%(C+1);
		if(dp[rl][rr][cl][cr] >= ans) continue;
		for(int dirr = 0; dirr <= 1; dirr++) for(int dirc = 0; dirc <= 1; dirc++)
		for(int rd = 1; rd < rr-rl; rd++) for(int cd = 1; cd < cr-cl; cd++) {
			int rl_in = dirr ? (rr-rd) : rl, rr_in = dirr ? rr : (rl+rd);
			int cl_in = dirc ? (cr-cd) : cl, cr_in = dirc ? cr : (cl+cd);
			if(rl_in <= P[0] && P[0] < rr_in && cl_in <= P[1] && P[1] < cr_in) continue;
			int cnt = -2 * (sum[rr_in][cr_in]+sum[rl_in][cl_in]-sum[rr_in][cl_in]-sum[rl_in][cr_in]);
			cnt += sum[rr_in][cr]+sum[rl_in][cl]-sum[rr_in][cl]-sum[rl_in][cr];
			cnt += sum[rr][cr_in]+sum[rl][cl_in]-sum[rr][cl_in]-sum[rl][cr_in];
			int rl_out = dirr ? rl : (rl+rd), rr_out = dirr ? (rr-rd) : rr;
			int cl_out = dirc ? cl : (cl+cd), cr_out = dirc ? (cr-cd) : cr;
			if(rl_out <= P[0] && P[0] < rr_out && cl_out <= P[1] && P[1] < cr_out)
				dp[rl_out][rr_out][cl_out][cr_out] = min(dp[rl_out][rr_out][cl_out][cr_out], dp[rl][rr][cl][cr] + cnt);
			else ans = min(ans, dp[rl][rr][cl][cr] + cnt);
		}
		int cnt = sum[P[0]+1][P[1]+1]+sum[rl][cl]-sum[P[0]+1][cl]-sum[rl][P[1]+1];
		ans = min(ans, dp[rl][rr][cl][cr] + cnt);
		cnt = sum[P[0]+1][cr]+sum[rl][P[1]]-sum[P[0]+1][P[1]]-sum[rl][cr];
		ans = min(ans, dp[rl][rr][cl][cr] + cnt);
		cnt = sum[rr][P[1]+1]+sum[P[0]][cl]-sum[P[0]][P[1]+1]-sum[rr][cl];
		ans = min(ans, dp[rl][rr][cl][cr] + cnt);
		cnt = sum[rr][cr]+sum[P[0]][P[1]]-sum[rr][P[1]]-sum[P[0]][cr];
		ans = min(ans, dp[rl][rr][cl][cr] + cnt);
	}
	cout << ans << "\n";
	return 0;}

// look at my code
// my code is amazing


Measured memory_bytes: 14592