#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;

#define ios ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define ll long long
#define ull unsigned long long
#define inf 1e15
#define llinf LLONG_MAX
#define mt make_tuple
#define mp make_pair
#define parll pair<ll,ll>
#define parii pair<int,int>
#define frs first
#define sec second
#define neparno(x) (x&1)
#define parno(x) (!neparno(x))
#define lowbit(x) (x&-x)
#define nzd __gcd
#define nzs(e1,e2) ((e1*e2)/nzd(e1,e2))
#define yesno(x) cout << ((x)?"Yes\n":"No\n")
#define condcout(x,y,z) cout << ((x)?y:z) << "\n"
#define all(V) V.begin(),V.end()
#define fill(V,v) fill(all(V),v)
#define l_b(BRL,brl) lower_bound( BRL.begin(), BRL.end(), brl )-BRL.begin()-1
#define u_b(BRU,bru) upper_bound( BRU.begin(), BRU.end(), bru )-BRU.begin()
#define mod 1000000007
#define mxn 500002

struct polje
{
    ll x,y,t;
};

ll n,m;
ll dx[] = {-1, 1, 0, 0};
ll dy[] = {0, 0, -1, 1};

bool Prazno( ll x, ll y, vector<string>& M )
{
    return (x >= 0 && y >= 0 && x < n && y < m && M[x][y] != '*');
}

int main()
{
    ios;

    cin >> n >> m;
    vector <string> M(n);
    for ( ll i = 0; i < n; i++ )
    {
        cin >> M[i];
    }

    ll sx,sy;
    queue <polje> VQ,SQ;
    vector < vector<ll> > VT(n,vector<ll>(m,-1)),ST(n,vector<ll>(m,-1));
    for ( ll i = 0; i < n; i++ )
    {
        for ( ll j = 0; j < m; j++ )
        {
            if ( M[i][j] == 'X' )
            {
                VQ.push({i,j,0});
                VT[i][j] = 0;
            }
            if ( M[i][j] == 'S' )
            {
                sx = i; sy = j;
                SQ.push({i,j,0});
                ST[i][j] = 0;
            }
        }
    }

    while ( !VQ.empty() )
    {
        ll x = VQ.front().x;
        ll y = VQ.front().y;
        ll t = VQ.front().t;
        VQ.pop();
        for ( ll i = 0; i < 4; i++ )
        {
            ll nx=x+dx[i],ny=y+dy[i];
            if ( Prazno(nx,ny,M) && VT[nx][ny] == -1 )
            {
                VT[nx][ny] = t+1;
                VQ.push({nx,ny,t+1});
            }
        }
    }

    ll rez = 0;
    while ( !SQ.empty() )
    {
        ll x = SQ.front().x;
        ll y = SQ.front().y;
        ll t = SQ.front().t;
        SQ.pop();
        if ( VT[x][y] == -1 || t < VT[x][y] )
        {
            rez++;
        }
        for ( ll i = 0; i < 4; i++ )
        {
            ll nx=x+dx[i],ny=y+dy[i];
            if ( Prazno(nx,ny,M) && ST[nx][ny] == -1 )
            {
                if ( VT[nx][ny] == -1 || t+1 < VT[nx][ny] )
                {
                    ST[nx][ny] = t+1;
                    SQ.push({nx,ny,t+1});
                }
            }
        }
    }
    cout << rez;

    return 0;
}