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

int main()
{
    unsigned short int N, M;
    unsigned int br = 1;
    unsigned int granicav, granicas;
    bool k = true;
    do{
        cin >> N >> M;
    }while(N > 1000 || M > 1000);
    vector<vector<char>> lavirint(N, vector<char>(M));
    vector<unsigned int> steva;
    vector<unsigned int> vatra;
    for(int i = 0; i < N; i++){
        for(int j = 0; j < M; j++){
            cin >> lavirint[i][j];
            if(lavirint[i][j] == 'S'){
                steva.push_back(i);
                steva.push_back(j);
                continue;
            }
            if(lavirint[i][j] == 'X'){
                vatra.push_back(i);
                vatra.push_back(j);
            }
        }
    }
    for(int i = 0; i < N; i++){
        for(int j = 0; j < M; j++){
            if(i == 0 && j == 0){
                cout << "usao" << '\n';
                granicav = vatra.size() / 2;
                for(int l = 0; l < granicav; l++){
                    cout << "usao" << '\n';
                    if(vatra[l] - 1 >= 0 && lavirint[vatra[l] - 1][vatra[l + 1]] == '.'){
                        lavirint[vatra[l] - 1][vatra[l + 1]] = 'X';
                        vatra.push_back(vatra[l] - 1);
                        vatra.push_back(vatra[l + 1]);
                        k = false;
                    }
                    if(vatra[l] + 1 < N && lavirint[vatra[l] + 1][vatra[l + 1]] == '.'){
                        lavirint[vatra[l] + 1][vatra[l + 1]] = 'X';
                        vatra.push_back(vatra[l] + 1);
                        vatra.push_back(vatra[l + 1]);
                        k = false;
                    }
                    if(vatra[l + 1] - 1 >= 0 && lavirint[vatra[l]][vatra[l + 1] - 1] == '.'){
                        lavirint[vatra[l]][vatra[l + 1] - 1] = 'X';
                        vatra.push_back(vatra[l]);
                        vatra.push_back(vatra[l + 1] - 1);
                        k = false;
                    }
                    if(vatra[l + 1] + 1 < M && lavirint[vatra[l]][vatra[l + 1] + 1] == '.'){
                        lavirint[vatra[l]][vatra[l + 1] + 1] = 'X';
                        vatra.push_back(vatra[l]);
                        vatra.push_back(vatra[l + 1] + 1);
                        k = false;
                    }
                }
            }
            else{
                cout << "usao" << '\n';
                granicas = steva.size() / 2;
                k = true;
                for(int l = 0; l < granicas; l++){
                    cout << "usao" << '\n';
                    if(steva[l] - 1 >= 0 && lavirint[steva[l] - 1][steva[l + 1]] == '.'){
                        lavirint[steva[l] - 1][steva[l + 1]] = 'S';
                        steva.push_back(steva[l] - 1);
                        steva.push_back(steva[l + 1]);
                        br++;
                        k = false;
                    }
                    if(steva[l] + 1 < N && lavirint[steva[l] + 1][steva[l + 1]] == '.'){
                        lavirint[steva[l] + 1][steva[l + 1]] = 'S';
                        steva.push_back(steva[l] + 1);
                        steva.push_back(steva[l + 1]);
                        br++;
                        k = false;
                    }
                    if(steva[l + 1] - 1 >= 0 && lavirint[steva[l]][steva[l + 1] - 1] == '.'){
                        lavirint[steva[l]][steva[l + 1] - 1] = 'S';
                        steva.push_back(steva[l]);
                        steva.push_back(steva[l + 1] - 1);
                        br++;
                        k = false;
                    }
                    if(steva[l + 1] + 1 < M && lavirint[steva[l]][steva[l + 1] + 1] == '.'){
                        lavirint[steva[l]][steva[l + 1] + 1] = 'S';
                        steva.push_back(steva[l]);
                        steva.push_back(steva[l + 1] + 1);
                        br++;
                        k = false;
                    }
                    if(k)
                        break;
                }
                if(k)
                    break;
                granicav = vatra.size() / 2;
                for(int l = 0; l < granicav; l++){
                    cout << "usao" << '\n';
                    if(vatra[l] - 1 >= 0 && lavirint[vatra[l] - 1][vatra[l + 1]] == '.'){
                        lavirint[vatra[l] - 1][vatra[l + 1]] = 'X';
                        vatra.push_back(vatra[l] - 1);
                        vatra.push_back(vatra[l + 1]);
                    }
                    if(vatra[l] + 1 < N && lavirint[vatra[l] + 1][vatra[l + 1]] == '.'){
                        lavirint[vatra[l] + 1][vatra[l + 1]] = 'X';
                        vatra.push_back(vatra[l] + 1);
                        vatra.push_back(vatra[l + 1]);
                    }
                    if(vatra[l + 1] - 1 >= 0 && lavirint[vatra[l]][vatra[l + 1] - 1] == '.'){
                        lavirint[vatra[l]][vatra[l + 1] - 1] = 'X';
                        vatra.push_back(vatra[l]);
                        vatra.push_back(vatra[l + 1] - 1);
                    }
                    if(vatra[l + 1] + 1 < M && lavirint[vatra[l]][vatra[l + 1] + 1] == '.'){
                        lavirint[vatra[l]][vatra[l + 1] + 1] = 'X';
                        vatra.push_back(vatra[l]);
                        vatra.push_back(vatra[l + 1] + 1);
                    }
                }
            }
            if(k)
                break;
        }
        if(k)
            break;
    }
    cout << "br = " << br;
}