#include <iostream>
#include <bits/stdc++.h>
#include <string>
typedef unsigned long long int ull;
typedef long long int ll;
using namespace std;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cerr.tie(0);

    int N, M;
    cin >> N >> M;
    int i, j, x, y, x_novi, y_novi, resenje = 0;

    int mapa[N][M];

    queue<int> X;
    queue<int> Y;
    char c;
    int xs = 0, ys = 0;
    vector<int> xvatre;
    vector<int> yvatre;

    for(i=0; i<N; i++)
    {
        for(j=0; j<M; j++)
        {
            cin >> c;
            if(c == '*')
            {
                mapa[i][j] = -10;
            }
            else if(c == 'S')
            {
                mapa[i][j] = -5;
                xs = i;
                ys = j;
            }
            else if(c == 'X')
            {
                mapa[i][j] = 0;
                X.push(i);
                Y.push(j);
            }
            else //slobodno polje
            {
                mapa[i][j] = -1;
            }
        }
    }
    int pom_x[4] = {1, 0, -1, 0};
    int pom_y[4] = {0, 1, 0, -1};

    while(!X.empty())
    {
        x = X.front();
        y = Y.front();
        X.pop(); Y.pop();
        for(i=0; i<4; i++)
        {
            x_novi = x + pom_x[i];
            y_novi = y + pom_y[i];
            if(0 <= x_novi && x_novi < N && 0 <= y_novi && y_novi < M &&
            (mapa[x_novi][y_novi] == -1 || mapa[x][y] + 1 < mapa[x_novi][y_novi]))
            {
                X.push(x_novi);
                Y.push(y_novi);
                mapa[x_novi][y_novi] = mapa[x][y] + 1;
            }
        }
    }
    X.push(xs);
    Y.push(ys);
    resenje = 1;
    mapa[xs][ys] = 0;
    while(!X.empty())
    {
        x = X.front();
        y = Y.front();
        X.pop(); Y.pop();
        for(i=0; i<4; i++)
        {
            x_novi = x + pom_x[i];
            y_novi = y + pom_y[i];
            if(0 <= x_novi && x_novi < N && 0 <= y_novi && y_novi < M &&
            (mapa[x_novi][y_novi] == -1 || mapa[x][y] + 1 < mapa[x_novi][y_novi]))
            {
                resenje++;
                X.push(x_novi);
                Y.push(y_novi);
                mapa[x_novi][y_novi] = mapa[x][y] + 1;
            }
        }
    }
    cout << resenje;

    /*for(i=0; i<N; i++)
    {
        for(j=0; j<M; j++)
        {
            cout << mapa[i][j] << " ";
        }
        cout << endl;
    }

    cout << endl;*/
}