#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

typedef int _loop_int;
#define REP(i,n) for(_loop_int i=0;i<(_loop_int)(n);++i)
#define FOR(i,a,b) for(_loop_int i=(_loop_int)(a);i<(_loop_int)(b);++i)
#define FORR(i,a,b) for(_loop_int i=(_loop_int)(b)-1;i>=(_loop_int)(a);--i)

#define DEBUG(x) cout<<#x<<": "<<x<<endl
#define DEBUG_VEC(v) cout<<#v<<":";REP(i,v.size())cout<<" "<<v[i];cout<<endl
#define ALL(a) (a).begin(),(a).end()

#define CHMIN(a,b) a=min((a),(b))
#define CHMAX(a,b) a=max((a),(b))

int h,w,a,b;
char mp[1252][1252];
ll dist[1252][1252];

int dx[] = {0,1, 0,-1,1, 1,-1,-1};
int dy[] = {1,0,-1, 0,1,-1,-1, 1};
int mu[] = {1,1, 1, 1,2, 2, 2, 2};

int main(){
  scanf("%d%d%d%d",&h,&w,&a,&b);
  REP(i,h)scanf("%s",mp[i]);
  REP(i,h)REP(j,w)dist[i][j] = 1ll<<60;
  int si = 0, sj = 0;
  int gi = 0, gj = 0;
  REP(i,h)REP(j,w){
    if(mp[i][j]=='s')si=i,sj=j;
    if(mp[i][j]=='g')gi=i,gj=j;
  }

  priority_queue<pair<ll,pii>> Q;
  Q.push(make_pair(-0ll, pii(si,sj)));
  dist[si][sj] = 0ll;
  while(!Q.empty()){
    pair<ll,pii> PP = Q.top(); Q.pop();
    int i = PP.second.first;
    int j = PP.second.second;
    if(-PP.first != dist[i][j])continue;
    bool can = true;
    if(mp[i][j]=='*')can=false;
    REP(dir,8){
      int ni = i + dy[dir];
      int nj = j + dx[dir];
      if(ni<0 || nj<0 || ni>=h || nj>=w)continue;
      if(mp[ni][nj]=='*')can=false;
    }
    REP(dir,8){
      int ni = i + dy[dir];
      int nj = j + dx[dir];
      ll nc = dist[i][j] + mu[dir]*a;
      if(ni<0 || nj<0 || ni>=h || nj>=w)continue;
      if(mp[ni][nj]=='#' || mu[dir]==2){
        if(!can)continue;
        nc += b;
      }
      if(nc < dist[ni][nj]){
        dist[ni][nj] = nc;
        Q.push(make_pair(-nc, pii(ni,nj)));
      }
    }
  }
  // REP(i,h){
  //   REP(j,w)printf("%lld ",dist[i][j]);puts("");
  // }
  if(dist[gi][gj]==1ll<<60){
    puts("INF");
  }else{
    printf("%lld\n",dist[gi][gj]);
  }
  return 0;
}

