#include <bits/stdc++.h>

char mat[1001][1001];
char map[1001][1001];
bool slobodna = true;
using coord = std::pair<int, int>;

void mvatra(coord& v, std::queue<coord>& vatra)
{
    if(v.first + 1 < 1000 && mat[v.first + 1][v.second] == '.')
    {
        mat[v.first + 1][v.second] = 'X';
        vatra.push({ v.first + 1, v.second });
    }
    if(v.first - 1 >= 0 && mat[v.first - 1][v.second] == '.')
    {
        mat[v.first - 1][v.second] = 'X';
        vatra.push({ v.first - 1, v.second });
    }
    if(v.second + 1 < 1000 && mat[v.first][v.second + 1] == '.')
    {
        mat[v.first][v.second + 1] = 'X';
        vatra.push({ v.first, v.second + 1 });
    }
    if(v.second - 1 >= 0 && mat[v.first][v.second - 1] == '.')
    {
        mat[v.first][v.second - 1] = 'X';
        vatra.push({ v.first, v.second - 1 });
    }
}

void msteva(coord& s, std::queue<coord>& steva, int& cnt, int& xc)
{
    bool f = false;
    if(s.first + 1 < 1000 && mat[s.first + 1][s.second] == '.' && !map[s.first + 1][s.second])
    {
        steva.push({ s.first + 1, s.second });
        map[s.first + 1][s.second] = 1;
        mat[s.first + 1][s.second] = 'S';
        cnt++;
        f = true;
    }
    if(s.first - 1 >= 0 && mat[s.first - 1][s.second] == '.' && !map[s.first - 1][s.second])
    {
        steva.push({ s.first - 1, s.second });
        map[s.first - 1][s.second] = 1;
        mat[s.first - 1][s.second] = 'S';
        cnt++;
        f = true;
    }
    if(s.second + 1 < 1000 && mat[s.first][s.second + 1] == '.' && !map[s.first][s.second + 1])
    {
        steva.push({ s.first, s.second + 1 });
        map[s.first][s.second + 1] = 1;
        mat[s.first][s.second + 1] = 'S';
        cnt++;
        f = true;
    }
    if(s.second - 1 >= 0 && mat[s.first][s.second - 1] == '.' && !map[s.first][s.second - 1])
    {
        steva.push({ s.first, s.second - 1 });
        map[s.first][s.second - 1] = 1;
        mat[s.first][s.second - 1] = 'S';
        cnt++;
        f = true;
    }

    if(!f)
        xc++;
}

int main()
{
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(NULL);

    memset(mat, 0, sizeof(mat));
    memset(map, 0, sizeof(map));

    std::queue<coord> vatra;
    std::queue<coord> steva;
    int count = 1, vatrac = 0, stevac = 1, stevacc, brojac;
    int N, M;
    std::cin >> N >> M;
    for(int i = 0; i < N; i++)
    {
        for(int j = 0; j < M; j++)
        {
            char c;
            std::cin >> c;
            mat[i][j] = c;
            if(c == 'S')
            {
                steva.push({ i, j });
                map[i][j] = 1;
            }
            else if(c == 'X')
            {
                vatra.push({ i, j });
                vatrac++;
            }
        }
    }

    while(slobodna)
    {
        while(vatrac > 0)
        {
            coord& v = vatra.front();
            vatra.pop();
            mvatra(v, vatra);
            vatrac--;
        }
        vatrac = vatra.size();

        brojac = 0;
        stevacc = stevac;
        while(stevac > 0)
        {
            coord& s = steva.front();
            steva.pop();
            msteva(s, steva, count, brojac);
            stevac--;
        }
        if(brojac == stevacc)
            slobodna = false;
        stevac = steva.size();
    }

    std::cout << count;

    return 0;
}