#include <bits/stdc++.h>
using namespace std;

void print_matrix(vector<vector<char>> v)
{
    for(auto a: v){
        for(auto b: a){
            cout<<b<<" ";
        }
        cout<<"\n";
    }
    cout<<"\n";
}

int main()
{
    long long unsigned int n, m;
    cin>>n>>m;
    vector<vector<char>> v(n + 2, vector<char>(m + 2));
    for (long long unsigned int i = 0; i < n + 2; i++)
    {
        for (long long unsigned int j = 0; j < m + 2; j++)
        {
            if (i == 0 || j == 0 || i == n + 1 || j == m + 1)
            {
                v[i][j] = '0';
            }
            else
            {
                cin >> v[i][j];
            }
        }
    }

    bool found_space = true;

    while (found_space)
    {

        for (long long unsigned int i = 1; i < n + 1; i++)
        {
            for (long long unsigned int j = 1; j < m + 1; j++)
            {
                if (v[i][j] == 'X')
                {
                    if (v[i - 1][j] == '.')
                        v[i - 1][j] = 'x';
                    if (v[i + 1][j] == '.')
                        v[i + 1][j] = 'x';
                    if (v[i][j - 1] == '.')
                        v[i][j - 1] = 'x';
                    if (v[i][j + 1] == '.')
                        v[i][j + 1] = 'x';
                }
            }
        }

        for (long long unsigned int i = 1; i < n + 1; i++)
        {
            for (long long unsigned int j = 1; j < m + 1; j++)
            {
                if (v[i][j] == 'x')
                {
                    v[i][j] = 'X';
                }
            }
        }

        for (long long unsigned int i = 1; i < n + 1; i++)
        {
            for (long long unsigned int j = 1; j < m + 1; j++)
            {
                if (v[i][j] == 'S')
                {
                    if (v[i - 1][j] == '.')
                        v[i - 1][j] = 's';
                    if (v[i + 1][j] == '.')
                        v[i + 1][j] = 's';
                    if (v[i][j - 1] == '.')
                        v[i][j - 1] = 's';
                    if (v[i][j + 1] == '.')
                        v[i][j + 1] = 's';
                }
            }
        }

        for (long long unsigned int i = 1; i < n + 1; i++)
        {
            for (long long unsigned int j = 1; j < m + 1; j++)
            {
                if (v[i][j] == 's')
                {
                    v[i][j] = 'S';
                }
            }
        }
        found_space = false;
        for (long long unsigned int i = 1; i < n + 1; i++)
        {
            for (long long unsigned int j = 1; j < m + 1; j++)
            {
                if (v[i][j] == '.')
                {
                    found_space = true;
                    goto balls;
                }
            }
        }

    balls:
    //print_matrix(v);
    }
    long long unsigned int num_of_S = 0;
    for (long long unsigned int i = 1; i < n + 1; i++)
    {
        for (long long unsigned int j = 1; j < m + 1; j++)
        {
            if (v[i][j] == 'S')
            {
                num_of_S++;
            }
        }
    }
    cout << num_of_S;
}