#include <bits/stdc++.h>

#define int long long

using namespace std;

const int N = 1005;

int pravac[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};

int minc1[N][N];

int minc2[N][N];

char a[N][N];

int n, m;

vector<pair<int,int>> steve;

vector<pair<int,int>> vatra;

int valid1(int x, int y) {
    if(x >= 1 && x <= n && y >= 1 && y <= m && a[x][y] != '*' && a[x][y] != 'X' && minc1[x][y] == -1) {
        return 1;
    }
    else {
        return 0;
    }
}

int valid2(int x, int y) {
    if(x >= 1 && x <= n && y >= 1 && y <= m && a[x][y] != '*' && a[x][y] != 'X' && minc2[x][y] == -1) {
        return 1;
    }
    else {
        return 0;
    }
}


void bfs() {
    queue<pair<int,int>> Q;
    for(auto it : steve) {
        Q.emplace(it.first, it.second);
        minc1[it.first][it.second] = 0;
    }
    while(!Q.empty()) {
        int x = Q.front().first;
        int y = Q.front().second;
        Q.pop();
        for(int i = 0; i < 4; i++) {
            int x1 = x + pravac[i][0];
            int y1 = y + pravac[i][1];
            if(valid1(x1, y1)) {
                minc1[x1][y1] = minc1[x][y] + 1;
                Q.emplace(x1, y1);
            }
        }
    }
}

void multisorcebfs() {
    queue<pair<int,int>> Q;
    for(auto it : vatra) {
        Q.emplace(it.first, it.second);
        minc2[it.first][it.second] = 0;
    }
    while(!Q.empty()) {
        int x = Q.front().first;
        int y = Q.front().second;
        Q.pop();
        for(int i = 0; i < 4; i++) {
            int x1 = x + pravac[i][0];
            int y1 = y + pravac[i][1];
            if(valid2(x1, y1)) {
                minc2[x1][y1] = minc2[x][y] + 1;
                Q.emplace(x1, y1);
            }
        }
    }
}

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cin >> n >> m;
    for(int i = 0; i <= n; i++) {
        for(int j = 0; j <= m; j++) {
            minc1[i][j] = - 1;
            minc2[i][j] = - 1;
        }
    }
    for(int i = 1; i <= n; i++) {
        for(int j = 1; j <= m; j++) {
            cin >> a[i][j];
            if(a[i][j] == 'S') {
                steve.push_back({i, j});
            }
            if(a[i][j] == 'X') {
                vatra.push_back({i, j});
            }
        }
    }
    bfs();
    multisorcebfs();
    int ans = 0;
    for(int i = 1; i <= n; i++) {
        for(int j = 1; j <= m; j++) {
            if(a[i][j] == '*') {
                continue;
            }
            if(minc1[i][j] == - 1) {
                continue;
            }
            if(minc2[i][j] == - 1) {
                if(minc1[i][j] != - 1) {
                    ans += 1;
                    continue;
                }
            }
            if(minc1[i][j] < minc2[i][j]) {
                //cout << i << " " << j << " " << minc1[i][j] << " " << minc2[i][j] << "\n";
                ans += 1;
            }
        }
        //cout << "\n";
    }
    cout << ans;
}