#include <iostream>
#include <vector>
#include <queue>
#include <utility>
#include <string>

using namespace std;

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int N, M;
    cin >> N >> M;
    vector<vector<char>>grid;
    queue<pair<short, short>> q,fires;
    pair<short, short> sanyi;
    string temp;
    getline(cin, temp);
    for (int i = 0;i < N;i++) {
        grid.push_back({});
        getline(cin, temp);
        for (int j = 0;j < M;j++) {
            grid[i].push_back(temp[j]);
            if (temp[j] == 'X')fires.push({ i,j });
            else if (temp[j] == 'S')sanyi = { i,j };
        }
    }
    
    q.push(sanyi);
    long long ans = 1;
    pair<short, short> dir[] = { {-1,0},{1,0},{0,-1},{0,1} };
    while (!q.empty()) {
        int wave = q.size();
        for (int i = 0;i < wave;i++) {
            short x = q.front().first, y = q.front().second;
            q.pop();
            for (pair<short, short>& pos : dir) {
                short nx = x + pos.first, ny = y + pos.second;
                if (nx >= 0 && nx < N && ny >= 0 && ny < M && grid[nx][ny] == '.') {
                    q.push({ nx,ny });
                    grid[nx][ny] = 'S';
                    ans++;
                }
            }
        }
        int fwave = fires.size();
        for (int i = 0;i < fwave;i++) {
            short x = fires.front().first, y = fires.front().second;
            fires.pop();
            for (pair<short, short>& pos : dir) {
                short fx = x + pos.first, fy = y + pos.second;
                if (fx >= 0 && fx < N && fy >= 0 && fy < M && grid[fx][fy] == '.') {
                    fires.push({fx,fy});
                    grid[fx][fy] = 'X';
                }
            }
        }
    }
    cout << ans;
}