This page is still under construction.

Parts of this page are still being built. What you see may change.

Largest Square

Time limit5sMemory limit256 MB

Summary
Given a 0/1 matrix, find the side length of the largest all-ones square submatrix.
Level

Medium5 of 10

Topics
Dynamic programming, Matrix
Solved
No attempts yet

Problem

Given an N×MN \times M matrix made up of 00s and 11s, write a program that finds the largest square submatrix consisting entirely of 11s.

Input

The input consists of several test cases. The first line of each test case contains NN and MM (1≤N,M≤1,0001 \le N, M \le 1{,}000). Each of the next NN lines contains MM numbers separated by spaces. The input ends with a line containing two zeros.

Output

For each test case, output the side length (width or height) of the largest square. If no such square exists, output 00.

Examples1

  1. Example 1

    Input
    4 5
    0 1 0 1 1
    1 1 1 1 1
    0 1 1 1 0
    1 1 1 1 1
    3 4
    1 1 1 1
    1 1 1 1
    1 1 1 1
    6 6
    0 0 0 0 0 0
    0 0 0 0 0 0
    0 0 0 0 0 0
    0 0 0 0 0 0
    0 0 0 0 0 0
    0 0 0 0 0 0
    0 0
    
    Expected output
    3
    3
    0