#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
void DBG() { cerr << "]" << endl; }
template<class H, class... T> void DBG(H h, T... t) { cerr << to_string(h); if(sizeof...(t)) cerr << ", "; DBG(t...); }
#define dbg(...) cerr << "[" << #__VA_ARGS__ << "]: [", DBG(__VA_ARGS__)

const int N = 1003;
const int inf = 1e9;
const int POM[2][4] = {{-1, 1, 0, 0}, {0, 0, 1, -1}};
int t[N][N][2];
char a[N][N];

void solve() {
    int n, m; cin >> n >> m;
    queue<pair<int, int>> q[2];
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            char x; cin >> x;
            a[i][j] = x;
            t[i][j][0] = inf; t[i][j][1] = inf;
            if (x == 'X') {
                t[i][j][0] = 1;
                q[0].emplace(i, j);
            } else if (x == 'S') {
                t[i][j][1] = 1;
                q[1].emplace(i, j);
            }
        }
    }

    auto bfs = [&](int tp) {
        while (!q[tp].empty()) {
            auto [i, j] = q[tp].front(); q[tp].pop();
            for (int d = 0; d < 4; d++) {
                int ik = i + POM[0][d], jk = j + POM[1][d];
                if (ik < 1 || ik > n || jk < 1 || jk > m) continue;
                if (a[ik][jk] == '*') continue;
                if (t[i][j][tp]+1 < t[ik][jk][tp]) {
                    t[ik][jk][tp] = t[i][j][tp]+1;
                    q[tp].emplace(ik, jk);
                }
            }
        }
    };

    bfs(0);
    bfs(1);

    int ans = 0;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            ans += (t[i][j][1] < t[i][j][0]);
        }
    }

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

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