#include <bits/stdc++.h>
#define maxn 1100
#define ll long long
using namespace std;
ll n, m;
vector<string> s(maxn);
vector<vector<ll>> vatra, steva;
bool provera(ll x, ll y) {
    return (x>=0 && y>=0 && x<n && y<m && s[x][y]!='*');
}
void ZaVatru(queue<pair<ll, ll>>& q) {
    while (!q.empty()) {
        pair<ll, ll> tr=q.front();
        q.pop();
        for (ll i=0; i<4; i++) {
            ll x=tr.first, y=tr.second;
            if (i==0) x++;
            if (i==1) x--;
            if (i==2) y++;
            if (i==3) y--;
            if (provera(x, y) && vatra[x][y]==(INT_MIN)) {
                vatra[x][y] = vatra[tr.first][tr.second]+1;
                q.push({x, y});
            }
        }
    }
}

void ZaStevu(pair<ll, ll> pocetak) {
    queue<pair<ll, ll>> q;
    q.push(pocetak);
    steva[pocetak.first][pocetak.second]=0;
    while (!q.empty()) {
        auto tr=q.front();
        q.pop();
        for (int i=0; i<4; i++) {
            int x=tr.first, y=tr.second;
            if (i==0) x++;
            if (i==1) x--;
            if (i==2) y++;
            if (i==3) y--;
            if (provera(x, y) && steva[x][y]==INT_MIN && (vatra[x][y]==INT_MIN || steva[tr.first][tr.second]+1<vatra[x][y])) {
                steva[x][y]=steva[tr.first][tr.second]+1;
                q.push({x, y});
            }
        }
    }
}

int main()
{
    cin.tie(0); cout.tie(0); ios::sync_with_stdio(0);
    cin >> n >> m;

    vatra.assign(n, vector<ll>(m, INT_MIN));
    steva.assign(n, vector<ll>(m, INT_MIN));

    queue<pair<ll, ll>> vatra_pocetak;
    pair<ll, ll> steva_pocetak;

    for (ll i=0; i<n; i++) {
        cin >> s[i];
        for (ll j=0; j<m; j++) {
            if (s[i][j]=='S') {
                steva_pocetak={i, j};
                steva[i][j]=0;
            }
            if (s[i][j]=='X') {
                vatra_pocetak.push({i, j});
                vatra[i][j]=0;
            }
        }
    }

    ZaVatru(vatra_pocetak);
    ZaStevu(steva_pocetak);

    ll br=0;
    for (ll i=0; i<n; i++) {
        for (ll j=0; j<m; j++) {
            if (s[i][j]=='.' && steva[i][j]!=(INT_MIN) &&
                (vatra[i][j]==(INT_MIN) || steva[i][j]<vatra[i][j])) {
                br++;
            }
        }
    }
    br++;
    cout << br << '\n';
    return 0;
}