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

int main(){
    int w,h;
    cin>>w>>h;
    vector<string> c(h);
    const long long int INF=1e15;
    vector<vector<long long int>> spr(h,vector<long long int>(w,INF));
    queue<pair<int,int>> que;
    pair<int,int> s;
    pair<int,int> g;
    for(int i=0;i<h;i++){
        cin>>c[i];
        for(int j=0;j<w;j++){
            if(c[i][j]=='s'){
                s={i,j};
                c[i][j]='.';
            }
            if(c[i][j]=='g'){
                g={i,j};
            }
            if(c[i][j]=='*'){
                spr[i][j]=0;
                que.push({i,j});
            }
        }
    }

    int dh[]={1,-1,0,0};
    int dw[]={0,0,1,-1};
    while(!que.empty()){
       auto p=que.front();
       que.pop();
       for(int i=0;i<4;i++){
        int th=p.first+dh[i];
        int tw=p.second+dw[i];
        if(c[th][tw]=='.' && spr[p.first][p.second]+1<spr[th][tw]){
            spr[th][tw]=spr[p.first][p.second]+1;
            que.push({th,tw});
        }
       }
    }
    vector<vector<long long int>> goal(h,vector<long long int>(w,INF));
    goal[g.first][g.second]=0;
    que.push({g.first,g.second});
    while(!que.empty()){
        auto p=que.front();
        que.pop();
        for(int i=0;i<4;i++){
            int th=p.first+dh[i];
            int tw=p.second+dw[i];
            if(c[th][tw]=='.' && goal[p.first][p.second]+1<goal[th][tw]){
            goal[th][tw]=goal[p.first][p.second]+1;
            que.push({th,tw});
            }
        }
       
    }
    int fcnt=0;
    long long int sum1=0;
    long long int sum2=0;
    vector<pair<long long int,pair<int,int>>> ex;
    for(int i=0;i<h;i++){
        for(int j=0;j<w;j++){
            if(c[i][j]=='.'){
                fcnt++;
                if(!(goal[i][j]!=INF || spr[i][j]!=INF)){
                    cout<<goal[s.first][s.second]<<endl;
                    return 0;
                }
                long long int dis=goal[i][j]-spr[i][j];
                if(dis<=0){
                    sum1+=goal[i][j];
                }
                else{
                    sum2+=spr[i][j];
                    ex.push_back({dis,{i,j}});
                }
            
            }
        }
    }
    sort(ex.begin(),ex.end());
    double resex=-1;
    int inde=0;
    
    for(int x=1;x<=1000000;x++){//ex=[x,x+1)
        for(;inde<ex.size();inde++){
            if(ex[inde].first<=x){
                int i=ex[inde].second.first;
                int j=ex[inde].second.second;
                sum1+=goal[i][j];
                sum2-=spr[i][j];
            }
            else{
                break;
            }
        }
        const double EPS=1e-12;
        if(ex.size()==inde){
            double xa=double(sum1)/fcnt;
            if(x-EPS<xa){
                resex=xa;
            }
            break;
        }
        double xa=double(sum1+sum2)/fcnt/(1-(double(ex.size()-inde)/fcnt));
        if(x-EPS<xa && xa<=x+1-EPS || (x==1000000 &&  x-EPS<xa)){
            assert(resex==-1);
            resex=xa;
        }
    }
    assert(resex!=-1);
    
    cout<<fixed<<setprecision(10);
    cout<<min<double>(goal[s.first][s.second],spr[s.first][s.second]+resex)<<endl;
    return 0;   
}

Measured memory_bytes: 11220