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

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

int main()
{
    int n, m, i, j, tacnapolja = 0, x, y, nx, ny;
    cin >> n >> m;

    vector<string> lavirint(n);
    vector<vector<int>> vremevatre(n, vector<int>(m, INT_MAX));
    vector<vector<int>> vremesteve(n, vector<int>(m, INT_MAX));

    pair<int, int> pozicijasteve;
    queue<pair<int, int>> vatraq;
    queue<pair<int, int>> stevaq;

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

        for (j = 0; j < m; j++)
        {
            if (lavirint[i][j] == 'S')
            {
                pozicijasteve = { i, j };
            }
            if (lavirint[i][j] == 'X')
            {
                vatraq.push({ i, j });
                vremevatre[i][j] = 0;
            }
        }
    }

    while (!vatraq.empty())
    {
        x = vatraq.front().first;
        y = vatraq.front().second;
        vatraq.pop();

        i = 0;
        while (i < 4)
        {
            nx = x + dx[i];
            ny = y + dy[i];
            if (nx >= 0 && nx < n && ny >= 0 && ny < m && lavirint[nx][ny] == '.' && vremevatre[nx][ny] == INT_MAX)
            {
                vremevatre[nx][ny] = vremevatre[x][y] + 1;
                vatraq.push({ nx, ny });
            }
            i++;
        }
    }

    stevaq.push(pozicijasteve);
    vremesteve[pozicijasteve.first][pozicijasteve.second] = 0;

    while (!stevaq.empty())
    {
        x = stevaq.front().first;
        y = stevaq.front().second;
        stevaq.pop();

        i = 0;
        while (i < 4)
        {
            nx = x + dx[i], ny = y + dy[i];

            if (nx >= 0 && nx < n && ny >= 0 && ny < m && lavirint[nx][ny] == '.' && vremesteve[nx][ny] == INT_MAX)
            {
                vremesteve[nx][ny] = vremesteve[x][y] + 1;
                stevaq.push({ nx, ny });
            }
            i++;
        }
    }

    for (i = 0; i < n; i++)
    {
        for (j = 0; j < m; j++)
        {
            if (lavirint[i][j] == '.' && vremesteve[i][j] < vremevatre[i][j])
            {
                tacnapolja++;
            }
        }
    }

    cout << tacnapolja + 1 << endl;

    return 0;
}