#include <iostream>

using namespace std;

int main()
{
    int n,m;
    scanf("%d %d",&n,&m);
    char lavirint[1000][1000];
    for (int i = 0; i < n; i++) 
    {
        scanf("%s",lavirint[i]);
    }
    int br=1;
    bool ok = true;
    while (ok)
    {
        ok=false;
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < m; j++)
            {
                if (lavirint[i][j] == 'X') 
                {
                    if (i + 1 < n && lavirint[i + 1][j] == '.')
                    {
                        lavirint[i + 1][j] = 'v';
                    }
                    if (i - 1 >= 0 && lavirint[i - 1][j] == '.')
                    { 
                        lavirint[i - 1][j] = 'v';
                    }
                    if (j + 1 < m && lavirint[i][j + 1] == '.') 
                    {
                        lavirint[i][j + 1] = 'v';
                    }
                    if (j - 1 >= 0 && lavirint[i][j - 1] == '.')
                    {
                        lavirint[i][j - 1] = 'v';
                    }
                }
                else if (lavirint[i][j] == 's') 
                {
                    lavirint[i][j] = 'S';
                }
            }
        }
        for (int i = 0; i < n; i++) 
        {
            for (int j = 0; j < m; j++) 
            {
                if (lavirint[i][j] == 'S') 
                {
                    if (i + 1 < n && lavirint[i + 1][j] == '.') 
                    {
                        lavirint[i + 1][j] = 's';
                        br++;
                        ok = true;
                    }
                    if (i - 1 >= 0 && lavirint[i - 1][j] == '.')
                    {
                        lavirint[i - 1][j] = 's';
                        br++;
                        ok = true;
                    }
                    if (j + 1 < m && lavirint[i][j + 1] == '.') 
                    {
                        lavirint[i][j + 1] = 's';
                        br++;
                        ok = true;
                    }
                    if (j - 1 >= 0 && lavirint[i][j - 1] == '.')
                    {
                        lavirint[i][j - 1] = 's';
                        br++;
                        ok = true;
                    }
                } 
                else if (lavirint[i][j] == 'v')
                {
                    lavirint[i][j] = 'X';
                }
            }
        }
    }
    printf("%d\n",br);
    return 0;
}