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

int main()
{
    int n,m;
    scanf("%d",&n);
    scanf("%d",&m);
    char matrica[n][m];
    vector <pair<int,int>> vatra;
    int vatracp =0,stevacp =0;
    vector <pair<int,int>> steva;
    char read[m];
    for(int i=0;i<n;i++){
        scanf("%s",&read);
        for(int j=0;j<m;j++){
            matrica[i][j] = read[j];
            if(matrica[i][j] == 'X'){
                vatra.push_back({i,j});
            }
            if(matrica[i][j] == 'S'){
                steva.push_back({i,j});
            }
        }
    }
    while(stevacp<steva.size()){
        int vsize = vatra.size();
        for(vatracp;vatracp<vsize;vatracp++){
            if(vatra[vatracp].first - 1>=0){
                if(matrica[vatra[vatracp].first - 1][vatra[vatracp].second] == '.'){
                    matrica[vatra[vatracp].first - 1][vatra[vatracp].second] = 'X';
                    vatra.push_back({vatra[vatracp].first - 1,vatra[vatracp].second});
                }
            }
            if(vatra[vatracp].first + 1 <n){    
                if(matrica[vatra[vatracp].first + 1][vatra[vatracp].second] == '.'){
                    matrica[vatra[vatracp].first + 1][vatra[vatracp].second] = 'X';
                    vatra.push_back({vatra[vatracp].first + 1,vatra[vatracp].second});
                }
            }
            if(vatra[vatracp].second - 1>=0){
                if(matrica[vatra[vatracp].first][vatra[vatracp].second - 1] == '.'){
                    matrica[vatra[vatracp].first][vatra[vatracp].second - 1] = 'X';
                    vatra.push_back({vatra[vatracp].first,vatra[vatracp].second - 1});
                }
            }
            if(vatra[vatracp].second + 1 <m){    
                if(matrica[vatra[vatracp].first][vatra[vatracp].second + 1] == '.'){
                    matrica[vatra[vatracp].first][vatra[vatracp].second + 1] = 'X';
                    vatra.push_back({vatra[vatracp].first,vatra[vatracp].second + 1});
                }
            }
        }
        int ssize = steva.size();
        for(stevacp;stevacp<ssize;stevacp++){
            if(steva[stevacp].first-1>=0){
                if(matrica[steva[stevacp].first - 1][steva[stevacp].second] == '.'){
                    matrica[steva[stevacp].first - 1][steva[stevacp].second] = 'S';
                    steva.push_back({steva[stevacp].first - 1,steva[stevacp].second});
                }
            }
            if(steva[stevacp].first+1 <n) {  
                if(matrica[steva[stevacp].first + 1][steva[stevacp].second] == '.'){
                    matrica[steva[stevacp].first + 1][steva[stevacp].second] = 'S';
                    steva.push_back({steva[stevacp].first + 1,steva[stevacp].second});
                }
            }
            if(steva[stevacp].second-1>=0){
                if(matrica[steva[stevacp].first][steva[stevacp].second - 1] == '.'){
                    matrica[steva[stevacp].first][steva[stevacp].second - 1] = 'S';
                    steva.push_back({steva[stevacp].first,steva[stevacp].second - 1});
                }
            }
            if(steva[stevacp].second+1 <m){
                if(matrica[steva[stevacp].first][steva[stevacp].second + 1] == '.'){
                    matrica[steva[stevacp].first][steva[stevacp].second + 1] = 'S';
                    steva.push_back({steva[stevacp].first,steva[stevacp].second + 1});
                }
            }
        }

    }
    printf("%d",stevacp);
    return 0;
}