#include <bits/stdc++.h>
#define endl '\n'
using namespace std;
#define int long long
//typedef long long int lint;

void solve(){
    int n, m;
    cin >> n >> m;
    vector<vector<char>> a(n, vector<char>(m));
    for(int i = 0; i < n; i++){
        for(int j = 0; j < m; j++){
            cin >> a[i][j];
        }
    }
    vector<vector<int>> stevo(n, vector<int>(m, INT_MAX));
    queue<pair<int, int>> red;
    vector<vector<bool>> mark(n, vector<bool>(m, false));
    for(int i = 0; i < n; i++){
        for(int j = 0; j < m; j++){
            if(a[i][j] == 'S'){
                stevo[i][j] = 0;
                red.push({i, j});
                mark[i][j] = true;
            }
            else if(a[i][j] == '*'){
                stevo[i][j] = -1;
            }
        }
    }
    int trx = red.size();
    int x = 1;
    int br = 0;
    while(!red.empty()){
        int i = red.front().first;
        int j = red.front().second;
        red.pop();
        if(i != 0){
            if(a[i - 1][j] == '.'){
                if(!mark[i - 1][j]){
                    red.push({(i - 1), j});         
                    stevo[i - 1][j] = x;           
                }
                mark[i - 1][j] = true;
            }
        }
        if(i != (n - 1)){
            if(a[i + 1][j] == '.'){
                if(!mark[i + 1][j]){
                    red.push({(i + 1), j});
                    stevo[i + 1][j] = x;                    
                }
                mark[i + 1][j] = true;
            }
        }
        if(j != 0){
            if(a[i][j - 1] == '.'){
                if(!mark[i][j - 1]){
                    red.push({i, (j - 1)});
                    stevo[i][j - 1] = x;
                }
                mark[i][j - 1] = true;
            }
        }
        if(j != (m - 1)){
            if(a[i][j + 1] == '.'){
                if(!mark[i][j + 1]){
                    red.push({i, (j + 1)});
                    stevo[i][j + 1] = x;
                }
                mark[i][j + 1] = true;
            }
        }
        br++;
        if(br == trx){
            x++;
            br = 0;
            trx = red.size();
        }
    }



    for(int i = 0; i < n; i++){
        for(int j = 0; j < m; j++){
            mark[i][j] = false;
        }
    }
    vector<vector<int>> vatra(n, vector<int>(m, INT_MAX));
    for(int i = 0; i < n; i++){
        for(int j = 0; j < m; j++){
            if(a[i][j] == 'X'){
                red.push({i, j});
                vatra[i][j] = 0;
                mark[i][j] = true;
            }
        }
    }

    trx = red.size();
    x = 1;
    br = 0;
    while(!red.empty()){
        int i = red.front().first;
        int j = red.front().second;
        red.pop();
        if(i != 0){
            if(a[i - 1][j] == '.'){
                if(!mark[i - 1][j]){
                    vatra[i - 1][j] = x;
                    red.push({(i - 1), j});
                }
                mark[i - 1][j] = true;
            }
        }
        if(i != (n - 1)){
            if(a[i + 1][j] == '.'){
                if(!mark[i + 1][j]){
                    vatra[i + 1][j] = x;
                    red.push({(i + 1), j});
                }
                mark[i + 1][j] = true;
            }
        }
        if(j != 0){
            if(a[i][j - 1] == '.'){
                if(!mark[i][j - 1]){
                    vatra[i][j - 1] = x;
                    red.push({i, (j - 1)});
                }
                mark[i][j - 1] = true;
            }
        }
        if(j != (m - 1)){
            if(a[i][j + 1] == '.'){
                if(!mark[i][j + 1]){
                    vatra[i][j + 1] = x;
                    red.push({i, (j + 1)});
                }
                mark[i][j + 1] = true;
            }
        }
        br++;
        if(br == trx){
            br = 0;
            trx = red.size();
            x++;
        }
    }
    int rez = 0;
    for(int i = 0; i < n; i++){
        for(int j = 0; j < n; j++){
            if(a[i][j] == 'S'){
                rez++;
            }
            else if(a[i][j] == '.'){
                if(stevo[i][j] < vatra[i][j]){
                    rez++;
                }
            }
        }
    }
    cout << rez << endl;
    /*
    cout << endl;
    for(int i = 0; i < n; i++){
        for(int j = 0; j < m; j++){
            cout << stevo[i][j] << " ";
        }
        cout << endl;
    }
    */
}   
 


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