#include <algorithm>
#include <cstring>
#include <functional>
#include <iostream>
#define get(loc) / _3p[loc] % 3

using namespace std;
using tp = int32_t;
constexpr tp Hat_N = 103, Hat_M = 13, Hat_S = 59049;

bool state[Hat_N][Hat_M];
tp _3p[Hat_N], f[Hat_N][Hat_S], cp2[Hat_S];

void Get_Pow(tp m) {
  *_3p = 1;
  for (tp i = 0; i < m; ++i) {
    _3p[i + 1] = _3p[i] * 3;
  }
}

void Get_Num(tp m) {
  for (tp i = 0; i < _3p[m]; ++i) {
    for (tp j = 0; j < m; ++j) {
      cp2[i] += i get(j) == 2;
    }
  }
}

void Init(tp n, tp m) {
  for (tp i = 0; i < n; ++i) {
    for (tp j = 0; j < m; ++j) {
      char c;
      cin >> c;
      state[i][j] = c == 'P';
    }
  }
  Get_Pow(m);
  Get_Num(m);
  memset(f, -128, sizeof f);
  **f = 0;
}

void DP(tp i, tp m) {
  for (tp St = 0; St < _3p[m]; ++St) {
    if (f[i][St] < 0) {
      continue;
    }
    f[i][St] += cp2[St];
    function<void(tp, tp, tp)> Crt = [&](tp idx, tp s, tp last_loc) -> void {
      if (idx == m) {
        f[i + 1][s] = max(f[i][St], f[i + 1][s]);
        return;
      }
      if (St get(idx) == 2) {
        Crt(idx + 1, s + _3p[idx], last_loc);
      } else {
        Crt(idx + 1, s, last_loc);
        if (!(St get(idx)) && state[i][idx] && idx - last_loc > 2) {
          Crt(idx + 1, s + _3p[idx] * 2, idx);
        }
      }
    };
    Crt(0, 0, -3);
  }
}

signed main() {
  tp n, m;
  cin >> n >> m;
  Init(n, m);
  for (tp i = 0; i < n; ++i) {
    DP(i, m);
  }
  for (tp i = 0; i < _3p[m]; ++i) {
    f[n][i] += cp2[i];
  }
  printf("%d", *max_element(f[n], f[n] + _3p[m]));
  return 0;
}

/*#################################################################
#.................................................................#
#.............#......#............Created.By.RBTree...............#
#............#.#....#.#........... Limiting-Factor................#
#.............########............................................#
#............#........#..##############################...........#
#...........#..V........V..#..#........................#..#...#...#
#............#........#....#..........###..###..........#..#.#.#..#
#............#..X##X..#..#............#....#.#...........#..#...#.#
#...........#...N##N...#..#...........###..###..........#.........#
#.......MOO..#........#....#.#.#.#...................#.#..........#
#.............########.....#.#.#.##############.#.#..#.#..........#
#..........................#.#.#.#.............#.#.#.#.#..........#
#......#########...........#.#.#.#.................#.#.#..........#
#.....#.........#..........#.#.#.#.................#.#.#..........#
#.#.#.#G#R#A#S#S#.#.#......#.#.#.#.................#.#.#..........#
#.###################......#.#.#.#.................#.#.#..........#
#...........................#.#.#...................#.#...........#
#.................................................................#
#################################################################*/