#C10181. Largest Connected Region in a Grid

    ID: 39358 Type: Default 1000ms 256MiB

Largest Connected Region in a Grid

Largest Connected Region in a Grid

You are given a grid with (M) rows and (N) columns. Each cell is either empty (denoted by a period '.') or blocked (denoted by a hash '#'). Two empty cells are considered connected if they share a side (up, down, left, or right). The task is to find the size of the largest connected region of empty cells in the grid.

For example, if the grid is:

.#...
..#..
#####
.#.#.
..#.#

Then the largest connected region of empty cells has size 5.

The connection is defined using the following directions:

[ \text{Directions} = { (-1, 0), (1, 0), (0, -1), (0, 1) } ]

Your solution should read the grid from standard input and output the result to standard output.

inputFormat

The first line contains two integers (M) and (N) separated by a space, indicating the number of rows and columns of the grid respectively. The next (M) lines each contain a string of length (N) representing the grid row. Each character of the string is either '.' (empty) or '#' (blocked).

outputFormat

Output a single integer on a new line: the size of the largest connected region of empty cells in the grid.## sample

5 5
.#...
..#..
#####
.#.#.
..#.#
5