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


int popuniPolje(int i, int j, int u, int m, int n, vector<int>& mat){
    int pos =i*m+j;
    if(mat[pos]==1000001 || mat[pos]>u || u==0){
        mat[pos]=u;
    if(i-1>=0 && mat[pos-m]>u+1)
        popuniPolje(i-1,j,u+1,m,n,mat);
    if(i+1<n && mat[pos+m]>u+1)
        popuniPolje(i+1,j,u+1,m,n,mat);
    if(j-1>=0 && mat[pos-1]>u+1)
        popuniPolje(i,j-1,u+1,m,n,mat);
    if(j+1<m && mat[pos+1]>u+1)
        popuniPolje(i,j+1,u+1,m,n,mat);}
    return 0;

}

int main() {

    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int n,m,xs,ys;
    cin>>n>>m;
    vector <int> mat;
    stack <int> xv, yv;
    char c;
    int mb = 1000001;
    for(int i =0; i<n; i++){
        for(int j =0; j<m; j++){
            cin>>c;
            if(c=='.')
                mat.push_back(mb);
            else if(c=='*')
                mat.push_back(-1);
            else if(c=='X'){
                mat.push_back(0);
                xv.push(i);
                yv.push(j);}
            else{
                mat.push_back(mb);
                xs=i;
                ys=j;
            }
        }
    }
    vector <int> mats = mat;
    mats[xs*m+ys] = 0;
    while(!xv.empty()){
        popuniPolje(xv.top(), yv.top(),0,m,n, mat);
        xv.pop();
        yv.pop();
    }
    popuniPolje(xs, ys,0,m,n, mats);
    int br =0;
    for(int i =0; i<n; i++){
        for(int j =0; j<m; j++){
            if (mats[i*m+j]<mat[i*m+j])
                br++;
        }
    }
    cout<<br;
    return 0;
}