#include <bits/stdc++.h>
#define MAXN 300005
#define MAXK 600005
#define MOD 998244353
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pli pair<ll, int>
#define ll long long
using namespace std;

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

#define ordered_set tree<pii, null_type,less<pii>, rb_tree_tag,tree_order_statistics_node_update>

struct polje
{
    int i, j, d=1e7;
};

void solve() {

    int n, m;cin>>n>>m;
    vector<vector<char>>mat(n+2, vector<char>(m+2));

    int si, sj;
    for(int i = 0; i<n; i++)
    {
        string s;cin>>s;
        for(int j = 0; j<m; j++)
        {
            mat[i][j]=s[j];
            if(mat[i][j]=='S')
            {
                si = i;
                sj = j;
            }
        }
    }
    vector<vector<bool>>vis(n+2, vector<bool>(m+2, false));
    vector<vector<int>>dist(n+2, vector<int>(m+2, 1e7));
    vector<vector<int>>distv(n+2, vector<int>(m+2, 1e7));

    queue<pii > q;
    q.push({ si, sj });
    dist[si][sj]=0;

    int dx[] = { 0, -1, 0, 1};
    int dy[] = { 1, 0, -1, 0};

    while (!q.empty()) {

        int x = q.front().first;
        int y = q.front().second;
        q.pop();
        if(vis[x][y])continue;
        vis[x][y]=true;

        for (int i = 0; i < 4; i++) {
            int a = x + dx[i];
            int b = y + dy[i];
            if (a < 0 || a >= n || b >= m || b < 0)
                continue;
            if(mat[a][b]=='X' || mat[a][b]=='*')continue;

            if(dist[a][b]>dist[x][y]+1){
                dist[a][b] = dist[x][y] + 1;
                q.push({ a, b });
            }

        }
    }

    for(int i = 0; i<n; i++)
    {
        for(int j = 0; j<m; j++)
        {
            if(mat[i][j]=='X')
            {
                vector<vector<bool>>vis2(n+2, vector<bool>(m+2, false));
                queue<pii> q2;
                q2.push({ i, j });
                distv[i][j]=0;

                while (!q2.empty()) {

                    int x2 = q2.front().first;
                    int y2 = q2.front().second;
                    q2.pop();
                    if(vis2[x2][y2])continue;
                    vis[x2][y2]=true;

                    for (int i = 0; i < 4; i++) {
                        int a2 = x2 + dx[i];
                        int b2 = y2 + dy[i];
                        if (a2 < 0 || a2 >= n || b2 >= m || b2 < 0)
                            continue;
                        if(mat[a2][b2]=='S' || mat[a2][b2]=='*')continue;

                        if(distv[a2][b2]>distv[x2][y2]+1){
                            distv[a2][b2] = distv[x2][y2] + 1;
                            q2.push({ a2, b2 });
                        }

                    }
                }
            }
        }
    }
    int ans=0;

    for(int i = 0; i<n; i++)
    {
        for(int j = 0; j<m; j++)
        {
            if(dist[i][j]<distv[i][j])ans++;
        }
    }

    cout<<ans<<"\n";
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    //int t;cin>>t;
    int t = 1;
    while (t--) {
        solve();
    }

    return 0;
}