cswork / OAOpen practice workspace

MicrosoftOA #13

Cleaning Robot

Estimated MediumJudge-ready

Topics (AI-labelled)SimulationGrid Search

The robot starts at the top-left corner facing right. If the cell in front of it is empty, it moves forward; otherwise it turns 90 degrees clockwise in place. This repeats forever. Every empty cell it passes through counts as cleaned, and cells may be passed through repeatedly. Find the number of distinct cleaned cells after it runs indefinitely.

Input

The first line contains N M (1..1000), followed by N strings of length M, where . is an empty cell and X is an obstacle; the start (0,0) is guaranteed to be empty.

Output

Output a single integer, the answer.

Example 1

Input:

3 6
...X..
....XX
..X...

Output:

6

Example 2

Input:

1 1
.

Output:

1

Example 3

Input:

1 4
....

Output:

4

Example notes

Example 1: the robot loops along the boundary of the reachable two rows and three columns, cleaning 6 distinct cells in total.

Example 2: it is blocked on all four sides and only turns in place, cleaning its single cell; the answer is 1.

Example 3: it can keep moving back and forth along the same row, so all 4 cells are cleaned.

Editorial

Loading solution…