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 <bits/stdc++.h>
using namespace std;
using i64 = long long;
#define rep(i,s,e) for(int (i) = (s);(i) <= (e);(i)++)
#define all(x) x.begin(),x.end()

int H,W;
i64 A,B;

int fie[1010][1010];
bool able[1010][1010];


int sx,sy;
int gx,gy;

int main() {
  cin >> H >> W >> A >> B;
  for(int i = 0;i < H;i++) {
    string s;
    cin >> s;
    for(int j = 0;j < W;j++) {
      if(s[j] == 's') {
        sx = i;
        sy = j;
      }
      else if(s[j] == 'g') {
        gx = i;
        gy = j;
      }
      else if(s[j] == '*') {
        fie[i][j] = -1;
      }
      else if(s[j] == '#') {
        fie[i][j] = 1;
      }
    }
  }
  for(int i = 0;i < H;i++) {
    for(int j = 0;j < W;j++) {
      bool OK = true;
      for(int x = max(0, i - 1);x <= min(H - 1, i + 1);x++) {
        for(int y = max(0, j - 1);y <= min(W - 1, j + 1);y++) {
          if(fie[x][y] == -1) OK = false;
        }
      }
      if(OK) {
        able[i][j] = true;
      }
    }
  }

  using P = pair<i64,pair<int,int>>;

  vector<vector<i64>> dist(H, vector<i64>(W, 1e18));

  priority_queue<P,vector<P>,greater<P>> que;

  dist[sx][sy] = 0;
  que.push({dist[sx][sy], {sx, sy}});

  int dd[] = {1, 0, -1, 0};

  while(!que.empty()) {
    int x = que.top().second.first;
    int y = que.top().second.second;
    i64 d = que.top().first;
    que.pop();

    if(dist[x][y] < d) continue;

    for(int c = 0;c < 4;c++) {
      int i = x + dd[c];
      int j = y + dd[c ^ 1];
      if(0 <= i && i < H && 0 <= j && j < W && (fie[i][j] == 0 || fie[i][j] == -1)) {
        if(dist[i][j] > d + A) {
          dist[i][j] = d + A;
          que.push({dist[i][j], {i , j}});
        }
      }
    }
    if(able[x][y]) {
      for(int i = max(0, x - 1);i <= min(H - 1, x + 1);i++) {
        for(int j = max(0, y - 1);j <= min(W - 1, y + 1);j++) {
          if(i == x && j == y) continue;
          int cc = abs(i - x) + abs(j - y);
          if(dist[i][j] > d + A * cc + B) {
            dist[i][j] = d + A * cc + B;
            que.push({dist[i][j], {i , j}});
          }
        }
      }
    }
  } 
  if(dist[gx][gy] == 1e18) {
    cout << "INF" << endl;
  }
  else {
    cout << dist[gx][gy] << endl;
  }
}