#include <bits/stdc++.h>
//#define int long long
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define sc second
#define endl "\n"
#define pii pair<int,int>

using namespace std;

const int MAXN = 1e5+5;
const int mod7 = 1e9+7;
const int inf = 1e9;

struct str{int i,j,d;};

signed main()
{
    ios_base::sync_with_stdio(false),cin.tie(0), cout.tie(0);
    int tt=1;
    //cin >> tt;
    while(tt--)
    {
        int n,m;
        cin >> n >> m;
        queue<str> steva;
        queue<str> pozar;
        vector<vector<char>> matrica(n, vector<char>(m));
        vector<vector<bool>> visited(n, vector<bool>(m, false));
        vector<vector<bool>> visited2(n, vector<bool>(m, false));
        vector<vector<int>> dist1(n, vector<int>(m, inf));
        vector<vector<int>> dist2(n, vector<int>(m, inf));
        for(int i=0; i<n; i++)
        {
            string s;cin >> s;
            for(int j=0; j<m; j++)
            {
                matrica[i][j] =s[j];
                if(matrica[i][j] == 'S')steva.push({i, j, 0});
                if(matrica[i][j] == 'X')pozar.push({i, j , 0});
                else if(matrica[i][j] == '*'){visited[i][j] = true, visited2[i][j] = true;}
            }
        }
        while(!steva.empty())
        {
            auto tr = steva.front();steva.pop();
            int i = tr.i;int j = tr.j;
            visited[i][j] = true;
            dist1[i][j] =tr.d;
            if(i!=0 && !visited[i-1][j])
            {
                steva.push({i-1, j, tr.d+1});
                visited[i-1][j] = true;
            }
            if(i!=n-1 && !visited[i+1][j])
            {
                steva.push({i+1, j, tr.d+1});
                visited[i+1][j] = true;
            }
            if(j!=0 && !visited[i][j-1])
            {
                steva.push({i, j-1, tr.d+1});
                visited[i][j-1] = true;
            }
            if(j!=m-1 && !visited[i][j+1])
            {
                steva.push({i, j+1, tr.d+1});
                visited[i][j+1] = true;
            }
        }
        while(!pozar.empty())
        {
            auto tr = pozar.front();pozar.pop();
            int i = tr.i;int j = tr.j;
            visited2[i][j] = true;
            dist2[i][j] =tr.d;
            if(i!=0 && !visited2[i-1][j])
            {
                pozar.push({i-1, j, tr.d+1});
                visited2[i-1][j] = true;
            }
            if(i!=n-1 && !visited2[i+1][j])
            {
                pozar.push({i+1, j, tr.d+1});
                visited2[i+1][j] = true;
            }
            if(j!=0 && !visited2[i][j-1])
            {
                pozar.push({i, j-1, tr.d+1});
                visited2[i][j-1] = true;
            }
            if(j!=m-1 && !visited2[i][j+1])
            {
                pozar.push({i, j+1, tr.d+1});
                visited2[i][j+1] = true;
            }
        }
        int cnt = 0;
        for(int i=0; i<n; i++)
        {
            for(int j=0; j<m; j++)
            {
                if(dist1[i][j] < dist2[i][j])cnt ++;
            }
        }
        cout << cnt << endl;
    }
}