#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>


void kopirajMatricu(int N, int M, char L[N][M], char temp[N][M]) {
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < M; j++) {
            temp[i][j] = L[i][j];
        }
    }
}


void vatra(int N, int M, char L[N][M])
{
    int i,j;
    char temp[N][M];
    kopirajMatricu(N, M, L, temp);

    for (int i = 0; i < N; i++) {
        for (int j = 0; j < M; j++) {
            if (L[i][j] == 'X') {
                if (i > 0 && temp[i-1][j] == '.') temp[i-1][j] = 'X';
                if (i < N-1 && temp[i+1][j] == '.') temp[i+1][j] = 'X';
                if (j > 0 && temp[i][j-1] == '.') temp[i][j-1] = 'X';
                if (j < M-1 && temp[i][j+1] == '.') temp[i][j+1] = 'X';
            }
        }
    }

    kopirajMatricu(N, M, temp, L);
}

void Steve(int N, int M, char L[N][M])
{
    int i,j;
    char temp[N][M];
    kopirajMatricu(N, M, L, temp);

    for (int i = 0; i < N; i++) {
        for (int j = 0; j < M; j++) {
            if (L[i][j] == 'S') {
                if (i > 0 && temp[i-1][j] == '.') temp[i-1][j] = 'S';
                if (i < N-1 && temp[i+1][j] == '.') temp[i+1][j] = 'S';
                if (j > 0 && temp[i][j-1] == '.') temp[i][j-1] = 'S';
                if (j < M-1 && temp[i][j+1] == '.') temp[i][j+1] = 'S';
            }
        }
    }

    kopirajMatricu(N, M, temp, L);
}

int count(int N, int M, char L[N][M])
{
int i,j;
int br = 0;
    for(i=0 ; i < N ; i++)
    {
        for(j=0 ; j < M ; j++)
        {
            if(L[i][j]=='S')
            {
                br++;
            }
        }
    }
    return br;
}

int main()
{
    int i,j,N,M;
    scanf("%i %i",&N,&M);

    char L[N][M];
    for(i=0 ; i < N ; i++)
    {
        for(j=0 ; j < M ; j++)
        {
            scanf(" %c",&L[i][j]);
        }
    }

    int iteracije = 0;
    bool promene;
    do {
        promene = false;

        char pre[N][M];
        kopirajMatricu(N, M, L, pre);

        vatra(N, M, L);
        Steve(N, M, L);

        for (int i = 0; i < N; i++) {
            for (int j = 0; j < M; j++) {
                if (L[i][j] != pre[i][j]) {
                    promene = true;
                }
            }
        }
        iteracije++;
    } while (promene && iteracije < N * M);

    printf("%i",count(N,M,L));

    return 0;
}