#include <bits/stdc++.h>

using namespace std;

int main()
{
    int n, m, b;
    cin >> n >> m;
    bool bo = true;
    vector<vector<char>> a(n + 2, vector<char>(m + 2, '*'));
    vector<pair<int, int>> x;
    vector<pair<int, int>> s;
    for (int i = 1; i < n + 1; i++)
        for (int j = 1; j < m + 1; j++)
        {
            cin >> a[i][j];
            if (a[i][j] == 'X')
                x.push_back({i, j});
            if (a[i][j] == 'S')
                s.push_back({i, j});
        }
    int s2 = 1;
    int s1 = 0;
    int ss, sx;
    while (bo)
    {
        bo = false;
        {
            sx = x.size();
            for (int i = 0; i < sx; i++) // vatra
            {
                if ((a[x[i].first + 1][x[i].second] != '*') && (a[x[i].first + 1][x[i].second] != 'X'))
                {
                    a[x[i].first + 1][x[i].second] = 'X';
                    x.push_back({x[i].first + 1, x[i].second});
                }
                if ((a[x[i].first][x[i].second + 1] != '*') && (a[x[i].first][x[i].second + 1] != 'X'))
                {
                    a[x[i].first][x[i].second + 1] = 'X';
                    x.push_back({x[i].first, x[i].second + 1});
                }
                if ((a[x[i].first - 1][x[i].second] != '*') && (a[x[i].first - 1][x[i].second] != 'X'))
                {
                    a[x[i].first - 1][x[i].second] = 'X';
                    x.push_back({x[i].first - 1, x[i].second});
                }
                if ((a[x[i].first][x[i].second - 1] != '*') && (a[x[i].first][x[i].second - 1] != 'X'))
                {
                    a[x[i].first][x[i].second - 1] = 'X';
                    x.push_back({x[i].first, x[i].second - 1});
                }
                x.erase(x.begin() + i);i--;sx--;
            }

            s1 = s2;
            ss = s.size();
            for (int i = 0; i < ss; i++)
            {
                if ((a[s[i].first + 1][s[i].second] != '*') && (a[s[i].first + 1][s[i].second] != 'X') && (a[s[i].first + 1][s[i].second] != 'S'))
                { // v
                    a[s[i].first + 1][s[i].second] = 'S';
                    s.push_back({s[i].first + 1, s[i].second});
                    bo = true;
                    s2++;
                }
                if ((a[s[i].first][s[i].second + 1] != '*') && (a[s[i].first][s[i].second + 1] != 'X') && (a[s[i].first][s[i].second + 1] != 'S'))
                { //>
                    a[s[i].first][s[i].second + 1] = 'S';
                    s.push_back({s[i].first, s[i].second + 1});
                    s2++;
                    bo = true;
                }
                if ((a[s[i].first - 1][s[i].second] != '*') && (a[s[i].first - 1][s[i].second] != 'X') && (a[s[i].first - 1][s[i].second] != 'S'))
                { //^
                    a[s[i].first - 1][s[i].second] = 'S';
                    s.push_back({s[i].first - 1, s[i].second});
                    s2++;
                    bo = true;
                }
                if ((a[s[i].first][s[i].second - 1] != '*') && (a[s[i].first][s[i].second - 1] != 'X') && (a[s[i].first][s[i].second - 1] != 'S'))
                { //<
                    a[s[i].first][s[i].second - 1] = 'S';
                    s.push_back({s[i].first, s[i].second - 1});
                    s2++;
                    bo = true;
                }
                s.erase(s.begin() + i);
                i--;
                ss--;
            }
        }
    }
    cout << s2;
}