logo AlgoBeat OnlineJudge
登录 注册

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

内存限制:1024 MiB 时间限制:2000 ms 标准输入输出
题目类型:VJudge(洛谷) 评测方式:VJudge
上传者: 匿名

题目描述

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 and width . 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.

输入格式

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

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

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

输出格式

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

样例

样例输入 1

3 5
.#...
.####
#.#.S

样例输出 1

6

样例输入 2

3 7
.......
...S...
.......

样例输出 2

21