시간 제한메모리 제한제출정답맞힌 사람정답 비율
2 초 1024 MB71133.333%

문제

Dungeon Crawl: Paper Soup just became the most popular game, and you are about to give it a try. The game takes place on a rectangular field which consists of $N$ rows and $M$ columns, where each cell can be one of the following types:

  • empty cell ‘.’;
  • wall ‘#’;
  • coin cell ‘o’;
  • explosive mine cell ‘X’;
  • starting cell ‘S’.

It’s guaranteed that the first and the last rows and columns contain only walls (take notice that the player cannot move through wall cells). The field will contain one or more starting cells. When the game starts, the player will be positioned at one of the starting cells, marked with an ‘S’. Because the game takes place in a dungeon system with reduced visibility, the player can’t see the entire map, only a $3 × 3$ square centered on his current position. Also, for the player the mines and starting cells appear as empty cells (they are invisible).

With each move, the player can only go to an adjacent cell to the north, south, east or west. If they enter a cell with a coin, the coin is collected and disappears. If they enter a cell with an explosive mine, the dungeon system collapses, the player loses all the coins they picked up and the game ends.

The good news is that you obtained the map of the dungeon by browsing multiple online guides. However, you won’t know what your starting position will be – although it is guaranteed that you will begin at one of the starting cells. If you play optimally, what’s the maximum number of coins you are guaranteed to obtain (again, without knowing where you start)?

입력

On the first line of the input there will be $N$ and $M$, the number of rows and columns of the map where the game will take place. The next $N$ rows contain the map, each row with $M$ characters, using the representation described in the problem statement.

출력

The output should contain only one number, the maximum number of coins that can be obtained on the respective map without knowing the starting position.

제한

  • Let $S$ be the number of possible starting cells on the map.
  • $N ≤ 400$, $M ≤ 400$, $S ≤ 60$.

서브태스크

번호배점제한
13

$S = 1$. There are no mines. Outside of the first and last row or column there are no walls.

27

$N = 3$

312

$S = 1$

423

$S = 2$

541

$1 ≤ N, M ≤ 250$, $1 ≤ S ≤ 12$

614

No further restrictions

예제 입력 1

3 7
#######
#Soooo#
#######

예제 출력 1

4

예제 입력 2

3 8
########
#SoXooS#
########

예제 출력 2

1

예제 입력 3

7 18
##################
#................#
#.o...SX.......o.#
#.o...X..X.....o.#
#.o.....XS.....o.#
#................#
##################

예제 출력 3

0

예제 입력 4

7 18
##################
#....#...........#
#.o...SX.......o.#
#.o...X..X.....o.#
#.o.....XS.....o.#
#.........#......#
##################

예제 출력 4

6

예제 입력 5

7 18
##################
#......X..S....oo#
##################
#..o..S.X......o.#
##########X#######
#o.....S...X.....#
##################

예제 출력 5

1

채점 및 기타 정보

  • 예제는 채점하지 않는다.