#include<bits/stdc++.h>
using namespace std;
vector<pair<int, int>> sigmaVatra;
vector<pair<int, int>> sigmaStevan;
vector<string> unos;

int M, N;
int brojStevana = 1;

bool gotovo = false;

void prekalkulisiSigme(){
    for(int i = sigmaVatra.size()-1; i>-1; i--){
        bool gore = true, dole=true, levo=true, desno=true;
        if (sigmaVatra[i].first-1==-1){gore=false;}
        else if (unos[sigmaVatra[i].first-1][sigmaVatra[i].second] != '.'){gore=false;}
        
        if (sigmaVatra[i].first+1==M){dole=false;}
        else if (unos[sigmaVatra[i].first+1][sigmaVatra[i].second] != '.'){dole=false;}
        
        if (sigmaVatra[i].second-1==-1){levo=false;}
        else if (unos[sigmaVatra[i].first][sigmaVatra[i].second-1] != '.'){levo=false;}

        if (sigmaVatra[i].second+1==N){desno=false;}
        else if (unos[sigmaVatra[i].first][sigmaVatra[i].second+1] != '.'){desno=false;}
        
        if(!gore && !dole && !levo && !desno){
            sigmaVatra.erase(sigmaVatra.begin()+i);
        }

    }
    for(int i = sigmaStevan.size()-1; i>-1; i--){
        bool gore = true, dole=true, levo=true, desno=true;
        if (sigmaStevan[i].first-1==-1){gore=false;}
        else if (unos[sigmaStevan[i].first-1][sigmaStevan[i].second] != '.'){gore=false;}

        if (sigmaStevan[i].first+1==M){dole=false;}
        else if (unos[sigmaStevan[i].first+1][sigmaStevan[i].second] != '.'){dole=false;}

        if (sigmaStevan[i].second-1==-1){levo=false;}
        else if (unos[sigmaStevan[i].first][sigmaStevan[i].second-1] != '.'){levo=false;}

        if (sigmaStevan[i].second+1==N){desno=false;}
        else if (unos[sigmaStevan[i].first][sigmaStevan[i].second+1] != '.'){desno=false;}
        
        if(!gore && !dole && !levo && !desno){
            sigmaStevan.erase(sigmaStevan.begin()+i);
        }

    }
    return;

}

void kreirajNove(){
    for(int i = sigmaVatra.size()-1; i>-1; i--){
        bool gore = true, dole=true, levo=true, desno=true;
        if (sigmaVatra[i].first-1==-1){gore=false;}
        else if (unos[sigmaVatra[i].first-1][sigmaVatra[i].second] != '.'){gore=false;}
        else{
            unos[sigmaVatra[i].first-1][sigmaVatra[i].second] = 'X';
            sigmaVatra.push_back(make_pair(sigmaVatra[i].first-1, sigmaVatra[i].second));
        }

        if (sigmaVatra[i].first+1==M){dole=false;}
        else if (unos[sigmaVatra[i].first+1][sigmaVatra[i].second] != '.'){dole=false;}
        else{
            unos[sigmaVatra[i].first+1][sigmaVatra[i].second] = 'X';
            sigmaVatra.push_back(make_pair(sigmaVatra[i].first+1, sigmaVatra[i].second));
        }

        if (sigmaVatra[i].second-1==-1){levo=false;}
        else if (unos[sigmaVatra[i].first][sigmaVatra[i].second-1] != '.'){levo=false;}
        else{
            unos[sigmaVatra[i].first][sigmaVatra[i].second-1] = 'X';
            sigmaVatra.push_back(make_pair(sigmaVatra[i].first, sigmaVatra[i].second-1));
        }

        if (sigmaVatra[i].second+1==N){desno=false;}
        else if (unos[sigmaVatra[i].first][sigmaVatra[i].second+1] != '.'){desno=false;}
        else{
            unos[sigmaVatra[i].first][sigmaVatra[i].second+1] = 'X';
            sigmaVatra.push_back(make_pair(sigmaVatra[i].first, sigmaVatra[i].second+1));
        }
    }
    bool dosloDoPromene = false;
    for(int i = sigmaStevan.size()-1; i>-1; i--){
        bool gore = true, dole=true, levo=true, desno=true;
        if (sigmaStevan[i].first-1==-1){gore=false;}
        else if (unos[sigmaStevan[i].first-1][sigmaStevan[i].second] != '.'){gore=false;}
        else{
            unos[sigmaStevan[i].first-1][sigmaStevan[i].second] = 'S';
            sigmaStevan.push_back(make_pair(sigmaStevan[i].first-1, sigmaStevan[i].second));
            brojStevana+=1;
            dosloDoPromene=true;
        }

        if (sigmaStevan[i].first+1==M){dole=false;}
        else if (unos[sigmaStevan[i].first+1][sigmaStevan[i].second] != '.'){dole=false;}
        else{
            unos[sigmaStevan[i].first+1][sigmaStevan[i].second] = 'S';
            sigmaStevan.push_back(make_pair(sigmaStevan[i].first+1, sigmaStevan[i].second));
            brojStevana+=1;
            dosloDoPromene=true;
        }

        if (sigmaStevan[i].second-1==-1){levo=false;}
        else if (unos[sigmaStevan[i].first][sigmaStevan[i].second-1] != '.'){levo=false;}
        else{
            unos[sigmaStevan[i].first][sigmaStevan[i].second-1] = 'S';
            sigmaStevan.push_back(make_pair(sigmaStevan[i].first, sigmaStevan[i].second-1));
            brojStevana+=1;
            dosloDoPromene=true;
        }

        if (sigmaStevan[i].second+1==N){desno=false;}
        else if (unos[sigmaStevan[i].first][sigmaStevan[i].second+1] != '.'){desno=false;}
        else{
            unos[sigmaStevan[i].first][sigmaStevan[i].second+1] = 'S';
            sigmaStevan.push_back(make_pair(sigmaStevan[i].first, sigmaStevan[i].second+1));
            brojStevana+=1;
            dosloDoPromene=true;
        }

    }
    if(!dosloDoPromene){
        gotovo=true;
    }
    return;
}


int main(){
    cin.tie(NULL);
    ios_base::sync_with_stdio(false);
    
    cin>>M>>N;
    for(int i = 0; i<M; i++){
        string a;
        cin>>a;
        unos.push_back(a);
    }
    for(int i = 0; i<M; i++){
        for(int j = 0; j<N; j++){
            if (unos[i][j]=='S'){
                sigmaStevan.push_back(make_pair(i,j));
            }else if(unos[i][j]=='X'){
                sigmaVatra.push_back(make_pair(i,j));
            }
        }
    }
    prekalkulisiSigme();

    while(!gotovo){
        kreirajNove();
        prekalkulisiSigme();
    }
    cout<<brojStevana;
    return 0;
}