#include<bits/stdc++.h>
#define inf 1e9
using namespace std;

bool validan(int i,int j,int n,int m){
    return i>=0&&j>=0&&i<n&&j<n;
}
void bfs1(int n,int m,vector<vector<char>>&mz,vector<vector<int>>&d1){
    queue<pair<int,int>>q;
    for(int i=0;i<n;++i){
        for(int j=0;j<m;++j){
            if(mz[i][j]=='X'){
                q.push({i,j});
                d1[i][j]=0;
            }
        }
    }
    while(!q.empty()){
        int i=q.front().first,j=q.front().second;
        q.pop();
        if(validan(i+1,j,n,m)&&d1[i+1][j]==inf&&mz[i+1][j]!='*'){
            q.push({i+1,j});
            d1[i+1][j]=d1[i][j]+1;
        }
        if(validan(i,j+1,n,m)&&d1[i][j+1]==inf&&mz[i][j+1]!='*'){
            q.push({i,j+1});
            d1[i][j+1]=d1[i][j]+1;
        }   
        if(validan(i-1,j,n,m)&&d1[i-1][j]==inf&&mz[i-1][j]!='*'){
            q.push({i-1,j});
            d1[i-1][j]=d1[i][j]+1;
        }
        if(validan(i,j-1,n,m)&&d1[i][j-1]==inf&&mz[i][j-1]!='*'){
            q.push({i,j-1});
            d1[i][j-1]=d1[i][j]+1;
        }
    }
}
void bfs2(int n,int m,vector<vector<char>>&mz,vector<vector<int>>&d2,int si,int sj){
    queue<pair<int,int>>q;
    q.push({si,sj});
    d2[si][sj]=0;
    while(!q.empty()){
        int i=q.front().first,j=q.front().second;
        q.pop();
        if(validan(i+1,j,n,m)&&d2[i+1][j]==inf&&mz[i+1][j]!='*'){
            q.push({i+1,j});
            d2[i+1][j]=d2[i][j]+1;
        }
        if(validan(i,j+1,n,m)&&d2[i][j+1]==inf&&mz[i][j+1]!='*'){
            q.push({i,j+1});
            d2[i][j+1]=d2[i][j]+1;
        }   
        if(validan(i-1,j,n,m)&&d2[i-1][j]==inf&&mz[i-1][j]!='*'){
            q.push({i-1,j});
            d2[i-1][j]=d2[i][j]+1;
        }
        if(validan(i,j-1,n,m)&&d2[i][j-1]==inf&&mz[i][j-1]!='*'){
            q.push({i,j-1});
            d2[i][j-1]=d2[i][j]+1;
        }
    }
}
int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n,m;
    cin>>n>>m;
    vector<vector<char>>mz(n,vector<char>(m));
    int si,sj;
    for(int i=0;i<n;++i){
        for(int j=0;j<m;++j){
            cin>>mz[i][j];
            if(mz[i][j]=='S')
                si=i,sj=j;
        }
    }
    vector<vector<int>>d1(n,vector<int>(m,inf)),d2(n,vector<int>(m,inf));
    bfs1(n,m,mz,d1);
    bfs2(n,m,mz,d2,si,sj);
    int ans=0;
    for(int i=0;i<n;++i)
        for(int j=0;j<m;++j)
            if(d1[i][j]>d2[i][j])
                ans++;
        
    cout<<ans<<'\n';
}