#K37932. Taco: Find the Smallest Enclosing Rectangle

    ID: 26086 Type: Default 1000ms 256MiB

Taco: Find the Smallest Enclosing Rectangle

Taco: Find the Smallest Enclosing Rectangle

You are given an ( n \times m ) grid composed of characters. Each cell is either a dot ('.') representing an empty cell or a hash ('#') representing a marked cell. Your task is to determine the smallest axis-aligned rectangle that encloses all the '#' characters in the grid.

More formally, let the grid indices be 0-indexed. If the marked cells are located at positions ( (i, j) ) (with 0 ≤ i < n and 0 ≤ j < m) such that the cell contains a '#', you need to compute:

[ r_1 = \min{ i \mid grid[i][j] = '#' } + 1, \quad c_1 = \min{ j \mid grid[i][j] = '#' } + 1, \quad r_2 = \max{ i \mid grid[i][j] = '#' } + 1, \quad c_2 = \max{ j \mid grid[i][j] = '#' } + 1 ]

where the addition of 1 converts the indices to 1-indexed format. It is guaranteed that there is at least one '#' character in the grid.

Your solution should read the input from standard input (stdin) and produce the output to standard output (stdout).

inputFormat

The first line contains two integers ( n ) and ( m ), representing the number of rows and columns respectively. The following ( n ) lines each contain a string of length ( m ) consisting of characters '.' and '#', representing the grid.

outputFormat

Output four integers in one line separated by spaces: ( r_1 ), ( c_1 ), ( r_2 ), ( c_2 ). These represent the coordinates of the top-left and bottom-right corners of the smallest rectangle (in 1-indexed format) that encloses all '#' characters.## sample

5 8
........
...##...
...##...
....#...
........
2 4 4 5