Largest Free Block in a Rack Grid

medium monotonic-stack stack arrays 2000 ms · 256 MB

Largest Free Block in a Rack Grid

A data hall is laid out as a grid of R rows and C slots per row. Every slot is either occupied (#) or free (.). A new tenant wants to install one rack cluster as a single contiguous rectangular block, so the whole block must land on free slots only.

Find the axis-aligned rectangle of free slots with the largest possible area (every slot inside it must be .). If several rectangles tie for the largest area, report the one whose top-left corner is topmost (smallest row), breaking further ties by the leftmost column; if a rectangle's top-left corner is still tied with another's, prefer the one with the smaller bottom row, and finally the smaller bottom-right column. In other words, among all rectangles of maximum area, output the one whose (top row, left column, bottom row, right column) tuple is smallest in that reading order.

Input

The first line contains two integers R and C (1 ≤ R, C ≤ 2000, R·C ≤ 4·10^6) — the number of rows and the number of slots per row.

Each of the next R lines contains a string of exactly C characters, each either . (free) or # (occupied).

Output

If at least one slot is free, print five integers separated by spaces: the maximum area, the 1-indexed row and column of the top-left corner of an optimal rectangle, and the 1-indexed row and column of its bottom-right corner — in the order area r1 c1 r2 c2. Use the tie-break rule above to choose among rectangles of maximum area.

If every slot is occupied, print a single 0.

Notes

In the first example, rows 1–5 are all free in columns 4–5 (the # blocks only ever appear in columns 2–3 or column 3), giving a 5×2 block of area 10, which beats every single row's run of free slots.

In the second example, the 2×2 block of free slots in the top-left corner and the 2×2 block in the bottom-right corner both have area 4. The tie-break rule picks the top-left one, since its top-left corner is topmost and leftmost.

Example 1

Input

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

Output

10 1 4 5 5

Example 2

Input

4 4
..##
..##
##..
##..

Output

4 1 1 2 2

Hints · reveal one at a time if you're stuck

Hint 1

Fix the bottom row of a candidate rectangle. For each column, how many free slots can you stack upward from that row before hitting an occupied slot, and what does that turn the rest of the problem into?

Hint 2

Each row becomes a histogram of these upward run lengths, and the best rectangle with that bottom row is the largest rectangle under the histogram. A monotonic stack finds that largest rectangle in linear time, so the whole grid fits comfortably in the limits.

Hint 3

Candidates turn up in bottom-row order, not in top-left order, so do not rely on which one you find first. Record each candidate as (area, top row, left column, bottom row, right column) and keep the smallest tuple among the largest areas.

report an issue Prefer your own editor? sooks prep 62 (CLI)
Sign in to run code

Run your code on the examples, or submit it against every test.