//Dedicated to my love,ivaziva
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp> 
// #ifndef ONLINE_JUDGE
// #include "C:\Users\andre\Downloads\cpp-dump-0.7.0\cpp-dump-0.7.0\cpp-dump.hpp"
// #define dbg(...) cpp_dump(__VA_ARGS__)
// #else
// #define dump(...)
// #endif 
using namespace std;
typedef long long ll;
typedef long double ldb; 
using namespace __gnu_pbds;
typedef tree<ll,null_type,less_equal<ll>,rb_tree_tag,tree_order_statistics_node_update> order_set;
const int mxN=1e3+50;  
const ldb eps=0.000001;
const int mod=998244353; //998244353 1e9+7
const int iinf=2e9;
const ll inf=2e18;  
int n,m;
string a[mxN];
int dist[mxN][mxN][2];
bool ok(int i,int j)
{ 
    if(i>=1&&i<=n&&j>=1&&j<=m&&a[i][j]!='*') return 1;
    return 0;
}
void bfs(int tp)
{
    queue<pair<int,int>> q;
    for(int i=1; i<=n; i++)
    {       
        for(int j=1; j<=m; j++)
        {
            if(a[i][j]=='S'&&tp==0)
            {
                dist[i][j][tp]=0; 
                q.push(make_pair(i,j)); 
            }
            if(a[i][j]=='X'&&tp==1)
            {  
                dist[i][j][tp]=0; 
                q.push(make_pair(i,j)); 
            }
        }
    }
    while(!q.empty())
    {
        pair<int,int> x=q.front();
        q.pop();
        int i=x.first,j=x.second;
        if(ok(i+1,j)&&dist[i+1][j][tp]>(dist[x.first][x.second][tp]+1))
        {
            dist[i+1][j][tp]=dist[x.first][x.second][tp]+1;
            q.push({i+1,j}); 
        }
        if(ok(i,j+1)&&dist[i][j+1][tp]>(dist[x.first][x.second][tp]+1))
        {
            dist[i][j+1][tp]=dist[x.first][x.second][tp]+1;
            q.push({i,j+1}); 
        }
        if(ok(i-1,j)&&dist[i-1][j][tp]>(dist[x.first][x.second][tp]+1))
        {
            dist[i-1][j][tp]=dist[x.first][x.second][tp]+1;
            q.push({i-1,j}); 
        } 
        if(ok(i,j-1)&&dist[i][j-1][tp]>(dist[x.first][x.second][tp]+1))
        { 
            dist[i][j-1][tp]=dist[x.first][x.second][tp]+1;
            q.push({i,j-1}); 
        } 
    }
} 
int main()
{ 
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    cin >> n >> m;
    for(int i=1; i<=n; i++)
    {
        cin >> a[i];
        a[i]='i'+a[i];
        for(int j=1; j<=m; j++)
        {
            dist[i][j][0]=1000001;
            dist[i][j][1]=1000001; 
        }
    }
    bfs(0);
    bfs(1);  
    int ans=0;
    for(int i=1; i<=n; i++)
    {
        for(int j=1; j<=m; j++)
        {
            if(dist[i][j][0]<dist[i][j][1]) ans++;
        }
    }
    cout << ans << "\n"; 
    return 0;
}