Computer programming can be a pain sometimes at Xorin's company. There is always the promise that they would finish the project in time, but Xorin knows better than that. That's exactly why, as soon as he is done working long hours during the week, he finds comfort in bizarre activities. This week it shall be… football!

In order to become a real football supporter, Xorin needs to prove his dedication by doing what his great ancestors did back in their day: spray painting a large "U" letter on the wall of some building.

Since Xorin wants to get it over and done with as fast as possible, we need to help him find the maximum possible size for his artwork. The wall is defined as a two-dimensional array with values of 0 - denoting areas of the wall that have already been vandalized - and 1 - denoting areas of the wall that Xorin can paint over.

A valid "U" spray painting is defined by the following sequence:

  1. Start from some pair of coordinates (i, j) which contains a value of 1.
  2. Move downwards at least one row.
  3. Move to the right at least one column.
  4. Move upwards at least one row.

Note that:

  • You can only navigate through cells which contain a value of 1.
  • You can at no point navigate outside the borders of the wall.
  • The number of rows you move downwards at step 2 does not have to be the same as the number of rows you move upwards at step 4 (that is, the "U" does not have to be symmetric).

The size of a "U" is defined as the number of cells visited during the sequence that generates it.


The first line of input contains integers N and M - the dimensions of the wall.
Each of the following N lines contain M binary values.


You should output a single integer: the maximum size of a valid "U" that Xorin can paint. If there is none, the answer is 0.


  • 1 ≤ N, M ≤ 500


3 3
7The largest "U" we can draw has the length 7. It is highlighted in the input.
5 6
4The only valid "U" is the one in the bottom-left of the wall.

Sponsors Gold