/**
 *    author:  tourist
 *    created: 25.11.2017 14:07:14       
**/
#include <bits/stdc++.h>

using namespace std;

string to_string(string s) {
  return '"' + s + '"';
}

string to_string(const char* s) {
  return to_string((string) s);
}

string to_string(bool b) {
  return (b ? "true" : "false");
}

template <typename A, typename B>
string to_string(pair<A, B> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}

template <typename A>
string to_string(A v) {
  bool first = true;
  string res = "{";
  for (const auto &x : v) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(x);
  }
  res += "}";
  return res;
}

void debug_out() { cerr << endl; }

template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}

#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif

const int inf = (int) 1e9;

const int N = 44;

char s[N][N];
int sum[N][N];
int f[N][N][N][N];

inline void update(int &a, int b) {
  if (b < a) {
    a = b;
  }
}

inline int get(int xa, int ya, int xb, int yb) {
  return sum[xb + 1][yb + 1] - sum[xa][yb + 1] - sum[xb + 1][ya] + sum[xa][ya];
}

inline bool inside(int xa, int ya, int xb, int yb, int xp, int yp) {
  return (xa <= xp && xp <= xb && ya <= yp && yp <= yb);
}

int main() {
  int h, w;
  scanf("%d %d", &h, &w);
  for (int i = 0; i < h; i++) {
    scanf("%s", s[i]);
  }
  int xp = -1, yp = -1;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (s[i][j] == 'P') {
        xp = i;
        yp = j;
      }
    }
  }
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      sum[i + 1][j + 1] = sum[i + 1][j] + sum[i][j + 1] - sum[i][j] + (int) (s[i][j] == '#');
    }
  }
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      for (int ii = i; ii < h; ii++) {
        for (int jj = j; jj < w; jj++) {
          f[i][j][ii][jj] = inf;
        }
      }
    }
  }
  f[0][0][h - 1][w - 1] = 0;
  int ans = inf;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      for (int ii = h - 1; ii >= i; ii--) {
        for (int jj = w - 1; jj >= j; jj--) {
          int ft = f[i][j][ii][jj];
          if (ft == inf) {
            continue;
          }
//          debug(i, j, ii, jj, ft);
          int cur = ft;
          if (i <= xp && xp <= ii && j <= yp && yp <= jj) {
            cur += min({get(i, j, xp, yp), get(i, yp, xp, jj), get(xp, j, ii, yp), get(xp, yp, ii, jj)});
          }
          if (cur == 12) {
            debug(i, j, ii, jj, cur);
          }
          ans = min(ans, cur);
/*          if (i < ii) {
            update(f[i + 1][j][ii][jj], ft + get(i, j, i, jj));
            update(f[i][j][ii - 1][jj], ft + get(ii, j, ii, jj));
          }
          if (j < jj) {
            update(f[i][j + 1][ii][jj], ft + get(i, j, ii, j));
            update(f[i][j][ii][jj - 1], ft + get(i, jj, ii, jj));
          }*/
          int all = get(i, j, ii, jj);
          for (int x = i; x <= ii + 1; x++) {
            for (int y = j; y <= jj + 1; y++) {
              if ((x == i || x == ii + 1) && (y == j || y == jj + 1)) {
                continue;
              }
              int here = get(i, j, x - 1, y - 1) + get(x, y, ii, jj);
              if (i < x && j < y && !inside(x, y, ii, jj, xp, yp)) {
                update(f[i][j][x - 1][y - 1], ft + (all - here));
              }
              if (x <= ii && y <= jj && !inside(i, j, x - 1, y - 1, xp, yp)) {
                update(f[x][y][ii][jj], ft + (all - here));
              }
              here = get(x, j, ii, y - 1) + get(i, y, x - 1, jj);
              if (x <= ii && j < y && !inside(i, y, x - 1, jj, xp, yp)) {
                update(f[x][j][ii][y - 1], ft + (all - here));
              }
              if (i < x && y <= jj && !inside(x, j, ii, y - 1, xp, yp)) {
                update(f[i][y][x - 1][jj], ft + (all - here));
              }
            }
          }
        }
      }
    }
  }
  printf("%d\n", ans);
  return 0;
}


Measured memory_bytes: 14592