#include<bits/stdc++.h>
using namespace std;
int n, m, res;
char matrica[1007][1007];
bool posecenis[1007][1007];
bool poseceniv[1007][1007];
int dx[] = {-1, 1, 0, 0};
int dy[] = {0, 0, -1, 1};

struct polje {
    int x, y;
};

bool provera(int x, int y, int n, int m) {
    return x >= 0 && x < n && y >= 0 && y < m && matrica[x][y] == '.';
}

int main() {
    cin >> n >> m;
    queue<polje> steva, vatra;
    
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < m; ++j) {
            cin >> matrica[i][j];
            if (matrica[i][j] == 'X') {
                vatra.push({i, j});
                poseceniv[i][j] = true;
            } else if (matrica[i][j] == 'S') {
                steva.push({i, j});
                posecenis[i][j] = true;
                res++;
            }
        }
    }

    while (!steva.empty()) {
        int vatrasize = vatra.size();
        while (vatrasize--) {
            polje fire = vatra.front();
            vatra.pop();
            for (int d = 0; d < 4; ++d) {
                int nx = fire.x + dx[d];
                int ny = fire.y + dy[d];
                if (provera(nx, ny, n, m) && !poseceniv[nx][ny]) {
                    poseceniv[nx][ny] = true;
                    matrica[nx][ny] = 'X';
                    vatra.push({nx, ny});
                }
            }
        }

        int stevasize = steva.size();
        while (stevasize--) {
            polje Steva = steva.front();
            steva.pop();
            for (int d = 0; d < 4; ++d) {
                int nx = Steva.x + dx[d];
                int ny = Steva.y + dy[d];
                if (provera(nx, ny, n, m) && !posecenis[nx][ny] && !posecenis[nx][ny]) {
                    posecenis[nx][ny] = true;
                    res++;
                    matrica[nx][ny] = 'S';
                    steva.push({nx, ny});
                }
            }
        }
    }

    cout << res << endl;

    return 0;
}