#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;
}
