#include <bits/stdc++.h>
using namespace std;

const int N=1003;
char a[N][N];
int dS[N][N],dX[N][N],n,m;

bool ok(int x,int y){
	if(x<0||y<0||x>=n||y>=m)
		return 0;
	if(a[x][y]=='*')
		return 0;
	return 1;
}

vector<array<int,2>>make(int i,int j){
	vector<array<int,2>>ret;
	if(ok(i+1,j))
		ret.push_back({i+1,j});
	if(ok(i-1,j))
		ret.push_back({i-1,j});
	if(ok(i,j+1))
		ret.push_back({i,j+1});
	if(ok(i,j-1))
		ret.push_back({i,j-1});
	return ret;
}

signed main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    
    cin>>n>>m;
    for(int i=0;i<n;++i){
    	string s;
    	cin>>s;
    	for(int j=0;j<m;++j)
    		a[i][j]=s[j];
    }

    for(int i=0;i<n;++i){
    	for(int j=0;j<m;++j){
    		dS[i][j]=1e9;
    	}
    }

    vector<array<int,2>>X;
    array<int,2>S;

    for(int i=0;i<n;++i){
    	for(int j=0;j<m;++j){
    		if(a[i][j]=='X')
    			X.push_back({i,j});
    		if(a[i][j]=='S')
    			S={i,j};
    	}
    }

    queue<array<int,2>>q;
    dS[S[0]][S[1]]=0;
    q.push(S);

    while(!q.empty()){
    	int i=q.front()[0];
    	int j=q.front()[1];
    	q.pop();
    	vector<array<int,2>>anx=make(i,j);
    	for(int ii=0;ii<anx.size();++ii){
    		int nxi=anx[ii][0];
    		int nxj=anx[ii][1];
            if(dS[nxi][nxj]!=1e9)
                continue;
    		dS[nxi][nxj]=dS[i][j]+1;
    		q.push({nxi,nxj});
    	}
    }

    for(int i=0;i<n;++i){
    	for(int j=0;j<m;++j)
    		dX[i][j]=1e9;
    }

   	for(int ii=0;ii<X.size();++ii){
   		int i=X[ii][0];
   		int j=X[ii][1];
   		q.push({i,j});
   		dX[i][j]=0;
   	}

   	while(!q.empty()){
    	int i=q.front()[0];
    	int j=q.front()[1];
    	q.pop();
    	vector<array<int,2>>anx=make(i,j);
    	for(int ii=0;ii<anx.size();++ii){
    		int nxi=anx[ii][0];
    		int nxj=anx[ii][1];
            if(dX[nxi][nxj]!=1e9)
                continue;
    		dX[nxi][nxj]=dX[i][j]+1;
    		q.push({nxi,nxj});
    	}
    }

    int ans=0;
    for(int i=0;i<n;++i){
    	for(int j=0;j<m;++j){
    		if(a[i][j]!='*')
                ans+=dS[i][j]<dX[i][j];
    	}
    }

    cout<<ans;
}