#include <iostream>
#include <queue>
using namespace std;

int n, m;
char niz[1000][1000];
int stevo[1000][1000];
bool stevop[1000][1000];
int vutro[1000][1000];
bool vutrop[1000][1000];

void pretragaS(int is, int js) {
    stevo[is][js] = 1;
    stevop[is][js] = true;

    queue <pair<int, int> > steva;


    steva.emplace(is, js);
    while (!steva.empty()) {
        is = steva.front().first;
        js = steva.front().second;
        steva.pop();

        if (is - 1 >= 0 and is - 1 < n and js >= 0 and js < m and niz[is - 1][js] != '*' and niz[is - 1][js] != 'S' and stevop[is - 1][js] == false) {
            stevo[is - 1][js] = stevo[is][js] + 1;
            stevop[is - 1][js] = true;
            steva.emplace(is - 1, js);
        }
        if (is >= 0 and is < n and js + 1 >= 0 and js + 1 < m and niz[is][js + 1] != '*' and niz[is][js + 1] != 'S' and stevop[is][js + 1] == false) {
            stevo[is][js + 1] = stevo[is][js] + 1;
            stevop[is][js + 1] = true;
            steva.emplace(is, js + 1);
        }
        if (is + 1 >= 0 and is + 1 < n and js >= 0 and js < m and niz[is + 1][js] != '*' and niz[is + 1][js] != 'S' and stevop[is + 1][js] == false) {
            stevop[is + 1][js] = true;
            stevo[is + 1][js] = stevo[is][js] + 1;
            steva.emplace(is + 1, js);
        }
        if (is >= 0 and is < n and js - 1 >= 0 and js - 1 < m and niz[is][js - 1] != '*' and niz[is][js - 1] != 'S' and stevop[is][js - 1] == false) {
            stevop[is][js - 1] = true;
            stevo[is][js - 1] = stevo[is][js] + 1;
            steva.emplace(is, js - 1);
        }

        
    }

}
void pretragaV(int is, int js) {
    if (is == -1 and js == -1) {
        for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) vutro[i][j] = 10000;

        return;
    }
    vutro[is][js] = 0;

    queue <pair<int, int> > steva;


    steva.emplace(is, js);
    while (!steva.empty()) {
        is = steva.front().first;
        js = steva.front().second;
        steva.pop();

        if (is - 1 >= 0 and is - 1 < n and js >= 0 and js < m and niz[is - 1][js] != '*' and vutrop[is - 1][js] == false) {
            vutro[is - 1][js] = vutro[is][js] + 1;
            vutrop[is - 1][js] = true;
            steva.emplace(is - 1, js);
        }
        if (is >= 0 and is < n and js + 1 >= 0 and js + 1 < m and niz[is][js + 1] != '*' and vutrop[is][js + 1] == false) {
            vutro[is][js + 1] = vutro[is][js] + 1;
            vutrop[is][js + 1] = true;
            steva.emplace(is, js + 1);
        }
        if (is + 1 >= 0 and is + 1 < n and js >= 0 and js < m and niz[is + 1][js] != '*' and vutrop[is + 1][js] == false) {
            vutrop[is + 1][js] = true;
            vutro[is + 1][js] = vutro[is][js] + 1;
            steva.emplace(is + 1, js);
        }
        if (is >= 0 and is < n and js - 1 >= 0 and js - 1 < m and niz[is][js - 1] != '*' and vutrop[is][js - 1] == false) {
            vutrop[is][js - 1] = true;
            vutro[is][js - 1] = vutro[is][js] + 1;
            steva.emplace(is, js - 1);
        }


    }

}

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

    int is, js, ix = -1, jx = -1;

    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            if (niz[i][j] == 'S') {
                is = i;
                js = j;
            }
        }
    }
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            if (niz[i][j] == 'X') {
                ix = i;
                jx = j;
            }
        }
    }
    pretragaS(is, js);
    pretragaV(ix, jx);

    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            if (niz[i][j] == '.') {
                if (stevo[i][j] < vutro[i][j]) niz[i][j] = 'S';
                else niz[i][j] = 'X';
            }
        }
    }


    int brojac = 0;

    for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) if (niz[i][j] == 'S') brojac++;
    cout << brojac;
return 0;
}