#include <bits/stdc++.h>
using namespace std;
int N,M,RES;
int STEVA[1005][1005],VUTRA[1005][1005];
int A1[5]={1,-1,0,0};
int YETTEL[5]={0,0,1,-1};
queue<int> QX,QY,qx,qy;
string s;
void BFS(int X,int Y){
    for(int i=0;i<=3;i++){
        if(STEVA[X+A1[i]][Y+YETTEL[i]] != -1 and STEVA[X][Y] +1 < STEVA[X+A1[i]][Y+YETTEL[i]] ){
            STEVA[X+A1[i]][Y+YETTEL[i]]=STEVA[X][Y]+1;
            QX.push(X+A1[i]);
            QY.push(Y+YETTEL[i]);
        }
    }
    QX.pop();
    QY.pop();
    for(int i=0;i<QX.size();i++){
        BFS(QX.front(),QY.front());
    }
    return ;
}
void bfs(int X,int Y){
    for(int i=0;i<=3;i++){
        if(VUTRA[X+A1[i]][Y+YETTEL[i]] != -1 and VUTRA[X][Y] +1 < VUTRA[X+A1[i]][Y+YETTEL[i]] ){
            VUTRA[X+A1[i]][Y+YETTEL[i]]=VUTRA[X][Y]+1;
            qx.push(X+A1[i]);
            qy.push(Y+YETTEL[i]);
        }
    }
    qx.pop();
    qy.pop();
    for(int i=0;i<qx.size();i++){
        bfs(qx.front(),qy.front());
    }
    return ;
}
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    scanf("%d %d",&N,&M);
    for(int i=1;i<=N;i++){
        for(int j=1;j<=M;j++){
            STEVA[i][j]=1000000;
            VUTRA[i][j]=1000000;
        }
    }
    for(int i=0;i<=N+1;i++){
        STEVA[i][0]=-1;
        STEVA[i][M+1]=-1;
        VUTRA[i][0]=-1;
        VUTRA[i][M+1]=-1;
    }
    for(int i=0;i<=M+1;i++){
        STEVA[0][i]=-1;
        STEVA[N+1][i]=-1;
        VUTRA[0][i]=-1;
        VUTRA[N+1][i]=-1;
    }

    for(int i=1;i<=N;i++){
            cin>>s;
        for(int j=0;j<s.size();j++){
            if(s[j]=='S'){
                STEVA[i][j+1]=0;
                QX.push(i);
                QY.push(j+1);
            }
            if(s[j]=='*'){
                STEVA[i][j+1]=-1;
                VUTRA[i][j+1]=-1;
            }
            if(s[j]=='X'){
                VUTRA[i][j+1]=0;
                qx.push(i);
                qy.push(j+1);

            }
        }
    }
    if(QX.size()!=0)BFS(QX.front(),QY.front());

    if(qx.size()!=0) bfs(qx.front(),qy.front());

    for(int i=1;i<=N;i++){
        for(int j=1;j<=M;j++){
            if(STEVA[i][j]<VUTRA[i][j])RES++;
        }
    }
    cout<<RES<<"\n";

 return 0;
}