#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e3+5;
const int inf = 2e9+11;


int n, m;
string a[MAXN];
vector<vector<int>> d, v;
queue<array<int,2>> q;

bool check(array<int,2> k, array<int,2> p, vector<vector<int>> &d){
	if(a[k[0]][k[1]]=='*' || d[k[0]][k[1]]-1<=d[p[0]][p[1]]) return 0;
	return 1;
}

void dfs(vector<vector<int>> &d){
	while(!q.empty()){
		auto p = q.front();
		q.pop();
		if(p[0]>0){
			auto k = p;
			k[0]--;
			if(check(k,p,d)){
				q.push(k);
				d[k[0]][k[1]] = d[p[0]][p[1]]+1;
			}
		}
		if(p[0]<n-1){
			auto k = p;
			k[0]++;
			if(check(k,p,d)){
				q.push(k);
				d[k[0]][k[1]] = d[p[0]][p[1]]+1;
			}
		}
		if(p[1]>0){
			auto k = p;
			k[1]--;
			if(check(k,p,d)){
				q.push(k);
				d[k[0]][k[1]] = d[p[0]][p[1]]+1;
			}
		}
		if(p[1]<m-1){
			auto k = p;
			k[1]++;
			if(check(k,p,d)){
				q.push(k);
				d[k[0]][k[1]] = d[p[0]][p[1]]+1;
			}
		}
	}
}

void solve(){
	cin >> n >> m;
	int x, y;
	d = v = vector<vector<int>>(n, vector<int>(m, inf));
	for(int i = 0; i < n; ++i){
		cin >> a[i];
		for(int j = 0; j < m; ++j){
			if(a[i][j]=='X'){
				q.push({i,j});
				d[i][j] = 0;
			}
			if(a[i][j]=='S'){
				x=i,y=j;
				v[i][j] = 0;
			}
		}
	}
	dfs(d);
	q.push({x,y});
	dfs(v);
	int br = 0;
	for(int i = 0; i < n; ++i){
		for(int j = 0; j < m; ++j){
			if(v[i][j] < d[i][j]) ++br;
		}
	}
	cout << br;
}

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