#include <bits/stdc++.h>
#define ll long long
#define MAXN 1010
#define llinf 1000000000000000
using namespace std;
char a[MAXN][MAXN];
ll n,m,dists[MAXN][MAXN],distv[MAXN][MAXN];
bool Check(ll i,ll j)
{
    if (i>0 && j>0 && i<=n && j<=m && a[i][j]!='*')
        return true;
    return false;
}
int main()
{
    ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    ll si,sj;
    cin >> n >> m;
    for (ll i=1;i<=n;i++)
    {
        for (ll j=1;j<=m;j++)
        {
            cin >> a[i][j];
            dists[i][j]=llinf;
            distv[i][j]=llinf;
            if (a[i][j]=='S')
            {
                si=i;
                sj=j;
            }
        }
    }
    queue<pair<ll,ll> > q;
    dists[si][sj]=0;
    q.push({si,sj});
    while (!q.empty())
    {
        ll i=q.front().first,j=q.front().second;
        q.pop();
        if (Check(i-1,j) && dists[i-1][j]==llinf)
        {
            dists[i-1][j]=dists[i][j]+1;
            q.push({i-1,j});
        }
        if (Check(i+1,j) && dists[i+1][j]==llinf)
        {
            dists[i+1][j]=dists[i][j]+1;
            q.push({i+1,j});
        }
        if (Check(i,j-1) && dists[i][j-1]==llinf)
        {
            dists[i][j-1]=dists[i][j]+1;
            q.push({i,j-1});
        }
        if (Check(i,j+1) && dists[i][j+1]==llinf)
        {
            dists[i][j+1]=dists[i][j]+1;
            q.push({i,j+1});
        }
    }
    for (ll i=1;i<=n;i++)
    {
        for (ll j=1;j<=m;j++)
        {
            if (a[i][j]=='X')
            {
                distv[i][j]=0;
                q.push({i,j});
            }
        }
    }
    while (!q.empty())
    {
        ll i=q.front().first,j=q.front().second;
        q.pop();
        if (Check(i-1,j) && distv[i-1][j]==llinf)
        {
            distv[i-1][j]=distv[i][j]+1;
            q.push({i-1,j});
        }
        if (Check(i+1,j) && distv[i+1][j]==llinf)
        {
            distv[i+1][j]=distv[i][j]+1;
            q.push({i+1,j});
        }
        if (Check(i,j-1) && distv[i][j-1]==llinf)
        {
            distv[i][j-1]=distv[i][j]+1;
            q.push({i,j-1});
        }
        if (Check(i,j+1) && distv[i][j+1]==llinf)
        {
            distv[i][j+1]=distv[i][j]+1;
            q.push({i,j+1});
        }
    }
    ll ans=0;
    for (ll i=1;i<=n;i++)
    {
        for (ll j=1;j<=m;j++)
        {
            if (a[i][j]!='*' && dists[i][j]<distv[i][j])
                ans++;
        }
    }
    cout << ans;
    return 0;
}