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

using namespace std;

void bfs(vector<vector<int>>& matrica, vector<int> start) {
    unordered_map<int, bool> provera;
    int n=matrica.size(), m=matrica[0].size(), br=0, pom, x, y, pomx, pomy;
    queue<int> q;
    for (int s : start) {
        q.push(s);
        provera[s]=true;
    }
    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 && matrica[pomx][pomy]!=-1 && !provera[pomx*m+pomy]) {
                if (matrica[x][y]+1<matrica[pomx][pomy] || matrica[pomx][pomy]==0) {
                    if (matrica[x][y]!=-1) matrica[pomx][pomy]=matrica[x][y]+1;
                    else matrica[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);
    if (n==0 || m==0) {
        printf("%d", 0);
        return 0;
    }
    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});
    bfs(matrica2, vatre);
    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;
}