#include <iostream>
#include <vector>
int main()
{
    int n,m;
    std::cin>>n>>m;
    int tacke=0;
    char steva;
    std::vector<char>pozark;
    std::vector<char>pozarl;
    std::vector<char>stevap(1);
    std::vector<char>stevaq(1);
    std::vector<std::vector<char>>matrica(n,std::vector<char>(m));
    for(int i=0;i<n; i++){
        for(int j=0;j<m; j++){
            std::cin>>matrica[i][j];
            if(matrica[i][j]=='S')
            {
                stevap(0)=i;
                stevaq(0)=j;
            }
            if(matrica[i][j]=='X')
            {
                pozark.push_back(i);
                pozarl.push_back(j);
            }
            if(matrica[i][j]=='.')
            {
                tacke++;
            }
        }
    }
    while(tacke>0){
        int velicina=pozark.size();
        int velicina2=stevap.size();
        for(int i=0; i<velicina; i++){
            if(pozark[i]>0 && matrica[pozark[i]-1][pozarl[i]]=='.'){
                matrica[pozark[i]-1][pozarl[i]]='X';
                pozark.push_back(pozark[i]-1);
                pozarl.push_back(pozarl[i]);
                tacke--;
            }
            else if(pozark[i]<n-1 && matrica[pozark[i]+1][pozarl[i]]=='.'){
                matrica[pozark[i]+1][pozarl[i]]='X';
                pozark.push_back(pozark[i]+1);
                pozarl.push_back(pozarl[i]);
                tacke--;
            }
            else if(pozarl[i]>0 && matrica[pozark[i]][pozarl[i]-1]=='.'){
                matrica[pozark[i]][pozarl[i]-1]='X';
                pozark.push_back(pozark[i]);
                pozarl.push_back(pozarl[i]-1);
                tacke--;
            }
            else if(pozarl[i]<n-1 && matrica[pozark[i]][pozarl[i]+1]=='.'){
                matrica[pozark[i]][pozarl[i]+1]='X';
                pozark.push_back(pozark[i]);
                pozarl.push_back(pozarl[i]+1);
                tacke--;
            }
        }
        for(int i=0; i<velicina2; i++){
            if(stevap[i]>0 && matrica[stevap[i]-1][stevaq[i]]=='.'){
                matrica[stevap[i]-1][stevaq[i]]='S';
                stevap.push_back(stevap[i]-1);
                stevaq.push_back(stevaq[i]);
                tacke--;
            }
            else if(stevap[i]<n-1 && matrica[stevap[i]+1][stevaq[i]]=='.'){
                matrica[stevap[i]+1][stevaq[i]]='S';
                stevap.push_back(stevap[i]+1);
                stevaq.push_back(stevaq[i]);
                tacke--;
            }
            else if(stevaq[i]>0 && matrica[stevap[i]][stevaq[i]-1]=='.'){
                matrica[stevap[i]][stevaq[i]-1]='S';
                stevap.push_back(stevap[i]);
                stevaq.push_back(stevaq[i]-1);
                tacke--;
            }
            else if(stevaq[i]<n-1 && matrica[stevap[i]][stevaq[i]+1]=='.'){
                matrica[stevap[i]][stevaq[i]+1]='S';
                stevap.push_back(stevap[i]);
                stevaq.push_back(stevaq[i]+1);
                tacke--;
            }
        }
    }
    
        std::cout<<stevap.size();
    
    
    return 0;
}