#include <bits/stdc++.h>
#define ubrzaj ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);

using namespace std;

int main()
{
    ubrzaj
    int n,m;
    cin >> n >> m;
    vector<vector<char>> v(n+2,vector<char>(m+2,'*'));
    vector<pair<int,int>> steva;
    vector<pair<int,int>> vatra;
    for(int i=1; i<=n; i++){
        //v[i].resize(m+2);
        for(int j=1; j<=m; j++){
            cin >> v[i][j];
            if(v[i][j]=='S'){steva.push_back({i,j});}
            if(v[i][j]=='X'){vatra.push_back({i,j});}
        }
    }
    int brojac=1;
    while(steva.size()!=0){
        vector<pair<int,int>> vatra2;
        for(int g=0; g<vatra.size(); g++){
            int i=vatra[g].first;
            int j=vatra[g].second;
            if(v[i-1][j]=='.'){v[i-1][j]='X';vatra2.push_back({i-1,j});}
            if(v[i][j-1]=='.'){v[i][j-1]='X';vatra2.push_back({i,j-1});}
            if(v[i][j+1]=='.'){v[i][j+1]='X';vatra2.push_back({i,j+1});}
            if(v[i+1][j]=='.'){v[i+1][j]='X';vatra2.push_back({i+1,j});}
        }
        vatra=vatra2;
        vector<pair<int,int>> steva2;

        for(int g=0; g<steva.size(); g++){
            int i=steva[g].first;
            int j=steva[g].second;

            if(v[i-1][j]=='.'){v[i-1][j]='S';steva2.push_back({i-1,j});}
            if(v[i][j-1]=='.'){v[i][j-1]='S';steva2.push_back({i,j-1});}
            if(v[i][j+1]=='.'){v[i][j+1]='S';steva2.push_back({i,j+1});}
            if(v[i+1][j]=='.'){v[i+1][j]='S';steva2.push_back({i+1,j});}
        }
        steva=steva2;

        brojac+=steva.size();

    }
    /*cout << endl;
    for(int i=0; i<=n+1; i++){
        for(int j=0; j<=m+1; j++)
            cout << v[i][j] << " ";
        cout << endl;
    }
    cout << endl;*/
    cout << brojac << endl;
    return 0;
}