CF135D.Cycle

省选/NOI-

通过率:0%

时间限制:3.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

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.

小佩佳非常喜欢由字符“0”和“1”组成的矩形表格。最近,他收到了母亲送给他的一张这样的表格作为礼物。该表格共有 nn 行 mm 列。行从上到下编号为 11 到 nn,列从左到右编号为 11 到 mm。佩佳立刻决定不惜一切代价找出最长的“酷”环(cool cycle)。

环(cycle)是指一个由互不相同的格子构成的序列,其中任意两个相邻格子具有公共边;此外,序列的第一个格子与最后一个格子也必须具有公共边。若一个环满足以下所有条件,则称其为“酷”环:

  • 该环完全由包含字符“1”的格子组成;
  • 环中每个格子恰好与环中另外两个格子有公共边;
  • 表格中每一个包含“1”的格子,要么属于该环,要么位于该环“外部”(见下方定义)。

为形式化定义“外部”,我们在平面上绘制该环:令环中每个格子 (i, j)(i,\,j)(其中 ii 为行号,jj 为列号)对应平面上的点 (i, j)(i,\,j);对环中每一对相邻(即有公共边)的格子所对应的点,用直线段连接。由此得到一条无自交、无自接触的闭合折线。该折线将平面划分为两个连通区域:一个面积无限的区域和一个面积有限的区域。若格子 (r, c)(r,\,c) 不属于该环,且其对应平面上的点 (r, c)(r,\,c) 位于面积无限的区域内,则称该格子位于该环“外部”。

请帮助佩佳找出表格中最长“酷”环的长度。环的长度定义为该环所包含的格子数目。

输入格式

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".

第一行包含两个整数 nn 和 mm(1≤n,m≤10001 \leq n, m \leq 1000),分别表示表格的行数和列数。接下来的 nn 行中,每行包含 mm 个字符,每个字符为 "0" 或 "1"。

输出格式

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

输出一个整数——表格中最长“酷”环的长度。如果不存在这样的环,则输出 0。

输入输出样例

  • 输入#1

    3 3
    111
    101
    111

    输出#1

    8
  • 输入#2

    5 5
    01010
    10101
    01010
    10101
    01010

    输出#2

    0
  • 输入#3

    7 7
    1111111
    1000101
    1000101
    1000101
    1000111
    1000001
    1111111

    输出#3

    24
  • 输入#4

    5 5
    11111
    10001
    10101
    10001
    11111

    输出#4

    0

说明/提示

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.

在第一个样例中,只有一个环,且它是“酷”的。

在第二个样例中,根本不存在环。

在第三个样例中,有两个“酷”的环:它们的长度分别为 1212 和 2424。

在第四个样例中,也仅存在一个环,但它不是“酷”的,因为该环内部存在一个值为 "1" 的格子。

输入解题思路,AI测评打分。不知道怎么写?

首页