#include <bits/stdc++.h>
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()

using namespace std;

using i64 = long long;

vector<int> dx = {1, 0, -1, 0};

vector<int> dy = {0, 1, 0, -1};

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int n, m;
    cin >> n >> m;

    vector<vector<char>> a(n, vector<char>(m));
    int x, y;
    int x2, y2;

    vector<array<int, 2>> pos;

    for(int i = 0;i < n;i++){
        for(int j = 0;j < m;j++){
            cin >> a[i][j];

            if(a[i][j] == 'S'){
                x = i;
                y = j;
            }

            if(a[i][j] == 'X'){
                pos.push_back({i, j});
            }
        }
    }

    vector<vector<int>> steva(n, vector<int>(m, INT_MAX));
    vector<vector<int>> fire(n, vector<int>(m, INT_MAX));
    vector<vector<bool>> visited1(n, vector<bool>(m, false));
    vector<vector<bool>> visited2(n, vector<bool>(m, false));
    auto check = [&](int X, int Y){
        if(X >= 0 && X < n && Y >= 0 && Y < m){
            return true;
        }

        return false;
    };

    queue<array<int, 3>> s;

    s.push({x, y, 0});

    while(!s.empty()){
        auto [x3, y3, t] = s.front();
        s.pop();
        if(visited1[x3][y3]){
            continue;
        }
        visited1[x3][y3] = true;
        steva[x3][y3] = t;

        for(int i = 0;i < 4;i++){
            int newX = x3 + dx[i];
            int newY = y3 + dy[i];

            if(check(newX, newY)){
                if(a[newX][newY] == '.'){
                    s.push({newX, newY, t + 1});
                }
            }
        }
    }

    queue<array<int, 3>> f;

    for(auto p : pos){
        f.push({p[0], p[1], 0});
    }

    while(!f.empty()){
        auto [x3, y3, t] = f.front();
        f.pop();
        if(visited2[x3][y3]){
            continue;
        }
        visited2[x3][y3] = true;
        fire[x3][y3] = t;

        for(int i = 0;i < 4;i++){
            int newX = x3 + dx[i];
            int newY = y3 + dy[i];

            if(check(newX, newY)){
                if(a[newX][newY] == '.'){
                    f.push({newX, newY, t + 1});
                }
            }
        }
    }

    int res = 0;

    for(int i = 0;i < n;i++){
        for(int j = 0;j < m;j++){
            if(steva[i][j] < fire[i][j]){
                res++;
            }
        }
    }

    cout << res << "\n";
}