All problems

Problem 197

Maximal Square

Medium · 20 pts · Matrix, Dynamic Programming

Problem

You are given a grid of '0' and '1' characters. Find the largest square block that contains only 1's, and print its area (side length squared). If the grid has no 1 at all, the answer is 0. Let size[i][j] be the side of the largest all-ones square whose bottom-right corner is at cell (i, j). For a cell containing 1, size[i][j] = 1 + the minimum of the sizes above, to the left and diagonally up-left. For a 0 cell it is 0.

Input

First line: integers R and C. Next R lines: C characters, each '0' or '1'.

Output

Print the area of the largest all-ones square.

Constraints

1 <= R, C <= 300

Example input

4 5
10100
10111
11111
10010

Example output

4

Explanation

In rows 2 and 3 of the grid, the cells in columns 3 and 4 (counting from 1) are all 1's and form a 2 x 2 square. No 3 x 3 square of 1's exists, so the largest area is 2 × 2 = 4.

Every problem here can be used in a RelayTrixCode team match. Create a room.

Previous: Longest Palindromic Subsequence Next: Burst Balloons