#include<stdio.h>
#include<math.h>
#include<algorithm>
#include<queue>
#include<deque>
#include<stack>
#include<string>
#include<string.h>
#include<vector>
#include<set>
#include<map>
#include<bitset>
#include<stdlib.h>
#include<cassert>
#include<time.h>
#include<bitset>
using namespace std;
const long long mod=1000000007;
const long long inf=mod*mod;
const long long d2=(mod+1)/2;
const long double EPS=1e-13;
const long double PI=acos(-1.0);
int ABS(int a){return max(a,-a);}
long long ABS(long long a){return max(a,-a);}
long double ABS(long double a){return max(a,-a);}
char in[50][50];
int dp[50][50][50][50];
int sum[50][50];
int rect(int a,int b,int c,int d){
	return sum[c][d]-sum[a][d]-sum[c][b]+sum[a][b];
}
int H,W;
int calc(int a,int b,int c,int d){
	if(~dp[a][b][c][d])return dp[a][b][c][d];
	if(a==0&&b==H&&c==0&&d==W)return dp[a][b][c][d]=0;
	int ret=mod;
	for(int i=0;i<H;i++){
		if(a<i&&i+1<b)continue;
		for(int j=0;j<W;j++){
			if(c<j&&j+1<d)continue;
			if((a==i||i+1==b)&&(c==j||j+1==d))continue;
			int tmp=calc(min(i,a),max(i+1,b),min(j,c),max(j+1,d));
			tmp+=rect(a,min(j,c),b,max(j+1,d))+rect(min(i,a),c,max(i+1,b),d)-rect(a,c,b,d)*2;
			ret=min(ret,tmp);
		}
	}
//	printf("%d %d %d %d: %d\n",a,b,c,d,ret);
	return dp[a][b][c][d]=ret;
}
int main(){
	int a,b;scanf("%d%d",&a,&b);
	H=a;W=b;
	int pi,pj;
	for(int i=0;i<a;i++)scanf("%s",in[i]);
	for(int i=0;i<a;i++)for(int j=0;j<b;j++){
		if(in[i][j]=='P'){
			pi=i;pj=j;
		}
	}
	for(int i=0;i<=a;i++)for(int j=0;j<=a;j++)
		for(int k=0;k<=b;k++)for(int l=0;l<=b;l++){
			dp[i][j][k][l]=-1;
		}
	for(int i=0;i<a;i++)for(int j=0;j<b;j++){
		if(in[i][j]=='#')sum[i+1][j+1]++;
	}
	for(int i=0;i<=a;i++)for(int j=0;j<=b;j++){
		sum[i][j+1]+=sum[i][j];
	}
	for(int i=0;i<=a;i++)for(int j=0;j<=b;j++){
		sum[i+1][j]+=sum[i][j];
	}
	int ret=mod;
	for(int i=0;i<a;i++){
		for(int j=i+1;j<=a;j++){
			for(int k=0;k<b;k++){
				for(int l=k+1;l<=b;l++){
					if(i>pi||j<=pi||k>pj||l<=pj)continue;
					int tmp=calc(i,j,k,l);
					int A=min(min(rect(i,k,pi+1,pj+1),rect(i,pj,pi+1,l)),min(rect(pi,k,j,pj+1),rect(pi,pj,j,l)));
					ret=min(ret,tmp+A);
				}
			}
		}
	}
	printf("%d\n",ret);
}