100 atcoder#ABC191C. [ABC191C] Digital Graffiti

[ABC191C] Digital Graffiti

Score : 300300 points

Problem Statement

We have a grid with HH rows and WW columns. Let (i,j)(i, j) be the square at the ii-th row from the top and jj-th column from the left. Each square is painted black or white. If Si,jS_{i, j} is #, (i,j)(i, j) is painted black; if Si,jS_{i, j} is ., (i,j)(i, j) is painted white. It is guaranteed that the outermost squares are white. That is, the squares that can be represented as (1,j)(1, j), (H,j)(H, j), (i,1)(i, 1), or (i,W)(i, W) are white.

Consider the part of the grid that is painted black as a polygon. How many sides does it have (at least)?

Here, it is guaranteed that the part painted black forms a polygon without self-intersection, that is, the following holds:

  • at least one square is painted black;
  • we can travel between any two squares painted black by repeatedly moving up, down, left, or right while going through only black squares;
  • we can travel between any two squares painted white by repeatedly moving up, down, left, or right while going through only white squares. (Note that the outermost squares in the grid are all painted white.)

Constraints

  • 3H103 \le H \le 10
  • 3W103 \le W \le 10
  • Si,jS_{i, j} is # or ..
  • S1,jS_{1, j} and SH,jS_{H, j} are ..
  • Si,1S_{i, 1} and Si,WS_{i, W} are ..
  • The part painted black forms a polygon without self-intersection.

Input

Input is given from Standard Input in the following format:

HH WW

S1,1S1,2S1,3S1,WS_{1, 1} S_{1, 2} S_{1, 3} \dots S_{1, W}

S2,1S2,2S2,3S2,WS_{2, 1} S_{2, 2} S_{2, 3} \dots S_{2, W}

S3,1S3,2S3,3S3,WS_{3, 1} S_{3, 2} S_{3, 3} \dots S_{3, W}

\hspace{40pt} \vdots

SH,1SH,2SH,3SH,WS_{H, 1} S_{H, 2} S_{H, 3} \dots S_{H, W}

Output

Print the minimum number nn such that the part painted black can be seen as an nn-gon.

5 5
.....
.###.
.###.
.###.
.....
4

If we use a coordinate system where the top-left, bottom-left, top-right, and bottom-right corners of the grid are (0,0)(0, 0), (H,0)(H, 0), (0,W)(0, W), and (H,W)(H, W), the given figure is a quadrilateral whose vertices are (1,1)(1, 1), (4,1)(4, 1), (4,4)(4, 4), and (1,4)(1, 4).