#include <iostream>
#include <vector>
using namespace std;

int main() {
    int n, m;
    cin >> n >> m;
    vector<vector<char>> v(n, vector<char>(m));
    vector<pair<int, int>> kv, ks;

    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            cin >> v[i][j];
            if (v[i][j] == 'S') {
                ks.push_back({i, j});
            } else if (v[i][j] == 'X') {
                kv.push_back({i, j});
            }
        }
    }

    bool pomereno = true;
    int pv = 0;
    int ps = 0;
    while (pomereno) {
        pomereno = false;
        int tv = 0;
        int ts = 0;
        int vv = kv.size();
        int sv = ks.size();
        for (int i = pv; i < vv; i++) {
            int x = kv[i].first, y = kv[i].second;
            if (x > 0 && v[x - 1][y] == '.') {
                v[x - 1][y] = 'X';
                kv.push_back({x - 1, y});
                tv++;
                pomereno = true;
            }
            if (x < n - 1 && v[x + 1][y] == '.') {
                v[x + 1][y] = 'X';
                kv.push_back({x + 1, y});
                tv++;
                pomereno = true;
            }
            if (y > 0 && v[x][y - 1] == '.') {
                v[x][y - 1] = 'X';
                kv.push_back({x, y - 1});
                tv++;
                pomereno = true;
            }
            if (y < m - 1 && v[x][y + 1] == '.') {
                v[x][y + 1] = 'X';
                kv.push_back({x, y + 1});
                tv++;
                pomereno = true;
            }
        }
        for (int i = ps; i < sv; i++) {
            int x = ks[i].first, y = ks[i].second;
            if (x > 0 && v[x - 1][y] == '.') {
                v[x - 1][y] = 'S';
                ks.push_back({x - 1, y});
                ts++;
                pomereno = true;
            }
            if (x < n - 1 && v[x + 1][y] == '.') {
                v[x + 1][y] = 'S';
                ks.push_back({x + 1, y});
                ts++;
                pomereno = true;
            }
            if (y > 0 && v[x][y - 1] == '.') {
                v[x][y - 1] = 'S';
                ks.push_back({x, y - 1});
                ts++;
                pomereno = true;
            }
            if (y < m - 1 && v[x][y + 1] == '.') {
                v[x][y + 1] = 'S';
                ks.push_back({x, y + 1});
                ts++;
                pomereno = true;
            }
        }
        pv = kv.size() - tv;
        ps = ks.size() - ts;
    }
    int broj;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            if(v[i][j] == 'S'){
                broj++;
            }
        }
    }
    cout << broj;
    return 0;
}