#include <bits/stdc++.h>
using namespace std;

const int MAXN = 1010;
bool vatra[MAXN][MAXN];
bool steva[MAXN][MAXN];
bool zid[MAXN][MAXN];

queue<pair<int, int>> qVatra; 
queue<pair<int, int>> qSteva;

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

    char c;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            cin >> c;

            if (c == '.') continue;
            else if (c == 'S') {
                // steva
                steva[i][j] = true;
                qSteva.push({i,j});
            } 
            else if (c == '*') {
                // zid
                zid[i][j] = true;
            }
            else if (c == 'X') {
                // vatra
                vatra[i][j] = true;
                qVatra.push({i,j});
            }
        }
    }

    while(!qSteva.empty()) {
    //for (int p = 0; p < 8; p++) {
        // for (int i = 0; i < n; i++) {
        //     for (int j = 0; j < m; j++) {
        //         if (vatra[i][j]) cout << 'x';
        //         else if (steva[i][j]) cout << 's';
        //         else if (zid[i][j]) cout << '*';
        //         else cout << '.';
        //     }
        //     cout << endl;
        // }
        // cout << endl;


        int nVatra = qVatra.size();
        int nSteva = qSteva.size();

        for (int z = 0; z < nVatra; z++) {
            auto uv = qVatra.front();
            int i = uv.first;
            int j = uv.second;

            if (i > 0 && !zid[i-1][j] && !steva[i-1][j] && !vatra[i-1][j]) {
                vatra[i-1][j] = true;
                qVatra.push({i-1, j});
            }
            if (i < n-1 && !zid[i+1][j] && !steva[i+1][j] && !vatra[i+1][j]) {
                vatra[i+1][j] = true;
                qVatra.push({i+1, j});
            }
            if (j > 0 && !zid[i][j-1] && !steva[i][j-1] && !vatra[i][j-1]) {
                vatra[i][j-1] = true;
                qVatra.push({i, j-1});
            }
            if (j < m-1 && !zid[i][j+1] && !steva[i][j+1] && !vatra[i][j+1]) {
                vatra[i][j+1] = true;
                qVatra.push({i, j+1});
            }

            qVatra.pop();
        }

        for (int z = 0; z < nSteva; z++) {
            auto uv = qSteva.front();
            int i = uv.first;
            int j = uv.second;

            if (i > 0 && !zid[i-1][j] && !vatra[i-1][j] && !steva[i-1][j]) {
                steva[i-1][j] = true;
                qSteva.push({i-1, j});
            }
            if (i < n-1 && !zid[i+1][j] && !vatra[i+1][j] && !steva[i+1][j]) {
                steva[i+1][j] = true;
                qSteva.push({i+1, j});
            }
            if (j > 0 && !zid[i][j-1] && !vatra[i][j-1] && !steva[i][j-1]) {
                steva[i][j-1] = true;
                qSteva.push({i, j-1});
            }
            if (j < m-1 && !zid[i][j+1] && !vatra[i][j+1] && !steva[i][j+1]) {
                steva[i][j+1] = true;
                qSteva.push({i, j+1});
            }

            qSteva.pop();
        }
    }

    int ans = 0;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            if (steva[i][j]) ans++;
        }
    }
    cout << ans << endl;
    return 0;
}