아직 만들고 있는 페이지입니다.

이 페이지는 아직 만드는 중입니다. 보이는 내용은 바뀔 수 있습니다.

Cape and gun

시간 제한1초메모리 제한256 MB

요약
빈 칸 사이를 활강해 S에서 E까지 지면에 닿지 않고 도달할 수 있는지 판정하고, 그 과정에서 죽일 수 있는 몬스터의 최대 수를 구한다.
난이도

보통10점 중 7점

유형
그래프, DFS, 구현, 그리디
정답자
아직 제출이 없습니다

문제

Gennadiy came up with a new challenge in his favorite game: pass levels without touching the ground.

In the game, a level is a field of cells. Each cell is either empty or filled with ground. In an empty cell, there can be a monster or the entrance or exit from the level. All cells outside the game field are filled with ground. Monsters cannot move.

Even though the level is presented as a field of cells, game time flows continuously and the player moves across the field continuously. Assume the player is a point. Thanks to the cape, the player is always moving down at a very slow constant speed. The cape allows the player to glide: he can choose the horizontal component of velocity as he wishes at any moment, in addition to the vertical component which cannot be changed. The player cannot pass through ground cells, and in the challenge, the player cannot even touch the ground.

The gun can only be used to shoot left or right. Bullets leave the gun and fly horizontally in the desired direction until the first contact with ground. Monsters perish in all cells on the bullet's path.

To pass a level, player must fly from the cell where the entrance to the level is located to the cell with the exit. It is allowed to start at any point inside the cell with the entrance, and to end at any point inside the cell with the exit. Gennadiy needs to know if he can do this, and if so, what is the maximum number of monsters he can kill in the process.

The player can have arbitrarily high horizontal velocity. The gun has endless ammo and can shoot as rapidly as desired. The player can fly through a cell with a monster, which kills the monster.

입력

The first line of the input file contains two integers NN and MM --- the vertical and horizontal size of the field, respectively (1≤N,M≤2,0001 \le N, M \le 2\\,000).

The next NN lines with MM symbols in each describe the game field. Each symbol describes the contents of the corresponding cell:

  • @ --- cell with ground.
  • . --- empty cell.
  • m --- empty cell with a monster.
  • S --- empty cell with level entrance.
  • E --- empty cell with level exit.

It is guaranteed that there is only one entrance and one exit cell on the level.

출력

If it is impossible to fly across the level, print -1, otherwise print the maximum number of monsters killed in the process.

힌트

The optimal plan of passing the level from the first example is provided below. Note that it is forbidden to fly diagonally between two ground cells.

In the second example, the path between the entrance and the exit is blocked, so it is impossible to fly through the level.

예제2

  1. 예제 1

    입력
    9 11
    ..m...S....
    ..@.@...@@.
    ..@m@@...@.
    ..@@...@...
    m@mm..m@.m.
    .@@...@.m@.
    ..@@m@@.@..
    ...m.m....E
    ...@.@@.m..
    
    예상 출력
    7
    
  2. 예제 2

    입력
    3 1
    S
    @
    E
    
    예상 출력
    -1