#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef vector<pii> vpii;
typedef vector<pll> vpll;
#define FOR(i,a,b) for (int i = (a); i < (b); ++i)
#define F0R(i,a) FOR(i,0,a)
#define ROF(i,a,b) for (int i = (b)-1; i >= (a); --i)
#define R0F(i,a) ROF(i,0,a)
#define run int teodorfonburgjenasgospodar; cin >> teodorfonburgjenasgospodar; while (teodorfonburgjenasgospodar--)
#define f first
#define s second
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define pb push_back
#define yes cout << "YES\n";
#define no cout << "NO\n";
#define cintienull ios_base::sync_with_stdio(0); cin.tie(0);
#define mp make_pair
#define inputV(a, n) for(int i = 0; i < n; i++)cin>>a[i];
#define ov(a, n) for(int i = 0; i < n; i++)cout << a[i] << " ";
#define ovr(a, n) for(int i = 0; i < n; i++)cout << a[i] << "\n";
#define INF 1000000000
#define LINF 1000000000000000000LL
#define EPS 1e-9
#define MOD 1000000007;

vpii smerovi = {{0,1}, {0,-1}, {1,0}, {-1,0}};

int main()
{
    cintienull
    int n,m;
    cin >> n >> m;
    char mat[n][m];
    ll ds[n][m];
    ll dv[n][m];
    bool vis1[n][m];
    bool vis2[n][m];
    pll steva;
    vpll vatre;
    for (int i = 0; i < n; i++) {
        for(int j = 0; j < m; j++){
            cin >> mat[i][j];
            ds[i][j] = LINF; dv[i][j] = LINF; vis1[i][j] = 0; vis2[i][j] = 0;
            if (mat[i][j] == 'S'){
                steva = mp(i,j);
                ds[i][j] = 0;
                vis1[i][j] = 1;
            }
            if(mat[i][j] == 'X'){
                vatre.pb(mp(i,j));
                dv[i][j] = 0;
                vis2[i][j] = 1;
            }
        }
    }
    queue<pll> q1;
    q1.push(steva);
    while(!q1.empty()){
        pll u = q1.front();
        q1.pop();
        for (auto i : smerovi) {
            if(u.f + i.f >=0 && u.f+i.f < n && u.s+i.s >=0 && u.s+i.s < m){
                if(!vis1[u.f+i.f][u.s+i.s]){
                    if(mat[u.f+i.f][u.s+i.s] == '.'){
                        ds[u.f+i.f][u.s+i.s] = ds[u.f][u.s]+1;
                        vis1[u.f][u.s] = 1;
                        q1.push(mp(u.f+i.f, u.s+i.s));
                    }
                }
            }
        }
    }
    queue<pll> q2;
    for (auto i : vatre) {
        q2.push(i);
    }
    while(!q2.empty()){
        pll u = q2.front();
        q2.pop();
        for (auto i : smerovi) {
            if(u.f + i.f >=0 && u.f+i.f < n && u.s+i.s >=0 && u.s+i.s < m){
                if(!vis2[u.f+i.f][u.s+i.s]){
                    if(mat[u.f+i.f][u.s+i.s] == '.'){
                        dv[u.f+i.f][u.s+i.s] = dv[u.f][u.s]+1;
                        vis2[u.f][u.s] = 1;
                        q2.push(mp(u.f+i.f, u.s+i.s));
                    }
                }
            }
        }
    }
    ll brrr=0;
    for (int i = 0; i < n; i++) {
        for(int j = 0; j < m; j++){
            if(mat[i][j] == '.' || mat[i][j] == 'S'){
                if(ds[i][j] < dv[i][j])brrr++;
            }
        }
    }
    cout << brrr;
}