cswork / OAOpen practice workspace

MetaOA #26

Cave Advanture

Estimated MediumJudge-ready

Topics (AI-labelled)Grid DP

Start from the top-left corner (0,0) of the grid and reach the bottom-right corner. ! is a passable cell and x is an obstacle. Each step moves exactly one cell left, right, or down; you cannot move up or re-enter a visited cell. Find the maximum number of cells on a path that reaches the exit (counting both the start and the end); output −1 if the exit cannot be reached.

Input

This site's input: the first line contains R C (1..200), followed by R strings of length C containing only ! and x; the entrance or exit may also be blocked.

Output

Output the maximum number of visited cells, or −1.

Example 1

Input:

3 3
!!!
!!!
!!!

Output:

9

Example 2

Input:

2 2
!x
x!

Output:

-1

Example 3

Input:

1 1
!

Output:

1

Example notes

Example 1: in an empty 3×3 grid you can snake row by row through all 9 cells and finish at the bottom-right corner.

Example 2: the cells to the right of and below the entrance are both obstacles, so the exit cannot be reached; the output is −1.

Example 3: the start is also the end and is passable, so the number of visited cells is 1.

Editorial

Loading solution…