#include <bits/stdc++.h>

using namespace std;

bool moze(int x, int y, int n, int m, char **l)
{
    if (x < 0 || x >= n || y < 0 || y >= m || l[x][y] == '*')
        return false;
    return true;
}

int main()
{
    long long int n, m;
    cin >> n;
    cin >> m;

    if (n == 1)
    {
        string str1;
        cin >> str1;
        char clevo = ' ', cdesno = ' ';
        int indexclevo = 0, indexcdesno = 0, index = 0, r = 1;
        bool s_nadjen = false;
        for (int i = 0; i < m; i++)
        {
            char c1 = str1[i];
            if (c1 == 'S')
            {
                index = i;
                s_nadjen = true;
                continue;
            }
            if (!s_nadjen)
            {
                if (c1 == 'X')
                {
                    clevo = 'X';
                    indexclevo = i;
                }
                else if (c1 == '*')
                {
                    clevo = '*';
                    indexclevo = i;
                }
            }
            if (s_nadjen)
            {
                if (c1 == 'X')
                {
                    cdesno = 'X';
                    indexcdesno = i;
                    break;
                }
                else if (c1 == '*')
                {
                    cdesno = '*';
                    indexcdesno = i;
                    break;
                }
            }
        }

        if (clevo == 'X')
            r += (index - indexclevo - 1) / 2;
        else if (clevo == '*')
            r += index - indexclevo - 1;
        else
            r += index;
        if (cdesno == 'X')
            r += (indexcdesno - index - 1) / 2;
        else if (cdesno == '*')
            r += indexcdesno - index - 1;
        else
            r += m - 1 - index;

        cout << r << endl;
    }
    else
    {
        char** l = new char* [n];
        int** l1 = new int* [n];
        int i1 = 0, j1 = 0, i2 = 0, j2 = 0;
        bool ima_x = true; int brzid = 0;
        for (int i = 0; i < n; i++)
        {
            string str1;
            cin >> str1;
            l[i] = new char[m];
            l1[i] = new int[m];
            for (int j = 0; j < m; j++)
            {
                l[i][j] = str1[j];
                l1[i][j] = 0;
                if (l[i][j] == 'S')
                {
                    i1 = i;
                    j1 = j;
                }
                if (l[i][j] == 'X')
                {
                    ima_x = false;
                    i2 = i;
                    j2 = j;
                }
                if (l[i][j] == '*')
                    brzid++;
            }
        }

        if (ima_x)
        {
            cout << (n * m - brzid) << endl;
        }
        else
        {
            queue<int>* s = new queue<int>[3];
            s[0].push(i1);
            s[1].push(j1);
            s[2].push(1);
            queue<int>* x = new queue<int>[3];
            x[0].push(i2);
            x[1].push(j2);
            x[2].push(1);

            l1[i1][j1] = 1;
            l1[i2][j2] = -1;
            
            int r = 1;
            while (!s[0].empty())
            {
                int i = s[0].front(), j = s[1].front(), broj = s[2].front();
                if (moze(i - 1, j, n, m, l) && l1[i - 1][j] == 0)
                {
                    s[0].push(i - 1);
                    s[1].push(j);
                    s[2].push(broj + 1);
                    l1[i - 1][j] = broj + 1;
                    r++;
                }
                if (moze(i, j - 1, n, m, l) && l1[i][j - 1] == 0)
                {
                    s[0].push(i);
                    s[1].push(j - 1);
                    s[2].push(broj + 1);
                    l1[i][j - 1] = broj + 1;
                    r++;
                }
                if (moze(i + 1, j, n, m, l) && l1[i + 1][j] == 0)
                {
                    s[0].push(i + 1);
                    s[1].push(j);
                    s[2].push(broj + 1);
                    l1[i + 1][j] = broj + 1;
                    r++;
                }
                
                if (moze(i, j + 1, n, m, l) && l1[i][j + 1] == 0)
                {
                    s[0].push(i);
                    s[1].push(j + 1);
                    s[2].push(broj + 1);
                    l1[i][j + 1] = broj + 1;
                    r++;
                }

                if (!x[0].empty())
                {
                    i = x[0].front(); j = x[1].front(); broj = x[2].front();
                    if (moze(i - 1, j, n, m, l) && l1[i - 1][j] == 0)
                    {
                        x[0].push(i - 1);
                        x[1].push(j);
                        x[2].push(broj + 1);
                        l1[i - 1][j] = -1 * (broj + 1);
                    }
                    if (moze(i, j - 1, n, m, l) && l1[i][j - 1] == 0)
                    {
                        x[0].push(i);
                        x[1].push(j - 1);
                        x[2].push(broj + 1);
                        l1[i][j - 1] = -1 * (broj + 1);
                    }
                    if (moze(i + 1, j, n, m, l) && l1[i + 1][j] == 0)
                    {
                        x[0].push(i + 1);
                        x[1].push(j);
                        x[2].push(broj + 1);
                        l1[i + 1][j] = -1 * (broj + 1);
                    }
                    if (moze(i, j + 1, n, m, l) && l1[i][j + 1] == 0)
                    {
                        x[0].push(i);
                        x[1].push(j + 1);
                        x[2].push(broj + 1);
                        l1[i][j + 1] = -1 * (broj + 1);
                    }
                }

                s[0].pop(); s[1].pop(); s[2].pop();
                if (!x[0].empty())
                {
                    x[0].pop();
                    x[1].pop();
                    x[2].pop();
                }

            }

            cout << r << endl;
        }
    }
}