#include<bits/stdc++.h>

using namespace std;

int funkcija(vector<string>& matrica, int n, int m) 
{
    queue<pair<int, int>> vatra, steva;
    vector<vector<int>> vreme(n, vector<int>(m, INT_MAX));
    pair<int, int> stevaPos;

    for (int i = 0; i < n; i++) 
    {
        for (int j = 0; j < m; j++) 
        {
            if (matrica[i][j] == 'X') 
            {
                vatra.push({i, j});
                vreme[i][j] = 0; 
            } 
            else if (matrica[i][j] == 'S') 
            {
                stevaPos = {i, j};
            }
        }
    }

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

        int x1 = x - 1, y1 = y;
        if(x1 >= 0 && matrica[x1][y1] == '.' && vreme[x1][y1] == INT_MAX)
        {
            vreme[x1][y1] = vreme[x][y] + 1; 
            vatra.push({x1, y1});
        }
        x1 = x; y1 = y + 1;
        if(y1 < m && matrica[x1][y1] == '.' && vreme[x1][y1] == INT_MAX)
        {
            vreme[x1][y1] = vreme[x][y] + 1; 
            vatra.push({x1, y1});
        }
        x1 = x + 1; y1 = y;
        if(x1 < n && matrica[x1][y1] == '.' && vreme[x1][y1] == INT_MAX)
        {
            vreme[x1][y1] = vreme[x][y] + 1; 
            vatra.push({x1, y1});
        }
        x1 = x; y1 = y - 1;
        if(y >=0 && matrica[x1][y1] == '.' && vreme[x1][y1] == INT_MAX)
        {
            vreme[x1][y1] = vreme[x][y] + 1; 
            vatra.push({x1, y1});
        }
    }

    steva.push({stevaPos.first, stevaPos.second});
    vector<vector<bool>> poseceni(n, vector<bool>(m, false));
    poseceni[stevaPos.first][stevaPos.second] = true;

    int cnt = 0;
    int korak = 0;

    while (!steva.empty()) 
    {
        int size = steva.size();
        for (int i = 0; i < size; i++) 
        {
            auto [x, y] = steva.front();
            steva.pop();

            cnt++;

            int x1 = x - 1, y1 = y;
            if(x1 >= 0 && matrica[x1][y1] == '.' && !poseceni[x1][y1] &&
                    matrica[x1][y1] == '.' && (korak + 1 < vreme[x1][y1]))
            {
                poseceni[x1][y1] = true;
                    steva.push({x1, y1});
            }
            x1 = x; y1 = y + 1;
            if(y1 < m && matrica[x1][y1] == '.' && !poseceni[x1][y1] &&
                    matrica[x1][y1] == '.' && (korak + 1 < vreme[x1][y1]))
            {
                poseceni[x1][y1] = true;
                    steva.push({x1, y1});
            }
            x1 = x + 1; y1 = y;
            if(x1 < n && matrica[x1][y1] == '.' && !poseceni[x1][y1] &&
                    matrica[x1][y1] == '.' && (korak + 1 < vreme[x1][y1]))
            {
                poseceni[x1][y1] = true;
                    steva.push({x1, y1});
            }
            x1 = x; y1 = y - 1;
            if(y >=0 && matrica[x1][y1] == '.' && !poseceni[x1][y1] && matrica[x1][y1] == '.' && (korak + 1 < vreme[x1][y1]))
            {
                poseceni[x1][y1] = true;
                steva.push({x1, y1});
            }
        }
        korak++;
    }

    return cnt;
}

int main() 
{
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);

    int n, m;
    cin >> n >> m;
    vector<string> matrica(n);
    
    for (int i = 0; i < n; i++) cin >> matrica[i];
    
    cout << funkcija(matrica, n, m);

    return 0;
}