#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <queue>
#define ll long long

using namespace std;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    ll n, m;
    char c;
    cin>>n>>m;
    vector<vector<char>>mat(n, vector<char>(m));
    vector<vector<ll>>steva(n, vector<ll>(m));
    vector<vector<ll>>vatra(n, vector<ll>(m, 1000001));
    queue<pair<ll,ll>>q;

    ll Si,Sj, brv=0;
    vector<pair<ll,ll>>v;

    for(ll i=0; i<n; i++){
        for(ll j=0; j<m; j++){
            cin>>mat[i][j];
            if(mat[i][j]=='S'){
                Si=i; Sj=j;
            }
            if(mat[i][j]=='X'){
                v.push_back(make_pair(i,j));
                brv+=1;
            }
        }
    }

   // cout<<"ciguligu"<<endl;



    //bfs za vatru
    vector<vector<bool>>pos2(n,vector<bool>(m,false));
    for (ll i=0; i<brv; i++){
        q.push(make_pair(v[i].first,v[i].second));
        vatra[v[i].first][v[i].second]=0;
        pos2[v[i].first][v[i].second] = true;
    }
    while (!q.empty()){
        pair<ll, ll> p = q.front(); q.pop();
        ll x = p.first;
        ll y = p.second;

        if (x+1<n && mat[x+1][y]!='*'){
                if (pos2[x+1][y]){
                    if(vatra[x][y]+1<vatra[x+1][y]) q.push(make_pair(x+1,y));
                }
                else{
                    vatra[x+1][y]=min(vatra[x][y]+1,vatra[x+1][y]);
                    q.push(make_pair(x+1,y));
                    pos2[x+1][y]=true;
                }
        }

        if (x-1>-1 && mat[x-1][y]!='*'){
                if (pos2[x-1][y]){
                    if(vatra[x][y]+1<vatra[x-1][y]) q.push(make_pair(x-1,y));
                }
                else{
                    vatra[x-1][y]=min(vatra[x][y]+1, vatra[x-1][y]);
                    q.push(make_pair(x-1,y));
                    pos2[x-1][y]=true;
                }
        }

        if (y+1<m && mat[x][y+1]!='*'){
                if (pos2[x][y+1]){
                    if(vatra[x][y]+1<vatra[x][y+1]) q.push(make_pair(x,y+1));
                }
                else{
                    vatra[x][y+1]=vatra[x][y]+1;
                    q.push(make_pair(x,y+1));
                    pos2[x][y+1]=true;
                }
        }

        if (y-1>-1 && mat[x][y-1]!='*'){
                if (pos2[x][y-1]){
                    if(vatra[x][y]+1<vatra[x][y-1]) q.push(make_pair(x,y-1));
                }
                else{
                    vatra[x][y-1]=vatra[x][y]+1;
                    q.push(make_pair(x,y-1));
                    pos2[x][y-1]=true;
                }

            }
        }


    //cout<<"pa ti se bodes"<<endl;



    ll reShy=1;
    //bfs za Stevu
    vector<vector<bool>>pos1(n,vector<bool>(m,false));

    steva[Si][Sj]=0;
    q.push(make_pair(Si,Sj));
    while (!q.empty()){
        pair<ll, ll> p = q.front(); q.pop();
        ll x = p.first;
        ll y = p.second;
        pos1[x][y] = true;
        if (x + 1 < n && mat[x + 1][y] != '*' && !pos1[x + 1][y]){
                steva[x+1][y]=steva[x][y]+1;
                q.push(make_pair(x+1,y));
                pos1[x+1][y]=true;

                if(steva[x+1][y]<vatra[x+1][y]) reShy+=1;
        }
        if (x - 1 >= 0 && mat[x - 1][y] != '*' && !pos1[x - 1][y]){
                steva[x-1][y]=steva[x][y]+1;
                q.push(make_pair(x-1,y));
                pos1[x-1][y]=true;

                if(steva[x-1][y]<vatra[x-1][y]) reShy+=1;
        }
        if (y + 1 < m && mat[x][y + 1] != '*' && !pos1[x][y + 1]){
                steva[x][y+1]=steva[x][y]+1;
                q.push(make_pair(x,y+1));
                pos1[x][y+1]=true;

                if(steva[x][y+1]<vatra[x][y+1]) reShy+=1;
        }
        if (y - 1 >= 0 && mat[x][y - 1] != '*' && !pos1[x][y - 1]){
                steva[x][y-1]=steva[x][y]+1;
                q.push(make_pair(x,y-1));
                pos1[x][y-1]=true;

                if(steva[x][y-1]<vatra[x][y-1]) reShy+=1;
        }
    }

    //cout<<"joleleihu"<<endl;

       cout<<reShy;


}

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

*/