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

int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};

char mat[1010][1010];
int visited[1010][1010];

int32_t main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);

    int n, m; cin >> n >> m;


    queue<tuple<int, int, int> > bfs;
    pair<int, int> start;
    for(int i = 0; i < n; i++){
        for(int j = 0; j < m; j++){
            visited[i][j]=0;
            cin >> mat[i][j];
            if(mat[i][j]=='X'){
                bfs.push({i, j, 2});
                visited[i][j]=2;
            }
            if(mat[i][j]=='S') start = {i, j};
        }
    }
    bfs.push({start.first, start.second, 1});
    visited[start.first][start.second]=1;
    int ans = 1;
    while(!bfs.empty()){
        int x = get<0>(bfs.front());
        int y = get<1>(bfs.front());
        int type = get<2>(bfs.front());
        bfs.pop();
        for(int d = 0; d < 4; d++){
            int nx = x+dx[d];
            int ny = y+dy[d];
            if(nx<0 || nx>=n) continue;
            if(ny<0 || ny>=m) continue;
            if(visited[nx][ny]>0) continue;
            if(mat[nx][ny]=='*') continue;
            visited[nx][ny]=type;
            bfs.push({nx, ny, type});
            if(type==1) ans++;

        }
    }

    cout << ans;

    return 0;
}