#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
int s[10001][10001], v[10001][10001];
queue <pair<pair<int,int>,int>> kju;
char mat[10001][10001];
bool provera (int i, int j, int n, int m)
{
    if (i < 1 || i > n) return false;
    if (j < 1 || j > m) return false;
    if (mat[i][j] == '*') return false;
    return true;
}
int main()
{
    ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    int n, m, res = 0;
    cin >> n >> m;
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= m; j++)
        {
            cin >> mat[i][j];
            if (mat[i][j] == 'S')
            {
                kju.push({{i,j},1});
            }
            s[i][j] = 10000000;
            v[i][j] = 10000000;
        }
    }
    while (!kju.empty())
    {
        pair<pair<int,int>,int> parovi = kju.front();
        pair<int,int> par = parovi.first;
        int i = par.first, j = par.second;
        int rast = parovi.second;
        kju.pop();
        if (s[i][j] != 10000000) continue;
        s[i][j] = rast;
        if (provera(i+1,j,n,m) && s[i+1][j] == 10000000) kju.push({{i+1,j},rast+1});
        if (provera(i-1,j,n,m) && s[i-1][j] == 10000000) kju.push({{i-1,j},rast+1});
        if (provera(i,j+1,n,m) && s[i][j+1] == 10000000) kju.push({{i,j+1},rast+1});
        if (provera(i,j-1,n,m) && s[i][j-1] == 10000000) kju.push({{i,j-1},rast+1});
    }
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= m; j++)
        {
            if (mat[i][j] == 'X')
            {
                kju.push({{i,j},1});
            }
        }
    }
    if (kju.empty())
    {
        for (int i = 1; i <= n; i++)
        {
            for (int j = 1; j <= m; j++)
            {
                if (s[i][j] != 10000000) res++;
            }
        }
        cout << res;
        return 0;
    }
    while (!kju.empty())
    {
        pair<pair<int,int>,int> parovi = kju.front();
        pair<int,int> par = parovi.first;
        int i = par.first, j = par.second;
        int rast = parovi.second;
        kju.pop();
        if (v[i][j] != 10000000) continue;
        v[i][j] = rast;
        if (provera(i+1,j,n,m) && v[i+1][j] == 10000000) kju.push({{i+1,j},rast+1});
        if (provera(i-1,j,n,m) && v[i-1][j] == 10000000) kju.push({{i-1,j},rast+1});
        if (provera(i,j+1,n,m) && v[i][j+1] == 10000000) kju.push({{i,j+1},rast+1});
        if (provera(i,j-1,n,m) && v[i][j-1] == 10000000) kju.push({{i,j-1},rast+1});
    }
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= m; j++)
        {
            if (s[i][j] < v[i][j]) res++;
        }
    }
    cout << res;
    return 0;
}