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

const int BESKONACNO = 1e9;
const int dx[] = {-1, 1, 0, 0};
const int dy[] = {0, 0, -1, 1};

int main() {
    int n, m;
        cin >> n >> m;

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

                                vector<vector<int>> vreme_vatre(n, vector<int>(m, BESKONACNO));
                                    queue<pair<int, int>> red_vatre;
                                        pair<int, int> steva_pocetak;

                                            for (int i = 0; i < n; i++) {
                                                    for (int j = 0; j < m; j++) {
                                                                if (lavirint[i][j] == 'X') {
                                                                                red_vatre.push({i, j});
                                                                                                vreme_vatre[i][j] = 0;
                                                                                                            } else if (lavirint[i][j] == 'S') {
                                                                                                                            steva_pocetak = {i, j};
                                                                                                                                        }
                                                                                                                                                }
                                                                                                                                                    }

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

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

                                                                                                                                                                                                                    if (nx >= 0 && nx < n && ny >= 0 && ny < m && lavirint[nx][ny] == '.' && vreme_vatre[nx][ny] == BESKONACNO) {
                                                                                                                                                                                                                                    vreme_vatre[nx][ny] = vreme_vatre[x][y] + 1;
                                                                                                                                                                                                                                                    red_vatre.push({nx, ny});
                                                                                                                                                                                                                                                                }
                                                                                                                                                                                                                                                                        }
                                                                                                                                                                                                                                                                            }

                                                                                                                                                                                                                                                                                vector<vector<int>> vreme_steve(n, vector<int>(m, BESKONACNO));
                                                                                                                                                                                                                                                                                    queue<pair<int, int>> red_steve;

                                                                                                                                                                                                                                                                                        red_steve.push(steva_pocetak);
                                                                                                                                                                                                                                                                                            vreme_steve[steva_pocetak.first][steva_pocetak.second] = 0;

                                                                                                                                                                                                                                                                                                int broj_polja = 0;

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

                                                                                                                                                                                                                                                                                                                            if (vreme_vatre[x][y] > vreme_steve[x][y]) {
                                                                                                                                                                                                                                                                                                                                        broj_polja++;
                                                                                                                                                                                                                                                                                                                                                }

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

                                                                                                                                                                                                                                                                                                                                                                                            if (nx >= 0 && nx < n && ny >= 0 && ny < m && lavirint[nx][ny] == '.' && vreme_steve[nx][ny] == BESKONACNO) {
                                                                                                                                                                                                                                                                                                                                                                                                            vreme_steve[nx][ny] = vreme_steve[x][y] + 1;

                                                                                                                                                                                                                                                                                                                                                                                                                            if (vreme_steve[nx][ny] < vreme_vatre[nx][ny]) {
                                                                                                                                                                                                                                                                                                                                                                                                                                                red_steve.push({nx, ny});
                                                                                                                                                                                                                                                                                                                                                                                                                                                                }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                            }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        }

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            cout << broj_polja << endl;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                return 0;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                }