Under construction

Still being built. Things may change

Stabbing Number

Time limit2sMemory limit512 MB

Summary
Given a histogram polygon drawn in ASCII, find a rectangle decomposition that minimizes the maximum number of rectangles an axis-parallel segment crosses.
Level

Hard9 of 10

Topics
Dynamic programming, Bit manipulation, Geometry
Solved
No attempts yet

Problem

A histogram is a simple rectilinear polygon HH, where the interior angle at each vertex is either 90∘90^\circ or 270∘270^\circ. HH has a horizontal edge that sees every point qq inside HH, including points on its interior and its boundary. An edge ee sees a point q∈Hq \in H if there is a vertical segment ss that connects ee to qq and lies inside HH.

Let HH be a histogram with nn vertices, and let RR be a decomposition of HH into rectangles whose sides are vertical or horizontal. The vertices of the rectangles need not all be vertices of HH. You may add extra vertices on the boundary of HH, in its interior, or both.

The stabbing number of a horizontal or vertical segment ss inside HH with respect to RR is the number of rectangles in RR whose interior (not just their boundaries) ss intersects. The stabbing number of RR is the maximum stabbing number of any horizontal or vertical segment ss inside HH. Compute a decomposition RR with the minimum stabbing number.

Input

The first line contains two positive integers mm and nn (1≤m,n≤501 \le m, n \le 50), the number of rows and columns of the table that shows the histogram. Each of the next mm lines contains exactly nn characters. * marks the boundary of the histogram, and the rest of the table is filled with dots (.). Each edge of the histogram contains at least three * characters. The histogram has at least four and at most 16 edges, and no two edges overlap, intersect, or touch. In other words, each * is adjacent to exactly two other * characters.

Output

Print the stabbing number of the given histogram on one line.

Examples2

  1. Example 1

    Input
    10 13
    .....****....
    .....*..*....
    .....*..***..
    .....*....*..
    .....*....***
    ...***......*
    ...*........*
    ****........*
    *...........*
    *************
    
    Expected output
    2
    
  2. Example 2

    Input
    8 15
    ...............
    .........*****.
    ....***..*...*.
    ....*.*..*...*.
    .****.****...*.
    .*...........*.
    .*************.
    ...............
    
    Expected output
    2