Problem: Problem

Kotatsu turtle is a turtle with a kotatsu shell.


As soon as Kotatsu was pokita (getting up), he was trapped in a stage divided into grid-like sections. There is only one exit on this stage. The parcel is either a road, a fence or a bomb, and the kotatsu can move up, down, left and right to enter the parcel of the road or bomb. It cannot be moved diagonally. In addition, the fence section cannot be passed. The road section extends infinitely outside the stage, but it cannot be passed because there is a crocodile, the natural enemy of the turtle.

Kotatsu wants to go home (sleep) quickly, so I decided to head to the exit of this stage.

The kotatsu turtle, which can freely manipulate the infrared rays of the kotatsu, has the ability to turn eight sections around it into a road at the same time. However, if the 8 sections and one of the sections where you are located are bomb sections, the kotatsu will burn up when you explode, and you will not even be able to use the famous SNS "Kametta", so you will activate the ability. I can't do that.

<image>

An example is illustrated. The section where the illustration is not placed and the section where the kotatsu is placed are the sections of the road. The eight compartments around the kotatsu are the orange compartments. In the figure on the left, the 8 compartments around the kotatsu and the compartment where you are are not included in the bomb compartment, so you can activate the ability. However, in the figure on the right, the ability cannot be activated because the bomb compartment is included in either the surrounding 8 compartments or the compartment where you are.

The cost to move is $ A $ for each parcel move, and the cost to activate the ability is $ B $ each time. Please note that it is not the cost per wall turned into a road. The starting point and the exit of the stage are roads. Output the minimum sum of the cost of moving from the starting point to the exit of the stage and the cost of activating the ability. However, if you cannot reach the exit of the stage, output "INF" instead.

Constraints

The input satisfies the following conditions.

* $ 1 \ le H, W, A, B \ le 1000 $
* $ 3 \ le H + W $
* $ c_ {i, j} \ in $ {'s','g','.','#','*'}
*'s' and'g' appear once each

Input

The input is given in the following format.


$ H $ $ W $ $ A $ $ B $
$ c_ {1,1} \ cdots c_ {1, W} $
$ \ vdots $
$ c_ {H, 1} \ cdots c_ {H, W} $


The input consists of $ H + 1 $ lines.
On the $ 1 $ line, there are an integer $ H $ that represents the vertical length, an integer $ W $ that represents the left and right length, an integer $ A $ that represents the cost to move, and an integer $ B $ that represents the cost to activate the ability. Each is given with a blank delimiter.
The $ H $ line from the $ 2 $ line is given the state $ c_ {i, j} $ in each section of the stage where the kotatsu is confined.
$ c_ {i, j} $ consists of one of's','g','.','#','*', Each, and the partition $ (i, j) $ is in the following state. Represents that.
's': Indicates that the section is the starting point.
'g': Indicates that the parcel is the exit of the stage.
'.': Indicates that the section is a road.
'#': Indicates that the section is a fence.
'*': Indicates that the parcel is a bomb.

Output

Output the minimum sum of the cost of moving from the starting point to the exit of the stage and the cost of activating the ability. However, if you cannot reach the exit of the stage, output "INF" instead.

Examples

Input

4 4 1 1
g#..
#...
.*..
...s


Output

7


Input

4 4 1 1
g#..
...
.*..
...s


Output

7


Input

4 4 1 1
g#..
*..
....
...s


Output

INF


Input

2 4 1 1
g
s###


Output

6


Input

3 3 1 10
g..
.
s..


Output

6


Input

3 3 10 1
g..
.
s..


Output

21

Solution (in CPP):

Solution:
#include <iostream>
#include <algorithm>
#include <vector>
#include <bitset>
#include <queue>

using namespace std;

#define INF 1<<29
#define MAX_H 1024
#define MAX_W 1024
#define MAX_V (MAX_H * MAX_W)

typedef pair<int, int>  P;

vector<P>G[MAX_V];

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

int d[MAX_V];

void dijkstra(int s){
  priority_queue<P, vector<P>, greater<P> > que;
  fill(d, d + MAX_V, INF);
  d[s] = 0;
  que.push(P(0, s));

  while(!que.empty()){
    P p = que.top(); que.pop();
    int v = p.second;
    if(d[v] < p.first) continue;
    for(int i = 0; i < G[v].size(); i++){
      P e = G[v][i];
      if(d[e.second] > d[v] + e.first){
	d[e.second] = d[v] + e.first;
	que.push(P(d[e.second], e.second));
      }
    }
  }
}


int main(){
  int H, W, A, B;
  cin >> H >> W >> A >> B;
  auto vec2idx = [&](int i, int j){ return i*W + j; };
  auto get_i = [&](int idx){ return int(idx/W); };
  auto get_j = [&](int idx){ return idx%W; };
  auto is_out_of_stage = [&](int i, int j){
			   if(i < 0 || j < 0 || i >= H || j >= W) return 1;
			   return 0;
			 };
  int start, goal;
  string stage[1024];
  for(int i=0;i<H;i++){
    cin >> stage[i];
  }

  for(int i=0;i<H;i++){
    for(int j=0;j<W;j++){
      if(stage[i][j] == 'g'){
	goal = i*W + j;
      }
      if(stage[i][j] == 's'){
	start = i*W + j;
      }
      auto can_use_gift = [&](int a, int b){
			    int dy[] = {0, -1, 0, -1, 1, 0, 1, -1, 1};
			    int dx[] = {0, 0, -1, -1, 0, 1, 1, 1, -1};
			    for(int k=0;k<9;k++){
			      if(is_out_of_stage(a+dy[k], b+dx[k])) continue;
			      if(stage[a+dy[k]][b+dx[k]] == '*') return 0;
			    }
			    return 1;
			  };

      int idx = vec2idx(i, j);      
      if(can_use_gift(i, j)){
	//	cout << i << " " << j << endl;
	int di[] = {-1, 0, 1, 0};
	int dj[] = { 0, -1, 0, 1};
	for(int k=0;k<4;k++){
	  int next_i = i + di[k];
	  int next_j = j + dj[k];
	  if(is_out_of_stage(next_i, next_j)) continue;
	  int next_idx = vec2idx(next_i, next_j);
	  G[idx].push_back(make_pair(A + B, next_idx));
	}

	int ddi[] = {-1, 1, -1, 1};
	int ddj[] = {-1, -1, 1, 1};
	for(int k=0;k<4;k++){
	  int next_i = i + ddi[k];
	  int next_j = j + ddj[k];
	  if(is_out_of_stage(next_i, next_j)) continue;	  
	  int next_idx = vec2idx(next_i, next_j);
	  G[idx].push_back(make_pair(2*A + B, next_idx));
	  //	  cout << idx << " " << next_idx << " " << 2*A + B << endl;
	}
      }
      int di[] = {-1, 0, 1, 0};
      int dj[] = {0, -1, 0, 1};
      for(int k=0;k<4;k++){
	int next_i = i + di[k];
	int next_j = j + dj[k];
	if(is_out_of_stage(next_i, next_j)) continue;	
	if(stage[next_i][next_j] == '#') continue;
	int next_idx = vec2idx(next_i, next_j);
	G[idx].push_back(make_pair(A, next_idx));
      }
    }
  }
  for(int i=0;i<H*W;i++){
    for(int j=0;j<G[i].size();j++){
      //      cout << i << " " << G[i][j].second << " " << G[i][j].first << endl;
    }
  }
  dijkstra(start);
  if(d[goal] == INF){
    cout << "INF" << endl;
  }else{
    cout << d[goal] << endl;
  }
  return 0;
}