import scala.collection.mutable
object Main extends App {
  import scala.io.StdIn._


  val Array(h, w, a, b) = readLine.trim.split(' ').map(_.toInt)
  val s = Array.tabulate(h){_ ⇒ readLine.trim}
  val start = (0 until h).flatMap(y ⇒ (0 until w).find(x ⇒ s(y)(x) == 's').map(x ⇒ Coordinate(x, y))).head
  val goal = (0 until h).flatMap(y ⇒ (0 until w).find(x ⇒ s(y)(x) == 'g').map(x ⇒ Coordinate(x, y))).head
  val state = s.map(_.map{
    case '#' ⇒ Wall
    case '*' ⇒ Bomb
    case _ ⇒ Road
  }.toArray)
  val minCost = Array.tabulate(h){_ ⇒ Array.tabulate(w){_ ⇒ Long.MaxValue}}
  val queue = mutable.PriorityQueue()(Ordering.by[(Long, Coordinate), Long](_._1).reverse)
  minCost(start.y)(start.x) = 0
  queue.enqueue((0L, start))
  while (queue.nonEmpty && queue.head._2 != goal){
    val (cost, current) = queue.dequeue()
    if (minCost(current.y)(current.x) == cost){
      for (n ← current.neighbor4.filter(_.contains(h, w)) if state(n.y)(n.x) != Wall){
        if (minCost(n.y)(n.x) > minCost(current.y)(current.x) + a){
          minCost(n.y)(n.x) = minCost(current.y)(current.x) + a
          queue.enqueue((minCost(n.y)(n.x), n))
        }
      }
      if (current.neighbor9.forall(c ⇒ !c.contains(h, w) || state(c.y)(c.x) != Bomb)) {
        for (n ← current.neighbor9.filter(_.contains(h, w))) {
          if (minCost(n.y)(n.x) > minCost(current.y)(current.x) + b + a * (math.abs(n.x - current.x) + math.abs(n.y - current.y))) {
            minCost(n.y)(n.x) = minCost(current.y)(current.x) + b + a * (math.abs(n.x - current.x) + math.abs(n.y - current.y))
            queue.enqueue((minCost(n.y)(n.x), n))
          }
        }
      }
    }
  }
  println(
    if (minCost(goal.y)(goal.x) == Long.MaxValue) "INF" else minCost(goal.y)(goal.x)
  )

  sealed trait State
  case object Road extends State
  case object Wall extends State
  case object Bomb extends State
  case class Coordinate(x: Int, y: Int) {
    def neighbor9 = (-1 to 1).flatMap(dx ⇒ (-1 to 1).map(dy ⇒ Coordinate(x + dx, y + dy)))
    def neighbor4 = List(Coordinate(x + 1, y), Coordinate(x - 1, y), Coordinate(x, y + 1), Coordinate(x, y - 1))
    def contains(h: Int, w: Int): Boolean = (0 until h).contains(y) && (0 until w).contains(x)
  }
  implicit class Scope[T](val value: T) extends AnyVal {
    @inline
    def let[A](func: T ⇒ A): A = func(value)
  }
}
