#include <bits/stdc++.h>

using namespace std;

struct poljence{
    int i;
    int j;
    int potez;
};

queue <poljence> vatra_obrada;
queue <poljence> steva_obrada;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int n,m;
    cin>>n>>m;
    string* lavirint=new string[n];
    int** vatra_put=new int*[n];
    int** steva_put=new int*[n];
    int i,j;
    for(i=0;i<n;i++){
        cin>>lavirint[i];
        vatra_put[i]=new int[m];
        steva_put[i]=new int[m];
        for(j=0;j<m;j++){
            vatra_put[i][j]=-1;
            steva_put[i][j]=-1;
            if(lavirint[i][j]=='X'){
                vatra_obrada.push({i,j,0});
                vatra_put[i][j]=0;
            }
            else if(lavirint[i][j]=='S'){
                steva_obrada.push({i,j,0});
                steva_put[i][j]=0;
            }
        }
    }
    //cout<<"ok"<<endl;
    /*for(i=0;i<n;i++){
        for(j=0;j<m;j++){
            cout<<steva_put[i][j]<<" ";
        }
        cout<<endl;
    }*/
    poljence t;
    while(!vatra_obrada.empty()){
        t=vatra_obrada.front();
        vatra_obrada.pop();
        i=t.i;
        j=t.j;
        if(i>0 && lavirint[i-1][j]!='*' && vatra_put[i-1][j]==-1){
            vatra_put[i-1][j]=t.potez+1;
            vatra_obrada.push({i-1,j,t.potez+1});
        }
        if(i<n-1 && lavirint[i+1][j]!='*' && vatra_put[i+1][j]==-1){
            vatra_put[i+1][j]=t.potez+1;
            vatra_obrada.push({i+1,j,t.potez+1});
        }
        if(j>0 && lavirint[i][j-1]!='*' && vatra_put[i][j-1]==-1){
            vatra_put[i][j-1]=t.potez+1;
            vatra_obrada.push({i,j-1,t.potez+1});
        }
        if(j<m-1 && lavirint[i][j+1]!='*' && vatra_put[i][j+1]==-1){
            vatra_put[i][j+1]=t.potez+1;
            vatra_obrada.push({i,j+1,t.potez+1});
        }
    }

    while(!steva_obrada.empty()){
        t=steva_obrada.front();
        steva_obrada.pop();
        i=t.i;
        j=t.j;
        if(i>0 && lavirint[i-1][j]!='*' && steva_put[i-1][j]==-1){
            steva_put[i-1][j]=t.potez+1;
            steva_obrada.push({i-1,j,t.potez+1});
        }
        if(i<n-1 && lavirint[i+1][j]!='*' && steva_put[i+1][j]==-1){
            steva_put[i+1][j]=t.potez+1;
            steva_obrada.push({i+1,j,t.potez+1});
        }
        if(j>0 && lavirint[i][j-1]!='*' && steva_put[i][j-1]==-1){
            steva_put[i][j-1]=t.potez+1;
            steva_obrada.push({i,j-1,t.potez+1});
        }
        if(j<m-1 && lavirint[i][j+1]!='*' && steva_put[i][j+1]==-1){
            steva_put[i][j+1]=t.potez+1;
            steva_obrada.push({i,j+1,t.potez+1});
        }
    }

    /*for(i=0;i<n;i++){
        for(j=0;j<m;j++){
            cout<<steva_put[i][j]<<" ";
        }
        cout<<endl;
    }
    cout<<endl;
    for(i=0;i<n;i++){
        for(j=0;j<m;j++){
            cout<<vatra_put[i][j]<<" ";
        }
        cout<<endl;
    }
    cout<<endl;*/

    //cout<<"ok"<<endl;

    int tmp=0;
    //cout<<"do ovde sam ok?"<<endl;
    for(i=0;i<n;i++){
        //cout<<"nesto, molim te"<<endl;
        for(j=0;j<m;j++){

            //cout<<i<<" "<<j<<endl;
            if(lavirint[i][j]!='*' && steva_put[i][j]!=-1 && (steva_put[i][j]<vatra_put[i][j] || vatra_put[i][j]==-1)){
               tmp++;
            }
        }
        delete[] vatra_put[i];
        delete[] steva_put[i];
    }
    cout<<tmp<<endl;
    delete[] steva_put;
    delete[] vatra_put;
    delete[] lavirint;
    return 0;
}