P27196 [AtCoder] Crossing Thin Ice
提高
题目描述

Problem Description

On a cold day in winter, Taro JOI decided to play by breaking thin ice on a plaza. The plaza is a rectangle divided into grids in the east-west direction with $m$ sections and in the north-south direction with $n$ sections, i.e., $m \times n$ grids in total. Some grids have thin ice, while others do not. Taro JOI decided to move across the grids while breaking the ice following these rules:

  • He can start breaking the ice from any grid that has ice.
  • He can move to an adjacent grid (either east, west, north, or south) that has ice and has not been broken yet.
  • He must break the ice on the grid he moves to.

Write a program to find the maximum number of grids Taro JOI can move across while breaking the ice. Note that $1 \leq m \leq 90$ and $1 \leq n \leq 90$. In the given input data, the number of possible moves does not exceed 200,000.


Input Format

The input consists of $n+2$ lines. The first line contains an integer $m$. The second line contains an integer $n$. From the third line to the $n+2$th line, each line contains $m$ integers separated by spaces, indicating whether each grid has ice or not. If the $i$th grid from the north and the $j$th grid from the west ($1 \leq i \leq n$, $1 \leq j \leq m$) has ice, the $j$th value on the $i+2$th line is $1$; otherwise, it is $0$.

Output Format

Output the maximum number of grids that can be moved across.


Sample Input and Output

Sample Input #1

3
3
1 1 0
1 0 1
1 1 0

Sample Output #1

5

Sample Input #2

5
3
1 1 1 0 1
1 1 0 0 0
1 0 0 0 1

Sample Output #2

5

Notes/Hints

Sample Explanation 1

For the first sample input, an example of a movement method that yields the maximum value: 2009-yo-t4-1.jpg

Sample Explanation 2

For the second sample input, an example of a movement method that yields the maximum value: 2009-yo-t4-2.jpg Examples of movement methods that do not yield the maximum value for the second sample input: 2009-yo-t4-2-1.jpg 2009-yo-t4-2-2.jpg 2009-yo-t4-2-3.jpg 2009-yo-t4-2-4.jpg 2009-yo-t4-2-5.jpg

输入格式

见题面 Input 部分

输出格式

见题面 Output 部分

样例输入
3 3 1 1 0 1 0 1 1 1 0
样例输出
5