/*
状态压缩DP
采用1038的方法
*/
// include file
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cctype>
#include <ctime>
#include <iostream>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <bitset>
#include <algorithm>
#include <string>
#include <vector>
#include <queue>
#include <set>
#include <list>
#include <functional>
using namespace std;
// typedef
typedef long long LL;
typedef unsigned long long ULL;
//
#define read freopen("in.txt","r",stdin)
#define write freopen("out.txt","w",stdout)
#define FORi(a,b,c) for(int i=(a);i<(b);i+=c)
#define FORj(a,b,c) for(int j=(a);j<(b);j+=c)
#define FORk(a,b,c) for(int k=(a);k<(b);k+=c)
#define FORp(a,b,c) for(int p=(a);p<(b);p+=c)
#define FORii(a,b,c) for(int ii=(a);ii<(b);ii+=c)
#define FORjj(a,b,c) for(int jj=(a);jj<(b);jj+=c)
#define FORkk(a,b,c) for(int kk=(a);kk<(b);kk+=c)
#define FF(i,a) for(int i=0;i<(a);i++)
#define FFD(i,a) for(int i=(a)-1;i>=0;i--)
#define Z(a) (a<<1)
#define Y(a) (a>>1)
const double eps = 1e-6;
const double INFf = 1e10;
const int INFi = 1000000000;
const double Pi = acos(-1.0);
template<class T> inline T sqr(T a){return a*a;}
template<class T> inline T TMAX(T x,T y)
{
if(x>y) return x;
return y;
}
template<class T> inline T TMIN(T x,T y)
{
if(x<y) return x;
return y;
}
template<class T> inline void SWAP(T &x,T &y)
{
T t = x;
x = y;
y = t;
}
template<class T> inline T MMAX(T x,T y,T z)
{
return TMAX(TMAX(x,y),z);
}
// code begin
int N,M;
char mp[110][12];
int dp[2][59050];
int prerow[12];
int currow[12];
int preten;
int curten;
int san[12];
int ret;
int tmp[5];
void Init()
{
san[0] = 1;
FORi(1,11,1)
san[i] = san[i-1]*3;
}
void san2ten(int A[],int &ans)
{
ans = 0;
FORi(0,M,1)
{
ans = ans*3+A[i];
}
}
void ten2san(int ten,int A[])
{
for(int i=M-1;i>=0;i--)
{
A[i] = ten%3;
ten/=3;
}
}
void dfs(int r,int c,int v,int cs)
{
if(c>=M)
{
dp[r&1][cs] = TMAX(dp[r&1][cs],dp[(r+1)&1][preten]+v);
if(dp[r&1][cs]>ret)
ret = dp[r&1][cs];
return;
}
if( currow[c]==0 && prerow[c]==0 && mp[r][c]=='P' )
{
currow[c] = 2;
cs += currow[c]*san[M-c-1];
dfs(r,c+3,v+1,cs);
cs -= currow[c]*san[M-c-1];
currow[c] = 0;
}
dfs(r,c+1,v,cs);
}
int main()
{
read;
write;
Init();
while(scanf("%d %d",&N,&M)!=-1)
{
FORi(1,N+1,1)
{
scanf("%s",mp[i]);
}
// 0之前和现在都是0
// 1当前是0,之前是1
// 2当前是1
FORi(0,M,1)
prerow[i] = 0;
preten = 0;
memset(dp,-1,sizeof(dp));
dp[0][preten] = 0;
ret = 0;
FORi(1,N+1,1)
{
memset(dp[i&1],-1,sizeof(dp[i&1]));
FORj(0,san[M],1)
{
if( dp[(i+1)&1][j]==-1 )
continue;
ten2san(j,prerow);
//
FORk(0,M,1)
{
if(prerow[k]==2)
{
currow[k] = 1;
}
else if(prerow[k]==1)
{
currow[k] = 0;
}
else
currow[k] = 0;
}
san2ten(currow,curten);
preten = j;
//
dfs(i,0,0,curten);
}
}
printf("%d\n",ret);
}
return 0;
}