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

int H,W,px,py,dp[45][45][45][45],sum[45][45];
char S[45][45];

void upd(int &x,int y){
	if(x>y)x=y;
}

int calc(int a,int b,int c,int d){
	if(a>c||b>d)return 0;
	return sum[c][d]-sum[a-1][d]-sum[c][b-1]+sum[a-1][b-1];
}

int main(){
	scanf("%d%d",&H,&W);
	for(int i=1;i<=H;++i){
		scanf("%s",S[i]+1);
		for(int j=1;j<=W;++j){
			sum[i][j]=sum[i][j-1]+(S[i][j]=='#');
			if(S[i][j]=='P')
				px=i,py=j;
		}
	}
	for(int i=1;i<=H;++i)
		for(int j=1;j<=W;++j)
			sum[i][j]+=sum[i-1][j];
	memset(dp,0x3f,sizeof dp);
	dp[1][1][H][W]=0;
	int ans=H*W;
	for(int a=1;a<=px;++a)
	for(int b=1;b<=py;++b)
	for(int c=H;c>=px;--c)
	for(int d=W;d>=py;--d){
		if(dp[a][b][c][d]>H*W)continue;
		upd(ans,dp[a][b][c][d]+min({calc(a,b,px,py),calc(a,py,px,d),calc(px,b,c,py),calc(px,py,c,d)}));
		for(int x=px;x<=c;++x)
			for(int y=py;y<=d;++y)
				upd(dp[a][b][x][y],dp[a][b][c][d]+calc(x+1,b,c,y)+calc(a,y+1,x,d));
		for(int x=a;x<=px;++x)
			for(int y=b;y<=py;++y)
				upd(dp[x][y][c][d],dp[a][b][c][d]+calc(x,b,c,y-1)+calc(a,y,x-1,d));
		for(int x=px;x<=c;++x)
			for(int y=b;y<=py;++y)
				upd(dp[a][y][x][d],dp[a][b][c][d]+calc(a,b,x,y-1)+calc(x+1,y,c,d));
		for(int x=a;x<=px;++x)
			for(int y=py;y<=d;++y)
				upd(dp[x][b][c][y],dp[a][b][c][d]+calc(a,b,x-1,y)+calc(x,y+1,c,d));
	}
	printf("%d\n",ans);
	return 0;
}
