#include<bits/stdc++.h>
#define int long long
using namespace std;
const int dx[4]={-1,0,1,0};
const int dy[4]={0,1,0,-1};
int n,m,cnt=1,maxn=0;
bool a[1010][1010],vis[1010][1010];
struct node
{
int x;
int y;
int f;
};
struct xy
{
int x;
int y;
};
vector<xy>s;
queue<node>q;
void bfs()
{
while(!q.empty())
{
node k=q.front();
q.pop();
if(q.empty()&&!s.empty())
{
maxn=cnt-1;
cnt=0;
q.push({s.back().x,s.back().y,cnt});
cnt++;
s.pop_back();
}
for(int i=0;i<4;i++)
{
int xx=k.x+dx[i];
int yy=k.y+dy[i];
if(xx>=1&&xx<=n&&yy>=1&&yy<=n&&a[xx][yy]&&!vis[xx][yy])
{
vis[xx][yy]=true;
q.push({xx,yy,cnt});
cnt++;
}
}
}
return;
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin>>n>>m;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
char c;
cin>>c;
if(c=='F')
{
a[i][j]=1;
if(q.empty())
{
q.push({i,j,cnt});
cnt+=1;
}
s.push_back({i,j});
}
else
{
a[i][j]=0;
}
}
}
bfs();
cout<<maxn*3;
return 0;
}