#include <bits/stdc++.h>
#define fast ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

using namespace std;

int main()
{
    fast;
    int n, m;
    char lav[1005][1005];
    queue<tuple<int, int, int> > steva;
    queue<tuple<int, int, int> > vatra;
    cin >> n >> m;



    for(int i = 0; i < n; i++)
    {
        string s;
        cin >> s;
        for(int j = 0; j < m; j++)
        {
            lav[i][j] = s[j];
            if(s[j] == 'S') steva.push(make_tuple(i, j, 0));
            if(s[j] == 'X') vatra.push(make_tuple(i, j, 0));
        }
    }

    int counter = 1;
    int generacija_vatre = 0;
    int generacija_steve = 0;

    while(!steva.empty())
    {
        while(1)
        {
            if(generacija_vatre != get<2>(vatra.front()) || steva.empty())
            {
                generacija_vatre++;
                break;
            }
            tuple<int, int, int> tr_vatra = vatra.front();
            vatra.pop();

            int x = get<0>(tr_vatra);
            int y = get<1>(tr_vatra);
            int gen = get<2>(tr_vatra);
            if(x + 1 < n && lav[x + 1][y] == '.')
            {
                lav[x + 1][y] = 'X';
                vatra.push(make_tuple(x + 1, y, gen + 1));
            }
            if(x - 1 >= 0 && lav[x - 1][y] == '.')
            {
                lav[x - 1][y] = 'X';
                vatra.push(make_tuple(x - 1, y, gen + 1));
            }
            if(y + 1 < m && lav[x][y + 1] == '.')
            {
                lav[x][y + 1] = 'X';
                vatra.push(make_tuple(x, y + 1, gen + 1));
            }
            if(y - 1 >= 0 && lav[x][y - 1] == '.')
            {
                lav[x][y - 1] = 'X';
                vatra.push(make_tuple(x, y - 1, gen + 1));
            }
        }

        while(1)
        {
            if(generacija_steve != get<2>(steva.front()) || steva.empty())
            {
                generacija_steve++;
                break;
            }
            tuple<int, int, int> tr_steva = steva.front();
            steva.pop();

            int x = get<0>(tr_steva);
            int y = get<1>(tr_steva);
            int gen = get<2>(tr_steva);
            if(x + 1 < n && lav[x + 1][y] == '.')
            {
                counter++;
                lav[x + 1][y] = 'S';
                steva.push(make_tuple(x + 1, y, gen + 1));
            }
            if(x - 1 >= 0 && lav[x - 1][y] == '.')
            {
                counter++;
                lav[x - 1][y] = 'S';
                steva.push(make_tuple(x - 1, y, gen + 1));
            }
            if(y + 1 < m && lav[x][y + 1] == '.')
            {
                counter++;
                lav[x][y + 1] = 'S';
                steva.push(make_tuple(x, y + 1, gen + 1));
            }
            if(y - 1 >= 0 && lav[x][y - 1] == '.')
            {
                counter++;
                lav[x][y - 1] = 'S';
                steva.push(make_tuple(x, y - 1, gen + 1));
            }
        }
    }

//    for(int i = 0; i < n; i++)
//    {
//        for(int j = 0; j < m; j++)
//        {
//            cout << lav[i][j];
//        }
//        cout << "\n";
//    }
//    cout << "\n";
    cout << counter << "\n";
    return 0;
}