#define here cerr<<"===========================================\n"
#define dbg(x) cerr<<#x<<": "<<x<<endl;
#include <bits/stdc++.h>
#define llinf 100000000000000000LL // 10^17
#define iinf 2000000000 // 2*10^9
#define pb push_back
#define eb emplace_back
#define popb pop_back
#define fi first
#define sc second
#define endl '\n'
#define all(a) a.begin(),a.end()
#define ceri(a,l,r) {cerr<<#a<<": ";for(ll i_ = l;i_<=r;i_++) cerr<<a[i_]<< " ";cerr<<endl;}
#define cer(a) {cerr<<#a<<": ";for(ll x_ : a) cerr<<x_<< " ";cerr<<endl;}
#define si(a) (ll)(a.size())
using namespace std;
using ld = double;
using ll = long long;
using ull = unsigned long long;
using pii = pair<int,int>;
using pll = pair<ll,ll>;
using pld = pair<ld,ld>;

#define mod 1
ll add(ll x,ll y){
	x+=y;
	if(x<0){
		x%=mod;
		x+=mod;
	}else{
		if(x>=mod) x%=mod;
	}
	return x;
}
ll mul(ll a,ll b){
	a = add(a,0);
	b = add(b,0);
	ll ans = (a*b)%mod;
	if(ans<0) ans+=mod;
	return ans;
}


const ll maxn = 1005;
ll a[maxn][maxn];
bool vis[maxn][maxn];
ll d1[maxn][maxn];
ll d2[maxn][maxn];
ll n,m;
vector<ll> dx = {0,0,1,-1};
vector<ll> dy = {-1,1,0,0};
bool valid(ll x,ll y) {
	return x>=1&&x<=n&&y>=1&&y<=m;
}
/*

6 4
.S..
*.**
..**
....
.***
X...

 */
void tc(){
	cin >> n >> m;
	pll poc = {1,1};
	queue<pll> q;
	for(ll i = 1;i<=n;i++) for(ll j = 1;j<=m;j++) d1[i][j] = d2[i][j] = llinf;
	for(ll i = 1;i<=n;i++) {
		string s; cin >> s;
		for(ll j = 1;j<=m;j++) {
			char c = s[j-1];
			if(c=='*') a[i][j] = 1;
			if(c=='S') poc = {i,j},d2[i][j] = 0;
			if(c=='X') q.push({i,j}),vis[i][j] = 1,d1[i][j] = 0;
		}
	}
	while(si(q)) {
		pll p = q.front();
		q.pop();
		ll x = p.fi,y = p.sc;
		for(ll e = 0;e<4;e++) {
			ll nx = x+dx[e],ny = y+dy[e];
			if(!valid(nx,ny)) continue;
			if(a[nx][ny]) continue;
			if(vis[nx][ny]) continue;
			d1[nx][ny] = d1[x][y]+1;
			vis[nx][ny] = 1;
			q.push({nx,ny});
		}
	}
	for(ll i = 1;i<=n;i++) for(ll j = 1;j<=m;j++) vis[i][j] = 0;
	vis[poc.fi][poc.sc] = 1;
	q.push(poc);
	while(si(q)) {
		pll p = q.front();
		q.pop();
		ll x = p.fi,y = p.sc;
		for(ll e = 0;e<4;e++) {
			ll nx = x+dx[e],ny = y+dy[e];
			if(!valid(nx,ny)) continue;
			if(a[nx][ny]) continue;
			if(vis[nx][ny]) continue;
			d2[nx][ny] = d2[x][y]+1;
			vis[nx][ny] = 1;
			q.push({nx,ny});
		}
	}
	ll ans = 0;
	for(ll i = 1;i<=n;i++) {
		for(ll j = 1;j<=m;j++) {
			if(!a[i][j]&&d1[i][j]>d2[i][j]) ans++;
		}
	}
	cout<<ans<<endl;
}

int main(){
	ios_base::sync_with_stdio(false);cerr.tie(0);cout.tie(0);cin.tie(0);
	int t; t = 1;
	while(t--){
		tc();
	}
	return (0-0);
}