#include <iostream>
#include <string>
using namespace std;

bool Provera1(int i) {
    return (i - 1 >= 0);
}

bool Provera2(int i, int M) {
    return (i + 1 < M);
}

void IspisiMat(char** a, int N, int M) {
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < M; j++) {
            cout << a[i][j] << " ";
        }
        cout << endl;
    }
    cout << endl;
}

void FireTick(char** a, int N, int M) // Minecraft referenca xd. (Cita li iko ove komentare? Oduvek me je zanimalo (i da li je ovo komentar na komentar?))
{
    char** priv = new char* [N];

    for (int i = 0; i < N; ++i) {
        priv[i] = new char[M];
        for (int j = 0; j < M; ++j) {
            priv[i][j] = a[i][j];
        }
    }

    for (int i = 0; i < N; i++) 
    {
        for (int j = 0; j < M; j++) 
        {
            if (a[i][j] == 'X') 
            {
                if (Provera1(i) && priv[i - 1][j] != '*')
                    priv[i - 1][j] = 'X';
                if (Provera1(j) && priv[i][j - 1] != '*')
                    priv[i][j - 1] = 'X';
                if (Provera2(i, N) && priv[i + 1][j] != '*')
                    priv[i + 1][j] = 'X';
                if (Provera2(j, M) && priv[i][j + 1] != '*')
                    priv[i][j + 1] = 'X';
            }
        }
    }

    for (int i = 0; i < N; ++i) 
    {
        for (int j = 0; j < M; ++j) 
        {
            a[i][j] = priv[i][j];
        }
        delete[] priv[i];
    }
    delete[] priv;
}

void StevaTick(char** a, int N, int M, int& StevaTickBr)
{
    char** priv = new char* [N];
    for (int i = 0; i < N; ++i) {
        priv[i] = new char[M];
        for (int j = 0; j < M; ++j) {
            priv[i][j] = a[i][j];
        }
    }

    for (int i = 0; i < N; i++)
    {
        for (int j = 0; j < M; j++)
        {
            if (a[i][j] == 'S')
            {
                if (Provera1(i) && priv[i - 1][j] == '.')
                {
                    priv[i - 1][j] = 'S';
                    StevaTickBr++;
                }
                    
                if (Provera1(j) && priv[i][j - 1] == '.')
                {
                    priv[i][j - 1] = 'S';
                    StevaTickBr++;
                }
                    
                if (Provera2(i, N) && priv[i + 1][j] == '.')
                {
                    priv[i + 1][j] = 'S';
                    StevaTickBr++;
                }
                    
                if (Provera2(j, M) && priv[i][j + 1] == '.')
                {
                    priv[i][j + 1] = 'S';
                    StevaTickBr++;
                }
                    
            }
        }
    }

    for (int i = 0; i < N; ++i)
    {
        for (int j = 0; j < M; ++j)
        {
            a[i][j] = priv[i][j];
        }
        delete[] priv[i];
    }
    delete[] priv;
}

int BSP(char** a, int N, int M)
{
    int BrojStevaPolja = 0;
    for (int i = 0; i < N; i++)
    {
        for (int j = 0; j < M; j++)
        {
            if (a[i][j] == 'S')
            {
                BrojStevaPolja++;
            }
        }
    }
    return BrojStevaPolja;
}

bool StevaSirotinjaNemaVisePolja(char** a, int N, int M)
{
    int ProverenaStevaPolja = 0;
    for (int i = 0; i < N; i++)
    {
        for (int j = 0; j < M; j++)
        {
            if (a[i][j] == 'S')
            {
                if (Provera1(i) && a[i - 1][j] == '.')
                    ProverenaStevaPolja++;
                if (Provera1(j) && a[i][j - 1] == '.')
                    ProverenaStevaPolja++;
                if (Provera2(i, N) && a[i + 1][j] == '.')
                    ProverenaStevaPolja++;
                if (Provera2(j, M) && a[i][j + 1] == '.')
                    ProverenaStevaPolja++;
            }
        }
    }
    if (ProverenaStevaPolja == 0)
    {
        return false;
    }
    else
    {
        return true;
    }
}

int main() {
    int N, M;
    cin >> N >> M;

    char** a = new char* [N];
    for (int i = 0; i < N; ++i) 
    {
        a[i] = new char[M];
    }

    for (int i = 0; i < N; i++) 
    {
        string red;
        cin >> red;
        for (int j = 0; j < M; j++) 
        {
            a[i][j] = red[j];
        }
    }
    int StevaTickBr = 0;

    while (StevaSirotinjaNemaVisePolja(a, N, M))
    {
        FireTick(a, N, M);
        StevaTick(a, N, M, StevaTickBr);
    }
    cout << StevaTickBr+1;
    for (int i = 0; i < N; ++i) 
    {
        delete[] a[i];
    }
    delete[] a;

    return 0;
}