#include<bits/stdc++.h>
using namespace std;
int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	int m,n;
	cin>>m>>n;
	char a[m][n];
	for(int i=0;i<m;i++){
		for(int j=0;j<n;j++){
			cin>>a[i][j];
		}
	}
	queue<pair<int, int>> qv;
	queue<pair<int, int>> qs;
	map<pair<int, int>, int> distv;
	map<pair<int, int>, int> dists; 
	for(int i=0;i<m;i++){
		for(int j=0;j<m;j++){
			if(a[i][j] == 'X'){
				qv.push({i,j});
				distv[{i,j}] = 0;
			}
			if(a[i][j] == 'S'){
				qs.push({i,j});
				dists[{i,j}] = 0;
			}              
		}
	}
	map<pair<int, int>, bool> visitedv;
	while(!qv.empty()){
		int x = qv.front().first;
		int y = qv.front().second;
		qv.pop();
		if(x+1<m && !visitedv[{x+1, y}] && a[x+1][y]!='*'){
			qv.push({x+1, y});
			distv[{x+1,y}] = distv[{x,y}]+1;
			visitedv[{x+1,y}] = 1;
		}
		if(x-1>=0 && !visitedv[{x-1, y}] && a[x-1][y]!='*'){
			qv.push({x-1, y});
			distv[{x-1,y}] = distv[{x,y}]+1;
			visitedv[{x-1,y}] = 1;
		}
		if(y-1>=0 && !visitedv[{x, y-1}] && a[x][y-1]!='*'){
			qv.push({x, y-1});
			distv[{x,y-1}] = distv[{x,y}]+1;
			visitedv[{x,y-1}] = 1;
		}
		if(y+1<n && !visitedv[{x, y+1}] && a[x][y+1]!='*'){
			qv.push({x, y+1});
			distv[{x,y+1}] = distv[{x,y}]+1;
			visitedv[{x,y+1}] = 1;
			//if(x==2 && y==0) cerr<<distv[{x, y+1}]<<endl;
		}
	}
	map<pair<int, int>, bool> visiteds;
	while(!qs.empty()){
		int x = qs.front().first;
		int y = qs.front().second;
		//cerr<<x<<" "<<y<<endl;
		//visiteds[qs.front()] = 1;
		qs.pop();
		if(x-1>=0 && !visiteds[{x-1, y}] && a[x-1][y]!='*'){
			qs.push({x-1, y});
			dists[{x-1,y}] = dists[{x,y}]+1;
			visiteds[{x-1,y}] = 1;
		}
		if(x+1<m && !visiteds[{x+1, y}] && a[x+1][y]!='*'){
			qs.push({x+1,y});
			dists[{x+1,y}] = dists[{x,y}]+1;
			visiteds[{x+1,y}] = 1;
		}
		if(y-1>=0 && !visiteds[{ x,y-1}] && a[x][y-1]!='*'){
			qs.push({x, y-1});
			dists[{x,y-1}] = dists[{x,y}]+1;
			visiteds[{x,y-1}] = 1;
		}
		if(y+1<n && !visiteds[{x, y+1}] && a[x][y+1]!='*'){
			qs.push({x, y+1});
			dists[{x,y+1}] = dists[{x,y}]+1;
			visiteds[{x,y+1}] = 1;
		}
	}
	int o = 0;
	for(int i=0;i<m;i++){
		for(int j=0;j<n;j++){
			if(dists[{i,j}]<distv[{i,j}] && a[i][j]!='*' && visiteds[{i,j}] && visitedv[{i,j}]) o++;
			else if(!visitedv[{i,j}] && visiteds[{i,j}]) o++;
		}
	}
	//cerr<<distv[{0,0}]<<endl;
	cout<<o<<endl;
}