#include <iostream>
#include <queue>
#include <vector>
using namespace std;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int n,m,sx,sy;

    queue<pair<int,int>> vq;
    cin >> n >> m;
    vector<vector<char>> mapa(n,vector<char>(m));
    vector<vector<int>> vt(n,vector<int>(m,-1));
    for(int i = 0; i < n;i++) {
        for(int j = 0; j < m;j++) {
            cin >> mapa[i][j];
            if(mapa[i][j] == 'S') {
                sx = i;sy=j;
            }
            else if(mapa[i][j] == 'X') {
                vq.push({i,j});
                vt[i][j] = 0;
            }
        }
    }


    int smerovi[4][2] = {{-1, 0}, {1, 0}, {0, 1}, {0, -1}};
    while(!vq.empty()) {
        pair<int,int> c = vq.front();
        for(auto& d : smerovi) {
            int nx = c.first + d[0],ny = c.second+d[1];
            if(nx >= 0 && nx < n && ny >= 0 && ny < m && vt[nx][ny] == -1 && mapa[nx][ny] != '*') {
                vq.push({nx,ny});
                vt[nx][ny] = vt[c.first][c.second]+1;
            }
        }
        vq.pop();
    }

    queue<pair<int,int>> sq;
    vector<vector<int>> vs(n,vector<int>(m,-1));
    int count = 1;
    sq.push({sx,sy});
    vs[sx][sy] = 0;
    while(!sq.empty()) {
        pair<int,int> c = sq.front();
        for(auto& d : smerovi) {
            int nx = c.first + d[0],ny = c.second+d[1];
            if(nx >= 0 && nx < n && ny >= 0 && ny < m && vs[nx][ny] == -1
                && mapa[nx][ny] != '*' && (vt[nx][ny] > vs[c.first][c.second]+1 || vt[nx][ny] == -1)) {
                sq.push({nx,ny});
                vs[nx][ny] = vs[c.first][c.second]+1;
                count++;
            }
        }
        sq.pop();
    }

    cout << count;


}