#include<bits/stdc++.h>
using namespace std;
int n, k, res = 1, pozi, pozj, pozxi[1005], pozxj[1005], br;
char m[1005][1005];
queue <int> qr;
queue <int> qc;
queue <int> qvi;
queue <int> qvj;
void bfs(int r, int c){
    qr.push(r);
    qc.push(c);
    m[r][c] = 'S';
    while(qr.size()){
        for(int i = 1; i <= br; i++){
            qvi.push(pozxi[i]);
            qvj.push(pozxj[i]);
            r = qvi.front();
            c = qvj.front();
            if(m[r][c+1] == '.'){
                qvi.push(r);
                qvj.push(c+1);
                m[r][c+1] = 'X';
            }
            if(m[r+1][c] == '.'){
                qvi.push(r+1);
                qvj.push(c);
                m[r+1][c] = 'X';
            }
            if(m[r][c-1] == '.'){
                qvi.push(r);
                qvj.push(c-1);
                m[r][c-1] = 'X';
            }
            if(m[r-1][c] == '.'){
                qvi.push(r-1);
                qvj.push(c);
                m[r-1][c] = 'X';
            }
            qvi.pop();
            qvj.pop();
        }
        r = qr.front();
        c = qc.front();
        if(m[r][c+1] == '.'){
            qr.push(r);
            qc.push(c+1);
            m[r][c+1] = 'S';
        res++;
        }
        if(m[r+1][c] == '.'){
            qr.push(r+1);
            qc.push(c);
            m[r+1][c] = 'S';
        res++;
        }
        if(m[r][c-1] == '.'){
            qr.push(r);
            qc.push(c-1);
            m[r][c-1] = 'S';
        res++;
        }
        if(m[r-1][c] == '.'){
            qr.push(r-1);
            qc.push(c);
            m[r-1][c] = 'S';
        res++;
        }
        qr.pop();
        qc.pop();
    }
}

int main(){
    cin >> n >> k;
    memset(m, '*', sizeof(m));
    for(int i = 1; i <= n; i++){
        for(int j = 1; j <= k; j++){
            cin >> m[i][j];
            if(m[i][j] == 'S'){
                pozi = i;
                pozj = j;
            }
            if(m[i][j] == 'X'){
                br++;
                pozxi[br] = i;
                pozxj[br] = j;
            }
        }
    }
    if(br == 0){
        bfs(pozi, pozj);
    }
    cout << res << endl;
    return 0;
}