#include <bits/stdc++.h>
using namespace std;

void popunjavanje_tabele(char a[][1001], int s[][1001], int n, int m, int sx, int sy, int br)
{
    s[sx][sy] = br;
    if(sx-1 >= 0 && a[sx-1][sy] != '*' && (s[sx-1][sy] == -1 || s[sx-1][sy] > br+1))  // gore
        popunjavanje_tabele(a, s, n, m, sx-1, sy, br+1);
    if(sy-1 >= 0 && a[sx][sy-1] != '*' && (s[sx][sy-1] == -1 || s[sx][sy-1] > br+1))  // levo
        popunjavanje_tabele(a, s, n, m, sx, sy-1, br+1);
    if(sx+1 < n && a[sx+1][sy] != '*' && (s[sx+1][sy] == -1 || s[sx+1][sy] > br+1))   // dole
        popunjavanje_tabele(a, s, n, m, sx+1, sy, br+1);
    if(sy+1 < m && a[sx][sy+1] != '*' && (s[sx][sy+1] == -1 || s[sx][sy+1] > br+1))   // desno
        popunjavanje_tabele(a, s, n, m, sx, sy+1, br+1);
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    int n, m, sx, sy;
    cin >> n >> m;
    char a[n][1001];
    vector<pair<int, int>> x;
    for(int i=0; i<n; i++)
    {
        for(int j=0; j<m; j++)
        {
            cin >> a[i][j];
            if(a[i][j] == 'S')
            {
                sx = i;
                sy = j;
            }
            if(a[i][j] == 'X')
            {
                x.push_back(make_pair(i, j));
            }
        }
    }
    int s[n][1001];
    for(int i=0; i<n; i++)
    {
        for(int j=0; j<m; j++)
        {
            s[i][j] = -1;
        }
    }
    popunjavanje_tabele(a, s, n, m, sx, sy, 0);

    int xuk[n][1001];
    for(int i=0; i<n; i++)
    {
        for(int j=0; j<m; j++)
        {
            xuk[i][j] = -1;
        }
    }
    for(auto i: x)
    {
        popunjavanje_tabele(a, xuk, n, m, i.first, i.second, 0);
    }

    int ans = 0;
    for(int i=0; i<n; i++)
    {
        for(int j=0; j<m; j++)
        {
            if(s[i][j]!= -1)
            {
                if(s[i][j] < xuk[i][j] || xuk[i][j] == -1)
                    ans++;
            }
        }
    }
    cout << ans;

    return 0;
}