#include <bits/stdc++.h>
using namespace std;
int main(int argc, char** argv) {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	
	int n,m;
	cin>>n>>m;
	vector<vector<char>> a(n+2,vector<char>(m+2,'*'));
	queue <pair<int,int>> p,s;
	vector<vector<int>> steva(n+2,vector<int>(m+2,-1));
	vector<vector<int>> pozar(n+2,vector<int>(m+2,-1));
	vector<vector<bool>> posecen_steva(n+2,vector<bool>(m+2,false));
	vector<vector<bool>> posecen_pozar(n+2,vector<bool>(m+2,false));
	for (int i=1;i<=n;i++){
		for (int j=1;j<=m;j++){
			
			cin>>a[i][j];
			if (a[i][j]=='S'){
				s.push(make_pair(i,j));
				steva[i][j]=0;
			}
			if (a[i][j]=='X'){
				p.push(make_pair(i,j));
				pozar[i][j]=0;
			}
		}
	}
	

	
	while(!s.empty()){
		pair<int,int> tr=s.front();
		int x=tr.first,y=tr.second;
		posecen_steva[x][y]=true;
		int dx[4]={0,0,1,-1},dy[4]={1,-1,0,0};
		for (int i=0;i<4;i++){
			
			if ((a[x+dx[i]][y+dy[i]] == '.') and (!posecen_steva[x+dx[i]][y+dy[i]])){
				posecen_steva[x+dx[i]][y+dy[i]]=true;
				steva[x+dx[i]][y+dy[i]]=steva[x][y]+1;
				s.push(make_pair(x+dx[i],y+dy[i]));
			}
			
		}
		s.pop();
	}
	
	while(!p.empty()){
		pair<int,int> tr=p.front();
		int x=tr.first,y=tr.second;
		posecen_pozar[x][y]=true;
		int dx[4]={0,0,1,-1},dy[4]={1,-1,0,0};
		for (int i=0;i<4;i++){
			
			if ((a[x+dx[i]][y+dy[i]] == '.') and (!posecen_pozar[x+dx[i]][y+dy[i]])){
				posecen_pozar[x+dx[i]][y+dy[i]]=true;
				pozar[x+dx[i]][y+dy[i]]=pozar[x][y]+1;
				p.push(make_pair(x+dx[i],y+dy[i]));
			}
			
		}
		p.pop();
	}	
	
/*	for (int i=1;i<=n;i++){
		for (int j=1;j<=m;j++){
			if (steva[i][j]!=-1) cout<<' ';
			cout<<steva[i][j]<<' ';
		}
		cout<<'\n';
	}
	cout<<'\n';
	for (int i=1;i<=n;i++){
		for (int j=1;j<=m;j++){
			if (pozar[i][j]!=-1) cout<<' ';
			cout<<pozar[i][j]<<' ';
		}
		cout<<'\n';
	}
*/	
	int rez=0;
	for (int i=1;i<=n;i++){
		for (int j=1;j<=m;j++){
			if ((steva[i][j]!=-1) and (pozar[i][j]==-1)) rez++;
			else if ((steva[i][j]<pozar[i][j]) and (steva[i][j]!=-1)) rez++;
		}
	}
	cout<<rez<<'\n';
	
	
	return 0;
}