#include <bits/stdc++.h>
using namespace std;

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    int n,m,resenje = 0;
    cin >> n >> m;

    queue<pair<int,int>> S;
    queue<pair<int,int>> X;
    vector<vector<char>> vec(n+2,vector<char>(m+2,'*'));

    for(int i = 1;i <= n;i++)
        for(int j = 1;j <= m;j++){
            cin >> vec[i][j];
            if(vec[i][j] == 'S') {S.push(make_pair(i,j)); resenje++;}
            if(vec[i][j] == 'X') X.push(make_pair(i,j));
        }

    while(!S.empty()){
        int x = X.size();
        for(int i = 0;i < x;i++){
            pair<int,int> poz = X.front();
            X.pop();
            int l = poz.first, r = poz.second;
            int dx[4] = {-1, 1, 0, 0};
            int dy[4] = {0, 0, 1, -1};
            for(int i = 0;i < 4;i++){
                if(vec[l+dx[i]][r+dy[i]] != 'X' && vec[l+dx[i]][r+dy[i]] != '*'){
                    vec[l+dx[i]][r+dy[i]] = 'X';
                    X.push(make_pair(l + dx[i], r + dy[i]));
                }
            }
        }

        int s = S.size();
        for(int i = 0;i < s;i++){
            pair<int,int> poz = S.front();
            S.pop();
            int l = poz.first, r = poz.second;
            int dx[4] = {-1, 1, 0, 0};
            int dy[4] = {0, 0, 1, -1};
            for(int i = 0;i < 4;i++){
                if(vec[l+dx[i]][r+dy[i]] == '.'){
                    vec[l+dx[i]][r+dy[i]] = 'S';
                    S.push(make_pair(l + dx[i], r + dy[i]));
                    resenje++;
                }
            }
        }
    }

    cout << resenje;
}