#include <iostream>
using namespace std;

const int a1 = 1000;
const int a2 = 1000;
void vatra (char a[a1][a2], int n, int m)
{
    for (int i = 0; i < n; i++)
        for (int j = 0; j < m; j++)
            if (a[i][j]=='Y')
                a[i][j]='X';
}
void zamena (char a[a1][a2], int n, int m, char x,int i, int j)
{
        if(a[i-1][j]=='.')
            a[i-1][j]=x;
        if(a[i+1][j]=='.')
            a[i+1][j]=x;
        if(a[i][j-1]=='.')
            a[i][j-1]=x;
        if(a[i][j+1]=='.')
            a[i][j+1]=x;
}
bool provera (char a[a1][a2],int n, int m)
{
    for (int i = 0; i < n; i++)
        for (int j = 0; j < m; j++)
            if(a[i][j]=='.')
            return true;
    return false;
}
void lavirint (char a[a1][a2], int n, int m)
{
    for (int i = 0; i < n; i++)
        for (int j = 0; j < m; j++)
            if(a[i][j]=='X')
                zamena(a,n,m,'Y',i,j);
    vatra(a,n,m);
    for (int i = 0; i < n; i++)
        for (int j = 0; j < m; j++)
            if(a[i][j]=='S')
        {
            zamena(a,n,m,'S',i,j);
        }
    if (provera(a,n,m))
        lavirint(a,n,m);
}
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    char a[a1][a2];
    int n,m,k=0;
    cin >> n >> m;
    for (int i = 0; i < n; i++)
        for (int j = 0; j < m; j++)
            cin >> a[i][j];
    lavirint(a,n,m);
    for (int i = 0; i < n; i++)
        for (int j = 0; j < m; j++)
            if(a[i][j]=='S')
                k++;
    cout << k << endl;
    return 0;
}