#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
    int N, M, s = 1;
    cin >> N >> M;
    char polja[N][M], polja1[N][M];
    for (int i = 0; i < N; i++)
        for (int j = 0; j < M; j++)
            cin >> polja[i][j];
    for (int i = 0; i < N; i++)
        for (int j = 0; j < M; j++)
            polja1[i][j] = polja[i][j];
    bool loop = true;
    cout << endl;
    while (loop)
    {
        bool novi_potez = false;
        for (int i = 0; i < N; i++)
            for (int j = 0; j < M; j++)
            {
                if (polja[i][j] == 'X')
                {
                    if (i-1>=0&&polja[i-1][j]=='.')
                    {
                        polja1[i-1][j] = 'X';
                        if (!novi_potez) novi_potez =  true;
                    }
                    if (i+1<N&&polja[i+1][j]=='.')
                    {
                        polja1[i+1][j] = 'X';
                        if (!novi_potez) novi_potez =  true;
                    }
                    if (j-1>=0&&polja[i][j-1]=='.')
                    {
                        polja1[i][j-1] = 'X';
                        if (!novi_potez) novi_potez =  true;
                    }
                    if (j+1<N&&polja[i][j+1]=='.')
                    {
                        polja1[i][j+1] = 'X';
                        if (!novi_potez) novi_potez =  true;
                    }
                }
            }
        for (int i = 0; i < N; i++)
            for (int j = 0; j < M; j++)
                polja[i][j] = polja1[i][j];
        if (!novi_potez) loop = false;
        for (int i = 0; i < N; i++)
            for (int j = 0; j < M; j++)
            {
                if (polja[i][j] == 'S')
                {
                    if (i-1>=0&&polja[i-1][j]=='.')
                    {
                        polja1[i-1][j] = 'S';
                        if (!novi_potez) novi_potez =  true;
                    }
                    if (i+1<N&&polja[i+1][j]=='.')
                    {
                        polja1[i+1][j] = 'S';
                        if (!novi_potez) novi_potez =  true;
                    }
                    if (j-1>=0&&polja[i][j-1]=='.')
                    {
                        polja1[i][j-1] = 'S';
                        if (!novi_potez) novi_potez =  true;
                    }
                    if (j+1<N&&polja[i][j+1]=='.')
                    {
                        polja1[i][j+1] = 'S';
                        if (!novi_potez) novi_potez =  true;
                    }
                }
            }
        for (int i = 0; i < N; i++)
            for (int j = 0; j < M; j++)
                polja[i][j] = polja1[i][j];
        if (!novi_potez) loop = false;
    }
    for (int i = 0; i < N; i++)
    {
        for (int j = 0; j < M; j++)
            s += 1*(polja[i][j]=='S');
    }
    cout << s <<endl;
    return 0;
}