Mr. Land wants to spend his retirement money on a plot of land and build a ranch on it. The plot he was shown is a rectangle, mostly open field, with trees that are hard to cut down and rocks that cannot be cleared scattered across it.
The ranch is a single square, and no tree or rock may stand inside it. Mr. Land wants that square to be as large as possible.
The plot is M meters tall and N meters wide. Cut it into a grid of 1 meter cells and write it as an M×N matrix. In the matrix, 0 means open field, 1 means a tree, and 2 means a rock.
Find the side length L of the largest square ranch that can be built. If the plot has no open field cell at all, L is 0.