// NOI 2001 炮兵阵地(屎山代码)
#include <bits/stdc++.h>
using namespace std;
int f[20], dp[110][1<<10][1<<10];
int main() {
int n, m;
cin >> n >> m;
for (int i = 1; i <= n; i++) for (int j = m - 1; j >= 0; j--) {
char c;
cin >> c;
if (c == 'P') f[i] += (1 << j);
}
for (int s = 0; s < (1 << m); s++) dp[1][s][0] = __builtin_popcount(s);
for (int i = 2; i <= n; i++) {
for (int s = f[i]; ; s = (s - 1) & f[i]) {
if ((s & (s << 1)) != 0 || (s & (s << 2)) != 0) continue;
int t = ((~s) & f[i-1]);
for (int p = t; ; p = (p - 1) & t) {
if ((p & (p << 1)) || (p & (p << 2))) continue;
int tt = ((~s) & (~p) & f[i-2]);
for (int pp = tt; ; pp = (pp-1) & tt) {
if ((pp & (pp << 1)) || (pp & (pp << 2))) continue;
dp[i][s][p] = max(dp[i][s][p], dp[i-1][p][pp]);
if (pp == 0) break;
}
dp[i][s][p] += __builtin_popcount(s);
if (p == 0) break;
}
if (s == 0) break;
}
}
int ans = -1;
for (int i = 0; i < (1 << m); i++) {
for (int j = 0; j < (1 << m); j++) ans = max(ans, dp[n][i][j]);
}
cout << ans << endl;
return 0;
}