#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long

using namespace std;


int main()
{
    ios_base::sync_with_stdio(false);

    int n, m;
    cin >> n >> m;
    char c;
    vector< vector<int> > prohodno(n, vector<int>(m, 0));
    queue< tuple<int, int, int> > q;
    int stevaX, stevaY;
    for(int i = 0; i < n; i++){
        for(int j = 0; j < m; j++){
            cin >> c;
            if(c == '.'){
                prohodno[i][j] = 1;
            }else if(c == 'X'){
                prohodno[i][j] = 0;
                q.emplace(0, i, j);
            }else if(c == '*'){
                prohodno[i][j] = 0;
            }else{
                prohodno[i][j] = 0;
                stevaX = i;
                stevaY = j;
            }
        }
    }
    q.emplace(1, stevaX, stevaY);
    int cnt = 1;
    int dx[] = {0, 1, 0, -1};
    int dy[] = {-1, 0, 1, 0};
    while(!q.empty()){
        int steva, i, j;
        tie(steva, i, j) = q.front();
        q.pop();
        for(int d = 0; d < 4; d++){
            int xx = i + dx[d], yy = j + dy[d];
            if(xx >= 0 && xx < n && yy >= 0 && yy < m && prohodno[xx][yy]){
                q.emplace(steva, xx, yy);
                prohodno[xx][yy] = 0;
                cnt += steva;
            }
        }
    }
    cout << cnt << endl;
}