#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;
    std::vector<char>stevaq;
    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')
            {
                int p=i;
                int q=j;
                stevap.push_back(p);
                stevaq.push_back(q);
            }
            if(matrica[i][j]=='X')
            {
                int k=i;
                int l=j;
                pozark.push_back(k);
                pozarl.push_back(l);
            }
            if(matrica[i][j]=='.')
            {
                tacke++;
            }
        }
    }
    while(tacke>0){
        int velicina=pozark.size();
        int velicina2=stevap.size();
        for(int i=0; i<velicina; i++){
            if(matrica[pozark[i]-1][pozarl[i]]=='.'){
                matrica[pozark[i]-1][pozarl[i]]='X';
                int k=pozark[i]-1;
                int l=pozarl[i];
                pozark.push_back(k);
                pozarl.push_back(l);
                tacke--;
            }
            else if(matrica[pozark[i]+1][pozarl[i]]=='.'){
                matrica[pozark[i]+1][pozarl[i]]='X';
                int k=pozark[i]+1;
                int l=pozarl[i];
                pozark.push_back(k);
                pozarl.push_back(l);
                tacke--;
            }
            else if(matrica[pozark[i]][pozarl[i]-1]=='.'){
                matrica[pozark[i]][pozarl[i]-1]='X';
                int k=pozark[i];
                int l=pozarl[i]-1;
                pozark.push_back(k);
                pozarl.push_back(l);
                tacke--;
            }
            else if(matrica[pozark[i]][pozarl[i]+1]=='.'){
                matrica[pozark[i]][pozarl[i]+1]='X';
                int k=pozark[i];
                int l=pozarl[i]+1;
                pozark.push_back(k);
                pozarl.push_back(l);
                tacke--;
            }
        }
        for(int i=0; i<velicina2; i++){
            if(matrica[stevap[i]-1][stevaq[i]]=='.'){
                matrica[stevap[i]-1][stevaq[i]]='S';
                int p=stevap[i]-1;
                int q=stevaq[i];
                stevap.push_back(p);
                stevaq.push_back(q);
                tacke--;
            }
            else if(matrica[stevap[i]+1][stevaq[i]]=='.'){
                matrica[stevap[i]+1][stevaq[i]]='S';
                int p=stevap[i]+1;
                int q=stevaq[i];
                stevap.push_back(p);
                stevaq.push_back(q);
                tacke--;
            }
            else if(matrica[stevap[i]][stevaq[i]-1]=='.'){
                matrica[stevap[i]][stevaq[i]-1]='S';
                int p=stevap[i];
                int q=stevaq[i]-1;
                stevap.push_back(p);
                stevaq.push_back(q);
                tacke--;
            }
            else if(matrica[stevap[i]][stevaq[i]+1]=='.'){
                matrica[stevap[i]][stevaq[i]+1]='S';
                int p=stevap[i];
                int q=stevaq[i]+1;
                stevap.push_back(p);
                stevaq.push_back(q);
                tacke--;
            }
        }
    }
    
        std::cout<<stevap.size();}
    
    
    return 0;
}