#include<bits/stdc++.h>
using namespace std;

int n, m, steve[1005][1005], vatre[1005][1005], br, x, y;
char  pocetna[1005][1005];
int dr[4]={0, -1, 0, 1};
int dc[4]={-1, 0, 1, 0};
queue<pair<int, int> >pvatra;
queue<pair<int, int> >qsteve;

void bfssteve(int r, int c){
    qsteve.push({r, c});
    while(!qsteve.empty()){
    pair<int, int> p = qsteve.front();
    r = p.first;
    c = p.second;
    qsteve.pop();
    for(int k=0; k<4; k++){
        if(steve[r+dr[k]][c+dc[k]]>1000000 and pocetna[r+dr[k]][c+dc[k]]!='*'){
            steve[r+dr[k]][c+dc[k]]=steve[r][c]+1;
            qsteve.push({r+dr[k], c+dc[k]});
            }
        }
    }
}

void bfsvatra(){

    while(!pvatra.empty()){
        pair<int, int> p = pvatra.front();
        int r = p.first;
        int c = p.second;
        pvatra.pop();
        for(int k=0; k<4; k++){
            if(vatre[r+dr[k]][c+dc[k]]>1000000 and pocetna[r+dr[k]][c+dc[k]]!='*'){
                vatre[r+dr[k]][c+dc[k]]=vatre[r][c]+1;
                pvatra.push({r+dr[k], c+dc[k]});
            }
        }
    }
}

int main()
{
    cin >> n >> m;
    memset(steve, 127, sizeof(steve));
    memset(pocetna, '*', sizeof(pocetna));
    memset(vatre, 127, sizeof(vatre));
    for(int i=1; i<=n; i++){
        for(int j=1; j<=m; j++){
            cin >> pocetna[i][j];
            if(pocetna[i][j]=='X'){
                pvatra.push({i, j});
                vatre[i][j]=0;
            }
            if(pocetna[i][j]=='S'){
                steve[i][j]=0;
                x=i;
                y=j;
            }
        }
    }
    bfssteve(x, y);
    bfsvatra();
    for(int i=1; i<=n; i++){
        for(int j=1; j<=m; j++){
            if(steve[i][j]<vatre[i][j]) br++;
        }
    }
    cout << br << endl;
    return 0;
}