#include <bits/stdc++.h>
using namespace std;
pair<int, int> unos(vector<vector<char>> &matrica, int n, int m)
{
    int x, y;
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            cin >> matrica[i][j];
            if(matrica[i][j]=='S')
            {
                x=i;
                y=j;
            }
        }
    }
    return make_pair(x, y);
}
void stampaj(vector<vector<char>> &matrica, int n, int m)
{
    int b=0;
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            if(matrica[i][j]=='S')
                b++;
        }
    }
    cout << b << endl;
}
void provera(vector<vector<char>> &matrica, queue<pair<int, int>> &vatra, int n, int m)
{
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            if(matrica[i][j]=='X')
                {
                    vatra.push(make_pair(i, j));
                }
        }
    }
}
int main()
{
    int n, m;
    cin >> n;
    cin >> m;
    vector<vector<char>> matrica(n, vector<char>(m));
    pair<int, int> steva = unos(matrica, n, m);
    queue<pair<int, int>> vatra;
    provera(matrica, vatra, n, m);
    queue<pair<int, int>> stek;
    stek.push(steva);
    int p=1;
    int promena1=1, promena2=vatra.size();
    while(!stek.empty() || !vatra.empty())
    {
        if(p==0 && !stek.empty())
        {
            if(promena1==1)
            {
                p=1;
            }
            int i=stek.front().first, j=stek.front().second;
            stek.pop();
            if(i+1<n && matrica[i+1][j]=='.')
            {
                stek.push(make_pair(i+1, j));
                matrica[i+1][j]='S';
            }
            if(i-1>=0 && matrica[i-1][j]=='.')
            {
                stek.push(make_pair(i-1, j));
                matrica[i-1][j]='S';
            }
            if(j+1<m && matrica[i][j+1]=='.')
            {
                stek.push(make_pair(i, j+1));
                matrica[i][j+1]='S';
            }
            if(j-1>=0 && matrica[i][j-1]=='.')
            {
                stek.push(make_pair(i, j-1));
                matrica[i][j-1]='S';
            }
            promena1--;
            if(p==1)
            {
                promena1=stek.size();
            }
        }
        else
        {
            if(!vatra.empty())
            {
                if(promena2==1)
                {
                    p=0;
                }
                int vx=vatra.front().first, vy=vatra.front().second;
                vatra.pop();
                if(vx+1<n && matrica[vx+1][vy]=='.')
                {
                    vatra.push(make_pair(vx+1, vy));
                    matrica[vx+1][vy]='X';
                }
                if(vx-1>=0 && matrica[vx-1][vy]=='.')
                {
                    vatra.push(make_pair(vx-1, vy));
                    matrica[vx-1][vy]='X';
                }
                if(vy+1<m && matrica[vx][vy+1]=='.')
                {
                    vatra.push(make_pair(vx, vy+1));
                    matrica[vx][vy+1]='X';
                }
                if(vy-1>=0 && matrica[vx][vy-1]=='.')
                {
                    vatra.push(make_pair(vx, vy-1));
                    matrica[vx][vy-1]='X';
                }
                promena2--;
                if(p==0)
                {
                    promena2=vatra.size();
                }
            }
            else
                p=0;
        }
    }
    stampaj(matrica, n, m);
    return 0;
}