#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define mp make_pair
#define pb push_back
#define F first
#define S second
#define debug(x) std::cout << #x << ": " << x << "\n"
#define all(v) v.begin(), v.end()
#define li(i,a,b) for (int (i) = (a); (i) < (b); (i)++)
#define endl '\n'
#define mem(name,val) memset(name,val,sizeof(name))
#define min(a,b) (a<=b ? a : b)
#define max(a,b) (a>=b ? a : b)
//using u64 = uint64_t;
//using u128 = __uint128_t;

int main()
{
    ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);    
    int n,m;
    cin>>n>>m;
    char w[n][m];

    deque<pair<int,int>> bfs;

    li(i,0,n){
        li(j,0,m){
            cin>>w[i][j];
            if(w[i][j] == 'X'){
                bfs.push_front(mp(i,j));
            }
            if(w[i][j] == 'S'){
                bfs.push_back(mp(i,j));
            }
        }
    }

    vector<pair<int,int>> dir = { {1,0}, {-1, 0}, {0, 1}, {0, -1} };
    while(!bfs.empty()){
        auto curr = bfs.front(); bfs.pop_front();

        for(auto ds : dir){
            int newy = curr.first + ds.first;
            int newx = curr.second + ds.second;

            if(!(newx < 0 or newx > m-1 or newy < 0 or newy > n-1)){
                if(w[newy][newx] == '.'){
                    w[newy][newx] = w[curr.first][curr.second];
                    bfs.push_back(mp(newy,newx));
                }
            }
        }
    }

    int cnt = 0;
    li(i,0,n){
        li(j,0,m){
            if(w[i][j] == 'S') cnt++;
        }
    }
    cout<<cnt;

    return 0;
}