#include <iostream>
#define pi pair<int, int>

using namespace std;

int matrica[1002][1002];

pi vatra1[1000000];
pi vatra2[1000000];
pi steva1[1000000];
pi steva2[1000000];

int vniz = 0, sniz = 0;
pi* vsw[2] = {vatra1, vatra2};
int vst[2] = {0, 0};
pi* ssw[2] = {steva1, steva2};
int sst[2] = {0, 0};

int dalje = 1;
int ukupno = 0;

int dodaj(int x, int y, pi *niz, int st, int vs) {
    int tx = x-1, ty = y;
    int uk = 0;

    if (matrica[tx][ty] == -1 || (vs == -3 && matrica[tx][ty] == -2)) {
        matrica[tx][ty] = vs;
        niz[st++] = {tx, ty};
        uk++;
    }

    tx = x+1, ty = y;
    if (matrica[tx][ty] == -1 || (vs == -3 && matrica[tx][ty] == -2)) {
        matrica[tx][ty] = vs;
        niz[st++] = {tx, ty};
        uk++;
    }

    tx = x, ty = y-1;
    if (matrica[tx][ty] == -1 || (vs == -3 && matrica[tx][ty] == -2)) {
        matrica[tx][ty] = vs;
        niz[st++] = {tx, ty};
        uk++;
    }

    tx = x, ty = y+1;
    if (matrica[tx][ty] == -1 || (vs == -3 && matrica[tx][ty] == -2)) {
        matrica[tx][ty] = vs;
        niz[st++] = {tx, ty};
        uk++;
    }

    return uk;
}

int main()
{
    int n, m;
    cin >> n >> m;

    for (int i = 1; i<=n; i++) {
        for (int j = 1; j<=m; j++) {
            char c;
            cin >> c;

            if (c == '*') matrica[i][j] = 0;
            else matrica[i][j] = -1;

            if (c=='S') {
                ssw[sniz][sst[sniz]++] = {i, j};
            } else if (c=='X') {
                vsw[vniz][vst[vniz]++] = {i, j};
            }
        }
    }

    while (dalje) {
        for (int i = 0; i<sst[sniz]; i++) {
            pi p = ssw[sniz][i];
            int x = p.first; 
            int y = p.second;
            
            if (matrica[x][y] == -3) continue;
            matrica[x][y] = 1;
            ukupno++;
            int dodato = dodaj(x, y, ssw[!sniz], sst[!sniz], -2);
            sst[!sniz] += dodato;
        }
        sst[sniz] = 0;
        sniz = !sniz;
        if (sst[sniz] == 0) dalje = 0;

        for (int i = 0; i<vst[vniz]; i++) {
            pi p = vsw[vniz][i];
            int x = p.first; 
            int y = p.second;

            matrica[x][y] = 1;
            int dodato = dodaj(x, y, vsw[!vniz], vst[!vniz], -3);
            vst[!vniz] += dodato;
        }
        vst[vniz] = 0;
        vniz = !vniz;
    }

    cout << ukupno;
    return 0;
}