#include <iostream>
#include <vector>
#include <queue>

using namespace std;

const int INF = 1e9;  

int N, M;
vector<vector<char>> labirint; 
vector<vector<int>> vognja;  
vector<vector<int>> vsteve;  


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


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


    void oognja() {
        bool changed = true;
            while (changed) {
                    changed = false;
                            for (int i = 0; i < N; ++i) {
                                        for (int j = 0; j < M; ++j) {
                                                        if (vognja[i][j] == INF && labirint[i][j] != 'X') {

                                                                            for (int d = 0; d < 4; ++d) {
                                                                                                    int nx = i + dx[d], ny = j + dy[d];
                                                                                                                            if (inBounds(nx, ny) && vognja[nx][ny] != INF && vognja[nx][ny] == vognja[i][j] + 1) {
                                                                                                                                                        vognja[i][j] = vognja[nx][ny] + 1;
                                                                                                                                                                                    changed = true;
                                                                                                                                                                                                            }
                                                                                                                                                                                                                                }
                                                                                                                                                                                                                                                }
                                                                                                                                                                                                                                                            }
                                                                                                                                                                                                                                                                    }
                                                                                                                                                                                                                                                                        }
                                                                                                                                                                                                                                                                        }


                                                                                                                                                                                                                                                                        void dstiva() {
                                                                                                                                                                                                                                                                            bool changed = true;
                                                                                                                                                                                                                                                                                while (changed) {
                                                                                                                                                                                                                                                                                        changed = false;
                                                                                                                                                                                                                                                                                                for (int i = 0; i < N; ++i) {
                                                                                                                                                                                                                                                                                                            for (int j = 0; j < M; ++j) {
                                                                                                                                                                                                                                                                                                                            if (vsteve[i][j] == INF && labirint[i][j] == '.') {
                                                                                                                                                                                                                                                                                                                                                for (int d = 0; d < 4; ++d) {
                                                                                                                                                                                                                                                                                                                                                                        int nx = i + dx[d], ny = j + dy[d];
                                                                                                                                                                                                                                                                                                                                                                                                if (inBounds(nx, ny) && vsteve[nx][ny] < vsteve[i][j]) {
                                                                                                                                                                                                                                                                                                                                                                                                                            vsteve[i][j] = vsteve[nx][ny] + 1;
                                                                                                                                                                                                                                                                                                                                                                                                                                                        changed = true;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            }

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            int main() {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    cin >> N >> M;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        labirint.resize(N, vector<char>(M));
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            vognja.resize(N, vector<int>(M, INF));
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                vsteve.resize(N, vector<int>(M, INF));

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    int steve_x = -1, steve_y = -1;

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            for (int i = 0; i < N; ++i) {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    for (int j = 0; j < M; ++j) {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                cin >> labirint[i][j];
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            if (labirint[i][j] == 'S') {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            steve_x = i;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            steve_y = j;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            vsteve[i][j] = 0;  
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        } else if (labirint[i][j] == 'X') {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        vognja[i][j] = 0;  
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                }

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        oognja();

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                dstiva();

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        int count = 0;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            for (int i = 0; i < N; ++i) {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    for (int j = 0; j < M; ++j) {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                if (labirint[i][j] == '.' && vsteve[i][j] < vognja[i][j]) {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                count++;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        }

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            cout << count << endl;

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                return 0;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                }