#include <bits/stdc++.h>
using namespace std;
char ch;
int N, M, px, py, a, sol, d[1005][1005];
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
struct slog{
    int x, y;
    bool tip;
    ///t - S, f - X;
};
queue <slog> Q;
void BFS(){
    while(Q.size())
    {
        slog temp = Q.front(), salji;
        if(!temp.tip){
            for(int i = 0; i < 4; i++){
                if(d[temp.x + dx[i]][temp.y + dy[i]] != 0 or (temp.x + dx[i] < 1 or temp.x + dx[i] > N) or ((temp.y + dy[i] < 1 or temp.y + dy[i] > M))) continue;
                d[temp.x + dx[i]][temp.y + dy[i]] = 1;
                salji.x = temp.x + dx[i];
                salji.y = temp.y + dy[i];
                salji.tip = false;
                Q.push(salji);

            }
        }else{
            for(int i = 0; i < 4; i++){
                if(d[temp.x + dx[i]][temp.y + dy[i]] != 0 or (temp.x + dx[i] < 1 or temp.x + dx[i] > N) or ((temp.y + dy[i] < 1 or temp.y + dy[i] > M))) continue;
                d[temp.x + dx[i]][temp.y + dy[i]] = d[temp.x][temp.y] + 1;
                salji.x = temp.x + dx[i];
                salji.y = temp.y + dy[i];
                salji.tip = true;
                sol++;
                Q.push(salji);

            }
        }

        Q.pop();
    }
}



int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cin >> N >> M;
    sol = 0;
    for(int i = 1; i <= N; i++){
        for(int j = 1; j <= M; j++){
            cin >> ch;
            if(ch == 'S') {
                px = i, py = j;
            }
            if(ch == 'X') {
                slog temp;
                temp.x = i;
                temp.y = j;
                temp.tip = false;
                Q.push(temp);
            }
            if(ch == '*') {
                d[i][j] = -1;
            }
            if(ch == '.') {
                d[i][j] = 0;
            }
        }
    }
    slog temp;
    temp.x = px;
    temp.y = py;
    temp.tip = true;
    Q.push(temp);
    BFS();
    cout << sol << endl;
return 0;
}