#include <iostream>
#include <vector>
#include <queue>

using namespace std;

void bfs(vector<vector<int>>& matrica1, int start) {
    int n=matrica1.size(), m=matrica1[0].size(), br=0, pom, x, y, pomx, pomy;
    queue<int> q;
    q.push(start);
    while (!q.empty()) {
        pom=q.front();
        q.pop();
        x=pom/m;
        y=pom%m;
        vector<pair<int, int>> smerovi={{-1,0}, {1,0}, {0,-1}, {0,1}};
        for (auto smer : smerovi) {
            pomx=x+smer.first;
            pomy=y+smer.second;
            if (pomx>=0 && pomx<n && pomy>=0 && pomy<m && matrica1[pomx][pomy]!=-1 && pomx*m+pomy!=start) {
                if (matrica1[x][y]+1<matrica1[pomx][pomy] || (matrica1[pomx][pomy]==0 && pomx*m+pomy!=start)) {
                    if (matrica1[x][y]!=-1) matrica1[pomx][pomy]=matrica1[x][y]+1;
                    else matrica1[pomx][pomy]=1;
                    q.push(pomx*m+pomy);
                }
            }
        }
    }
}

int main() {
    int n, m, br=1, start;
    vector<int> vatre;
    char pom;
    scanf("%d %d", &n, &m);
    vector<vector<int>> matrica1(n, vector<int>(m, 0)), matrica2(n, vector<int>(m, 0));
    for (int i=0; i<n; i++) {
        for (int j=0; j<m; j++) {
            scanf(" %c", &pom);
            if (pom=='*') matrica1[i][j]=-1;
            else if (pom=='X') {
                matrica1[i][j]=-1;
                vatre.push_back(i*m+j);
            }
            else {
                matrica1[i][j]=0;
                if (pom=='S') start=i*m+j;
            }
        }
    }
    matrica2=matrica1;
    bfs(matrica1, start);
    if (vatre.size()>0) bfs(matrica2, vatre[0]);
    for (int i=1; i<vatre.size(); i++) bfs(matrica2, vatre[i]);
    for (int i=0; i<n; i++) {
        for (int j=0; j<m; j++) {
            if ((matrica1[i][j]<matrica2[i][j] || matrica2[i][j]==0) && matrica1[i][j]!=0) br++;
        }
    }
    printf("%d", br);
    return 0;
}