// Original C++ solution (slower):
#include <bits/stdc++.h>

using namespace std;

typedef long long lint;

typedef long double llf;

typedef pair<int, int> pi;

const int MAXN = 41;

const int mod = 1e9 + 7;

#define sz(v) ((int)v.size())



int n, m, px, py;

char str[MAXN][MAXN];

int sum[MAXN][MAXN];

int dp[MAXN][MAXN][MAXN][MAXN];



bool has(int sx, int ex, int sy, int ey){

	return sx <= px && px < ex && sy <= py && py < ey;

}



int gs(int sx, int ex, int sy, int ey){

	return sum[ex][ey] - sum[sx][ey] - sum[ex][sy] + sum[sx][sy];

}



int f(int sx, int ex, int sy, int ey){

	if(ex - sx == 1 && ey - sy == 1) return 0;

	if(~dp[sx][ex][sy][ey]) return dp[sx][ex][sy][ey];

	int ret = min({

		gs(sx, px + 1, sy, py + 1),

		gs(px, ex, py, ey),

		gs(sx, px + 1, py, ey),

		gs(px, ex, sy, py + 1)});

	for(int i=sx+1; i<ex; i++){

		int x1 = has(sx, i, sy, ey) ? f(sx, i, sy, ey) : gs(sx, i, sy, ey);

		int x2 = has(i, ex, sy, ey) ? f(i, ex, sy, ey) : gs(i, ex, sy, ey);

		ret = min(ret, x1 + x2);

	}

	for(int i=sy+1; i<ey; i++){

		int x1 = has(sx, ex, sy, i) ? f(sx, ex, sy, i) : gs(sx, ex, sy, i);

		int x2 = has(sx, ex, i, ey) ? f(sx, ex, i, ey) : gs(sx, ex, i, ey);

		ret = min(ret, x1 + x2);

	}

	for(int i=sx+1; i<ex; i++){

		for(int j=sy+1; j<ey; j++){

			if(has(sx, i, sy, j)){

				ret = min(ret, f(sx, i, sy, j) + gs(i, ex, sy, j) + gs(sx, i, j, ey));

			}

			if(has(i, ex, sy, j)){

				ret = min(ret, f(i, ex, sy, j) + gs(i, ex, j, ey) + gs(sx, i, sy, j));

			}

			if(has(sx, i, j, ey)){

				ret = min(ret, f(sx, i, j, ey) + gs(sx, i, sy, j) + gs(i, ex, j, ey));

			}

			if(has(i, ex, j, ey)){

				ret = min(ret, f(i, ex, j, ey) + gs(sx, i, j, ey) + gs(i, ex, sy, j));

			}

		}

	}

	return dp[sx][ex][sy][ey] = ret;

}



int main(){

	cin >> n >> m;

	for(int i=0; i<n; i++){

		cin >> str[i];

		for(int j=0; j<m; j++){

			if(str[i][j] == 'P'){

				px = i;

				py = j;

			}

			if(str[i][j] == '#') sum[i+1][j+1]++;

		}

	}

	for(int i=1; i<=n; i++){

		for(int j=1; j<=m; j++){

			sum[i][j] += sum[i-1][j] + sum[i][j-1] - sum[i-1][j-1];

		}

	}

	memset(dp, -1, sizeof(dp));

	cout << f(0, n, 0, m) << endl;

}


// Optimized solution to the same problem (faster):
#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;

}

// Fastest known solution for this problem: