#include"bits/stdc++.h"
using namespace std;
typedef long long ll;
template<class T1,class T2> bool cmin(T1 &x,const T2 &y) { if (y<x) { x=y; return 1; }return 0; }
template<class T1,class T2> bool cmax(T1 &x,const T2 &y) { if (x<y) { x=y; return 1; }return 0; }
#define all(x) (x).begin(),(x).end()
const int N=702;
int f[N][N][2];
int main()
{
ios::sync_with_stdio(0); cin.tie(0);
cout<<fixed<<setprecision(15);
int n,m,i,j;
cin>>n>>m;
vector<string> a(n);
for (auto &s:a) cin>>s;
memset(f,-0x3f,sizeof f);
f[0][1][1]=f[1][0][0]=0;
for (i=0; i<=n; i++) for (j=!i; j<=m; j++)
{
if (i<n)
{
cmax(f[i+1][j][0],max(f[i][j][0],f[i][j][1]+(j&&a[i][j-1]=='O')));
}
if (j<m)
{
cmax(f[i][j+1][1],max(f[i][j][0]+(i&&a[i-1][j]=='O'),f[i][j][1]));
}
}
int ans=max(f[n][m][0],f[n][m][1]);sadf
cerr<<ans*(4-acos(-1))<<endl;
cout<<10*(n+m-(4-acos(-1))*ans)<<endl;
}