#include <bits/stdc++.h>

using namespace std;

int n,m;
bool jelpostoji(pair<int,int> tacka) {
    return (tacka.first>=0 && tacka.first<n && tacka.second>=0 && tacka.second<m);
}

pair<int,int> translacija(pair<int,int> tacka, int strana) {
    if (strana == 0) {
        return make_pair(tacka.first+1, tacka.second);
    }
    if (strana == 1) {
        return make_pair(tacka.first-1, tacka.second);
    }
    if (strana == 2) {
        return make_pair(tacka.first, tacka.second+1);
    }
    return make_pair(tacka.first, tacka.second-1);
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cin>>n>>m;
    queue<pair<int,int> > vatre;
    queue<pair<int,int> > steva;
    int vutra[n][m];
    int seva[n][m];
    for (int i=0; i<n; i++) {
        string cur;
        cin>>cur;
        for (int j=0; j<m; j++) {
            if (cur[j]=='.') {
                vutra[i][j]=0;
                seva[i][j]=0;
            }
            if (cur[j]=='*') {
                vutra[i][j]=-1;
                seva[i][j]=-1;
            }
            if (cur[j]=='X') {
                vutra[i][j]=1;
                seva[i][j]=-1;
                vatre.push(make_pair(i,j));
            }
            if (cur[j]=='S') {
                vutra[i][j]=-1;
                seva[i][j]=1;
                steva.push(make_pair(i,j));
            }
        }
    }
    while (!steva.empty()) {
        pair<int,int> trenutniSteva = steva.front();
        steva.pop();
        for (int i=0; i<4; i++) {
            pair<int,int> transliran = translacija(trenutniSteva,i);
            if (jelpostoji(transliran) && seva[transliran.first][transliran.second]==0) {
                seva[transliran.first][transliran.second] = seva[trenutniSteva.first][trenutniSteva.second]+1;
                steva.push(transliran);
            }
        }
    }
    while (!vatre.empty()) {
        pair<int,int> trenutnaVatra = vatre.front();
        vatre.pop();
        for (int i=0; i<4; i++) {
            pair<int,int> transliran = translacija(trenutnaVatra,i);
            if (jelpostoji(transliran) && vutra[transliran.first][transliran.second]==0) {
                vutra[transliran.first][transliran.second] = vutra[trenutnaVatra.first][trenutnaVatra.second]+1;
                vatre.push(transliran);
            }
        }
    }
    int sukurina=1;
    for (int i=0; i<n; i++) {
        for (int j=0; j<m; j++) {
            if ((seva[i][j]<vutra[i][j] && seva[i][j]!=0 && seva[i][j]!=-1) || (vutra[i][j]==0 && seva[i][j]!=0)) {
                sukurina++;
            }
        }
    }
    cout<<sukurina;
    return 0;
}