#P15744. [JAG 2024 Summer Camp #3] Renovation

    ID: 15682 远端评测题 2000ms 1024MiB 尝试: 0 已通过: 0 难度: 6 上传者: 标签>2024枚举记忆化搜索连通块ICPC

[JAG 2024 Summer Camp #3] Renovation

Description

Jack is planning to renovate his house to make his room as large as possible. However, since Jack doesn't have much money, he wants to create a spacious room with minimal effort.

Jack's house is represented as a grid with height HH and width WW. Each cell in the grid is in one of the following states:

  • . : A floor that Jack can freely move through.
  • # : A wall that Jack cannot pass through.
  • S : The cell where Jack is currently located, which is also a floor.

Jack can move to adjacent floor cells in the grid, either up, down, left, or right. He cannot move outside the boundaries of his house.

In this renovation, Jack can destroy up to one wall and turn it into a floor. Determine the maximum number of cells Jack can reach from the currently located position after making this change.

Input Format

The input consists of a single test case of the following format.

$$\begin{aligned} & H \ W \\ & l_{1} \\ & l_{2} \\ & \vdots \\ & l_{H} \end{aligned}$$

The first line contains two integers, HH and WW, representing the height and width of the grid, respectively. Both HH and WW are between 22 and 500500 (both inclusive).

The next HH lines each contain a string of length WW, representing the grid. Each string lil_{i} consists of the characters . (floor), # (wall), and S (Jack's starting position). It is guaranteed that the grid contains exactly one S.

Output Format

Output a single integer, the maximum number of cells Jack can reach from his starting position after optionally destroying one wall.

3 5
.#...
.####
#.#.S
6
3 7
.......
...S...
.......
21