#include <iostream>
#include <cmath>
#include <list>
#include <vector>
#include <algorithm>
#include <bitset>
#include <string>
#include <set>
#include <map>
#include <queue>
#define ll int64_t
#define int int64_t
#define VI vector<ll>
#define rep(i,a,b) for (int i=a;i<=b;i++)
#define per(i,a,b) for (int i=a;i>=b;i--)
#define load(a) for (auto& x : a) cin >> x
#define unload(a) for(auto x: a) cout << x << " "

using namespace std;
const int N = 2e5+20; ll A[N], n;


void solve(){
    ll m; cin >> n >> m;
    char mapa[n][m];
    vector<vector<ll>> g (n*m);
    rep(i,0,n - 1){
        string input; cin >> input;
        rep(j,0,m - 1){
            char c;
            c = input[j];
            mapa[i][j] = c;
        }
    }
    ll stiv;
    queue<ll> q;
    bool visited[n*m];
    bool steva[n*m];
    rep(i,0,n - 1){
        rep(j,0,m - 1){
        visited[j+i*m] = false;
        steva[j+i*m] = false;
        char elem = mapa[i][j];
        if(elem != '*'){
            if(i + 1 < n) if(mapa[i+1][j] != '*') g[j+ i * m].push_back(j+(i+1)*m);
            if(i - 1 >= 0) if(mapa[i-1][j] != '*') g[j+ i * m].push_back(j+(i-1)*m);
            if(j + 1 < m) if(mapa[i][j+1] != '*') g[j+ i * m].push_back(j+1+i*m);
            if(j - 1 >= 0) if(mapa[i][j-1] != '*') g[j+ i * m].push_back(j-1+i*m);
            }
            if(elem == 'S') {
                stiv = j+i*m;
                visited[stiv] = true;
                steva[stiv] = true;
            }
            if(elem == 'X') {
                q.push(j+i*m);
                visited[j+i*m] = true;
            }
        }
    }
    ll ans = 1;

    q.push(stiv);
    while(!q.empty()){
        ll t = q.front();
        q.pop();
        for(auto x: g[t]){
            if(!visited[x]){
                visited[x] = true;
                if(steva[t] == true){
                    steva[x] = true;
                    ans++;
                }
                q.push(x);
            }
        }
    }
    cout << ans;
    return;
}

int32_t main() {
    ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);

    solve();
    return 0;
}