Under construction

Still being built. Things may change

Candy in the Maze

Time limit2sMemory limit1024 MB

Summary
Find the longest lollipop that Ralph can carry from the left column to the right column through a grid maze with walls.
Level

Medium6 of 10

Topics
Graph, Union-find, Matrix
Solved
No attempts yet

Statement

Ralph and Vanellope found a long lollipop that they need to build a machine. Once they found it, they realized the only way back is through a maze. Of course, they have to take the lollipop with them.

The maze is a grid of nn by mm cells. Each cell is either empty or contains a maze wall. The lollipops at this place come in different lengths, but each one occupies kk consecutive cells in a single line for some kk. A lollipop cannot be placed on a cell that contains a wall. Ralph is very strong, so he can carry a lollipop of any length.

At first, Ralph can enter the maze at any cell in the leftmost column, but he must hold the lollipop parallel to the left border of the maze. To exit, he must reach any cell in the rightmost column while holding the lollipop parallel to that border.

While Ralph holds the lollipop parallel to one of the borders, he can move it one cell along that border. Alternatively, he can grab the lollipop by one of its ends, lift it into the air at that spot, and lower it parallel to the other side of the maze. He can do these actions only if the lollipop ends up on empty cells afterward.

Ralph and Vanellope want to know the maximum length of a lollipop that can be carried through the maze.

Input

The first line contains two integers nn and mm, the number of rows and columns in the maze (1≤n,m≤3001 \le n, m \le 300). The next nn lines each contain mm characters. The jj-th character of the ii-th line is \# if cell (i,j)(i, j) contains a wall, and . otherwise.

Output

Print one integer, the maximum number of cells a lollipop can occupy such that Ralph can carry it through the maze. If no lollipop length can be carried through the maze, print 00.

Examples1

  1. Example 1

    Input
    3 5
    ...##
    .#.#.
    ##...
    
    Expected output
    2