#include <bits/stdc++.h>
char matrica[1005][1005];
int counter = 1;
int x, y;
std::queue<std::pair<int, int>> fire_q;
std::queue<std::pair<int, int>> player_q;
inline bool spread_fire_and_player();
inline void spread_fire();
int main()
{
    std::ios_base::sync_with_stdio(0);
    std::cin.tie(0);
    std::cin >> x >> y;
    for (int i = 0; i < x + 2; i++)
    {
        memset(matrica[i], '*', y + 2);
    }
    for (int i = 1; i <= x; i++)
    {
        for (int j = 1; j <= y; j++)
        {
            std::cin >> matrica[i][j];
        }
    }
    while (spread_fire_and_player())
    {
        int _ = 0;
    }

    std::cout << counter;
    return 0;
}
inline void spread_fire()
{
    for (int i = 1; i <= x; i++)
    {
        for (int j = 1; j <= y; j++)
        {
            if (matrica[i][j] == 'X')
            {
                fire_q.push({i, j});
            }
        }
    }
    while (!fire_q.empty())
    {
        int x = fire_q.front().first;
        int y = fire_q.front().second;
        fire_q.pop();
        if (matrica[x - 1][y] == '.')
        {
            matrica[x - 1][y] = 'X';
        }
        if (matrica[x + 1][y] == '.')
        {
            matrica[x + 1][y] = 'X';
        }
        if (matrica[x][y - 1] == '.')
        {
            matrica[x][y - 1] = 'X';
        }
        if (matrica[x][y + 1] == '.')
        {
            matrica[x][y + 1] = 'X';
        }
    }
}
inline bool spread_fire_and_player()
{
    spread_fire();
    bool success = false;
    for (int i = 1; i <= x; i++)
    {
        for (int j = 1; j <= y; j++)
        {
            if (matrica[i][j] == 'S')
            {
                player_q.push({i, j});
            }
        }
    }
    while (!player_q.empty())
    {
        int x = player_q.front().first;
        int y = player_q.front().second;
        player_q.pop();
        if (matrica[x - 1][y] == '.')
        {
            matrica[x - 1][y] = 'S';
            counter++;
            success = true;
        }
        if (matrica[x + 1][y] == '.')
        {
            matrica[x + 1][y] = 'S';
            counter++;
            success = true;
        }
        if (matrica[x][y - 1] == '.')
        {
            matrica[x][y - 1] = 'S';
            counter++;
            success = true;
        }
        if (matrica[x][y + 1] == '.')
        {
            matrica[x][y + 1] = 'S';
            counter++;
            success = true;
        }
    }
    return success;
}