#include <bits/stdc++.h>

using namespace std;

int charInt(char a)
{
  if(a == 'S')
    return 0;
  else if(a == '.')
    return 1;
  else if(a == '*')
    return 2;
  else
    return 3;
}

int main()
{
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  
  int n, m;
  cin >> n >> m;
  
  vector<vector<int>> matrica(n, vector<int>(m));
  queue<pair<int, int>> vatre;
  queue<pair<int, int>> steve;
  
  pair<int, int> priv;
  pair<int, int> priv2;
  
  string ulaz;
  
  for(int i = 0; i < n; i++)
  {
    cin >> ulaz;
    for(int j = 0; j < m; j++)
    {
      matrica[i][j] = charInt(ulaz[j]);
      priv.first = i;
      priv.second = j;
      if(matrica[i][j] == 3)
        vatre.push(priv);
      else if(matrica[i][j] == 0)
        steve.push(priv);
    }
  }
  
  int smerI[] = {0, 0, 1, -1};
  int smerJ[] = {-1, 1, 0, 0};
  
  int brSteve = 1;
  int posBrSteve = 0;
  
  int lQSize = -1;
  int nQSize = -1;
  
  while(brSteve > posBrSteve)
  {
    posBrSteve = brSteve;
    
    nQSize = 0;
    
    while(vatre.size() != nQSize)
    {
      priv = vatre.front();
      vatre.pop();
      
      for(int i = 0; i < 4; i++)
      {
        int si = priv.first + smerI[i];
        int sj = priv.second + smerJ[i];
        
        if(si >= 0 && si < n && sj >= 0 && sj < m && matrica[si][sj] == 1)
        {
          priv2.first = si;
          priv2.second = sj;
          matrica[si][sj] = 3;
          vatre.push(priv2);
          nQSize++;
        }
      }
    }
    
    nQSize = 0;
    
    while(steve.size() != nQSize)
    {
      priv = steve.front();
      steve.pop();
      
      for(int i = 0; i < 4; i++)
      {
        int si = priv.first + smerI[i];
        int sj = priv.second + smerJ[i];
        
        if(si >= 0 && si < n && sj >= 0 && sj < m && matrica[si][sj] == 1)
        {
          priv2.first = si;
          priv2.second = sj;
          matrica[si][sj] = 0;
          steve.push(priv2);
          nQSize++;
          brSteve++;
        }
      }
    }
  }
  
  cout << brSteve;
  
  return 0;
}