#include<bits/stdc++.h>
#define int long long
#define pii pair<int,int>
#define fi first
#define se second
using namespace std;
int dis[4][2]={{1,0},{0,1},{-1,0},{0,-1}};
int n,m;
bool UMatrici(int x,int y){
	return x>=0&&y>=0&&x<n&&y<m;
}
vector<vector<int>> memo;
vector<vector<bool>>vis;
vector<string> v;
void dfs(vector<pii> o){
	queue<tuple<int,int,int>> q;
	for(auto&x:o){
		q.emplace(x.fi,x.se,0ll);
	}
	while(!q.empty()){
		int x=0,y=0,z=0;
		tie(x,y,z)=q.front();q.pop();
		if(memo[x][y]<=z){continue;}
		memo[x][y]=z;
		for(int i=0;i<4;i++){
			int xx=x+dis[i][0],yy=y+dis[i][1];
			if(UMatrici(xx,yy)&&memo[xx][yy]>z+1&&v[xx][yy]!='*'){
				q.emplace(xx,yy,z+1);
			}
		}
	}
}
int dfs2(int x,int y){
	int u=0;
	queue<tuple<int,int,int>> q;
	q.emplace(x,y,0ll);
	while(!q.empty()){
		int z=0;
		tie(x,y,z)=q.front();q.pop();
		if(vis[x][y]){continue;}
		vis[x][y]=1;
		u+=memo[x][y]>z;
		for(int i=0;i<4;i++){
			int xx=x+dis[i][0],yy=y+dis[i][1];
			if(UMatrici(xx,yy)&&!vis[xx][yy]&&v[xx][yy]!='*'){
				q.emplace(xx,yy,z+1);
			}
		}
	}
	return u;
}
void solve() {
	cin>>n>>m;
	v.resize(n);
	memo.resize(n,vector<int>(m,1e9));
	vis.resize(n,vector<bool>(m,0));
	for(string&s:v){
		cin>>s;
	}
	int x=0,y=0;
	vector<pii> df;
	for(int i=0;i<n;i++){
		for(int j=0;j<m;j++){
			if(v[i][j]=='X'){
				df.emplace_back(i,j);
			}else if(v[i][j]=='S'){
				x=i,y=j;
			}
		}
	}
	dfs(df);
	cout<<dfs2(x,y)<<'\n';

}
int32_t main() {
	ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
	int32_t T=1;//cin>>T;
	while(T--) {
		solve();
	}
	return 0;
}