#include<bits/stdc++.h>
#define F(i,a,b) for(int i=(a);i<=(b);++i)
#define D(i,a,b) for(int i=(a);i>=(b);--i)
#define y1 yy1
using namespace std;
const int N=50,inf=1e9;
char a[N][N];
int s[N][N];
int dp[N][N][N][N];
inline void cmin(int &x,int y){x=x<y?x:y;}
inline int qsize(int x1,int y1,int x2,int y2){
	return s[x2][y2]-s[x1-1][y2]-s[x2][y1-1]+s[x1-1][y1-1];
}
int main(){
	int n,m;
	scanf("%d%d",&n,&m);
	F(i,1,n)scanf("%s",a[i]+1);
	int px,py;
	F(i,1,n)F(j,1,m){
		if(a[i][j]=='P')
			px=i,py=j,a[i][j]='+';
		s[i][j]=s[i-1][j]+s[i][j-1]-s[i-1][j-1]+(a[i][j]=='#');
	}
	F(x1,1,n)F(y1,1,m)F(x2,x1,n)F(y2,y1,m)
		dp[x1][y1][x2][y2]=inf;
	dp[1][1][n][m]=0;
	int ans=inf;
	F(x1,1,n)F(y1,1,m)
		D(x2,n,x1)D(y2,m,y1){
			if(px<x1||px>x2||py<y1||py>y2)continue;
			int w=dp[x1][y1][x2][y2];
			F(x,x1,x2)F(y,y1,y2){
				int c1=qsize(x1,y1,x,y)+qsize(x+1,y+1,x2,y2);
				int c2=qsize(x1,y1,x2,y2)-c1;
				cmin(dp[x1][y+1][x][y2],w+c1);
				cmin(dp[x+1][y1][x2][y],w+c1);
				cmin(dp[x1][y1][x][y],w+c2);
				cmin(dp[x+1][y+1][x2][y2],w+c2);
			}
			cmin(ans,w+qsize(x1,y1,px,py));
			cmin(ans,w+qsize(px,y1,x2,py));
			cmin(ans,w+qsize(x1,py,px,y2));
			cmin(ans,w+qsize(px,py,x2,y2));
	}
	printf("%d\n",ans);
	return 0;
}