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

vector<vector<bool>> sl;
vector<vector<int>> t;
vector<vector<bool>> mk;
int res;

struct g {int x, y, t;};

void ss(int x, int y, int time){
    if(t[x][y] <= time) return;

    res++;
    mk[x][y] = true;

    if(sl[x-1][y] && !mk[x-1][y]) ss(x-1, y, time+1);
    if(sl[x+1][y] && !mk[x+1][y]) ss(x+1, y, time+1);
    if(sl[x][y-1] && !mk[x][y-1]) ss(x, y-1, time+1);
    if(sl[x][y+1] && !mk[x][y+1]) ss(x, y+1, time+1);
}

signed main(){
    ios_base::sync_with_stdio(0); cin.tie(0);
    int n, m; cin >> n >> m;
    sl = mk = vector<vector<bool>>(n+2, vector<bool>(m+2, false));
    t = vector<vector<int>>(n+2, vector<int>(m+2, INT_MAX));
    res = 0;
    int sx, sy;
    vector<pair<int,int>> v;
    for(int i = 1; i <= n; i++){
        for(int j = 1; j <= m; j++){
            char c; cin >> c;

            if(c == 'S'){
                sx = i;
                sy = j;
            }
            else if(c == 'X'){
                v.emplace_back(i, j);
                sl[i][j] = true;
            }
            else if(c == '.') sl[i][j] = true;
        }
    }

    queue<g> bfs;
    for(int i = 0; i < v.size(); i++) bfs.push({v[i].first, v[i].second, 0});

    while(!bfs.empty()){
        g cur = bfs.front();
        bfs.pop();
        if(mk[cur.x][cur.y] || t[cur.x][cur.y] <= cur.t) continue;

        mk[cur.x][cur.y] = true;
        t[cur.x][cur.y] = cur.t;
        if(sl[cur.x-1][cur.y] && !mk[cur.x-1][cur.y]) bfs.push({cur.x-1, cur.y, cur.t + 1});
        if(sl[cur.x+1][cur.y] && !mk[cur.x+1][cur.y]) bfs.push({cur.x+1, cur.y, cur.t + 1});
        if(sl[cur.x][cur.y-1] && !mk[cur.x][cur.y-1]) bfs.push({cur.x, cur.y-1, cur.t + 1});
        if(sl[cur.x][cur.y+1] && !mk[cur.x][cur.y+1]) bfs.push({cur.x, cur.y+1, cur.t + 1});
    }


    /*for(int i = 1; i <= n; i++){
        for(int j = 1; j <= m; j++){
            if(t[i][j] == INT_MAX) cout << "m ";
            else cout << t[i][j] << ' ';
        }
        cout << '\n';
    }*/


    mk = vector<vector<bool>>(n+2, vector<bool>(m+2, false));
    ss(sx, sy, 0);
    cout << res << '\n';
    return 0;
}