#include <bits/stdc++.h>


using namespace std;

// ?????????? ????? ?? ???????
const int dx[] = {-1, 1, 0, 0};
const int dy[] = {0, 0, -1, 1};

// ????????? ?? ?? ?? ?????????? ?????? ???????
bool inBounds(int x, int y, int N, int M) {
    return x >= 0 && x < N && y >= 0 && y < M;
    }

    int solveMaze(int N, int M, vector<string>& maze) {
        // ??????????? ????? ? ?????
            pair<int, int> steve_pos;
                vector<pair<int, int>> fire_positions;
                    for (int i = 0; i < N; i++) {
                            for (int j = 0; j < M; j++) {
                                        if (maze[i][j] == 'S') {
                                                        steve_pos = {i, j};
                                                                    } else if (maze[i][j] == 'X') {
                                                                                    fire_positions.push_back({i, j});
                                                                                                }
                                                                                                        }
                                                                                                            }

                                                                                                                // ?????? ?????
                                                                                                                    vector<vector<int>> fire_time(N, vector<int>(M, INT_MAX));
                                                                                                                        queue<pair<int, int>> fire_queue;

                                                                                                                            for (auto& fire : fire_positions) {
                                                                                                                                    fire_time[fire.first][fire.second] = 0;
                                                                                                                                            fire_queue.push(fire);
                                                                                                                                                }

                                                                                                                                                    while (!fire_queue.empty()) {
                                                                                                                                                            auto [x, y] = fire_queue.front();
                                                                                                                                                                    fire_queue.pop();

                                                                                                                                                                            for (int dir = 0; dir < 4; dir++) {
                                                                                                                                                                                        int nx = x + dx[dir];
                                                                                                                                                                                                    int ny = y + dy[dir];

                                                                                                                                                                                                                if (inBounds(nx, ny, N, M) && maze[nx][ny] != '*' && fire_time[nx][ny] == INT_MAX) {
                                                                                                                                                                                                                                fire_time[nx][ny] = fire_time[x][y] + 1;
                                                                                                                                                                                                                                                fire_queue.push({nx, ny});
                                                                                                                                                                                                                                                            }
                                                                                                                                                                                                                                                                    }
                                                                                                                                                                                                                                                                        }

                                                                                                                                                                                                                                                                            // ??????? ?????
                                                                                                                                                                                                                                                                                vector<vector<bool>> visited(N, vector<bool>(M, false));
                                                                                                                                                                                                                                                                                    queue<tuple<int, int, int>> steve_queue;
                                                                                                                                                                                                                                                                                        steve_queue.push({steve_pos.first, steve_pos.second, 0});
                                                                                                                                                                                                                                                                                            visited[steve_pos.first][steve_pos.second] = true;

                                                                                                                                                                                                                                                                                                int reachable_positions = 0;

                                                                                                                                                                                                                                                                                                    while (!steve_queue.empty()) {
                                                                                                                                                                                                                                                                                                            auto [x, y, time] = steve_queue.front();
                                                                                                                                                                                                                                                                                                                    steve_queue.pop();

                                                                                                                                                                                                                                                                                                                            // ??? ????? ???? ????? ??? ?????, ??????? ????
                                                                                                                                                                                                                                                                                                                                    if (time < fire_time[x][y]) {
                                                                                                                                                                                                                                                                                                                                                reachable_positions++;
                                                                                                                                                                                                                                                                                                                                                        }

                                                                                                                                                                                                                                                                                                                                                                for (int dir = 0; dir < 4; dir++) {
                                                                                                                                                                                                                                                                                                                                                                            int nx = x + dx[dir];
                                                                                                                                                                                                                                                                                                                                                                                        int ny = y + dy[dir];

                                                                                                                                                                                                                                                                                                                                                                                                    if (inBounds(nx, ny, N, M) && !visited[nx][ny] && maze[nx][ny] == '.' && time + 1 < fire_time[nx][ny]) {
                                                                                                                                                                                                                                                                                                                                                                                                                    visited[nx][ny] = true;
                                                                                                                                                                                                                                                                                                                                                                                                                                    steve_queue.push({nx, ny, time + 1});
                                                                                                                                                                                                                                                                                                                                                                                                                                                }
                                                                                                                                                                                                                                                                                                                                                                                                                                                        }
                                                                                                                                                                                                                                                                                                                                                                                                                                                            }

                                                                                                                                                                                                                                                                                                                                                                                                                                                                return reachable_positions;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                }

                                                                                                                                                                                                                                                                                                                                                                                                                                                                int main() {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                    int N, M;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                        cin >> N >> M;

                                                                                                                                                                                                                                                                                                                                                                                                                                                                            vector<string> maze(N);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                for (int i = 0; i < N; i++) {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        cin >> maze[i];
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            }

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                cout << solveMaze(N, M, maze) << endl;

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    return 0;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    }