Codeforces Beta Round 97 (Div. 1)
D
Cycle
Codeforces
2500
原题
← CE →
翻译中…

Little Petya very much likes rectangular tables that consist of characters "0" and "1". Recently he has received one such table as a gift from his mother. The table contained $*n*$ rows and $*m*$ columns. The rows are numbered from top to bottom from $1$ to $*n*$, the columns are numbered from the left to the right from $1$ to $*m*$. Petya immediately decided to find the longest cool cycle whatever it takes.

翻译中…

A cycle is a sequence of pairwise distinct cells where each two consecutive cells have a common side; besides, the first cell has a common side with the last cell. A cycle is called cool if it fulfills all the following conditions simultaneously:

翻译中…
  • The cycle entirely consists of the cells that contain "1".
  • Each cell that belongs to the cycle, has a common side with exactly two other cells that belong to the cycle.
  • Each cell of the table that contains "1" either belongs to the cycle or is positioned outside of it (see definition below).
翻译中…

To define the notion of "outside" formally, let's draw a cycle on a plane. Let each cell of the cycle $(*i*, *j*)$ ($*i*$ is the row number, $*j*$ is the column number) correspond to the point $(*i*, *j*)$ on the coordinate plane. Let a straight line segment join each pair of points that correspond to the cells belonging to the cycle and sharing a side. Thus, we will get a closed polyline that has no self-intersections and self-touches. The polyline divides the plane into two connected parts: the part of an infinite area and the part of a finite area. It is considered that cell $(*r*, *c*)$ lies outside of the cycle if it does not belong to the cycle and the corresponding point on the plane with coordinates $(*r*, *c*)$ lies in the part with the infinite area.

翻译中…

Help Petya to find the length of the longest cool cycle in the table. The cycle length is defined as the number of cells that belong to the cycle.

翻译中…

Input

翻译中…

The first line contains two integers $*n*$ and $*m*$ ($1 ≤ *n*, *m* ≤ 1000$) — the number of rows and columns in the table, respectively. Each of the following $*n*$ lines contains $*m*$ characters. Each character can be either "0" or "1".

翻译中…

Output

翻译中…

Print a single number — the length of the longest cool cycle in the table. If such cycles do not exist, print 0.

翻译中…

Note

翻译中…

In the first example there's only one cycle and it is cool.

翻译中…

In the second sample there's no cycle at all.

翻译中…

In the third sample there are two cool cycles: their lengths are 12 and 24.

翻译中…

In the fourth sample there also is only one cycle but it isn't cool as there's a cell containing "1" inside this cycle.

翻译中…

样例

Input #1
3 3
111
101
111
Output #1
8
InputCopy OutputCopy
Input #2
5 5
01010
10101
01010
10101
01010
Output #2
0
InputCopy OutputCopy
Input #3
7 7
1111111
1000101
1000101
1000101
1000111
1000001
1111111
Output #3
24
InputCopy OutputCopy
Input #4
5 5
11111
10001
10101
10001
11111
Output #4
0
InputCopy OutputCopy
题目信息
题号
135D
时间限制
3 s
内存限制
256 MB
标签
暴力
DFS 及类似
模拟

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。