#P15031. [UOI 2021 II Stage] 矩阵

    ID: 16963 远端评测题 1000ms 512MiB 尝试: 0 已通过: 0 显示难度普及− 上传者: 标签>2021枚举广度优先搜索 BFSUOI(乌克兰)

[UOI 2021 II Stage] 矩阵

Problem Description

Cossack Beard got a matrix aa of size nn by mm (that is, a matrix with nn rows and mm columns). The elements of this matrix are only 00 and 11.

Someone introduced Cossack to the Manhattan distance between elements in a matrix. It turns out that if one element is in row ii and column jj, and another element is in row pp and column kk, then the Manhattan distance between these two elements is defined as ∣i−p∣+∣j−k∣\mid i - p \mid + \mid j - k \mid (the sum of the absolute values of the coordinate differences).

After that, Beard understood that the "beauty" of any element in the matrix is the distance from that element to the nearest element with value 11. Note that the "beauty" of any 11 is 00. Also, Cossack learned that in such a matrix, there is at least one 11.

Your task is simple: find the "beauty" of the most "beautiful" element in the matrix.

Input Format

The first line contains two integers nn and mm (1≤n,m≤20)(1 \leq n,m \leq 20), the number of rows and columns of the matrix.

The next nn lines each contain mm digits ai,1,ai,2,...,ai,ma_{i,1}, a_{i,2},...,a_{i,m} (0≤ai,j≤1)(0 \leq a_{i,j} \leq 1), the values of the matrix elements.

It is guaranteed that there is at least one 11.

Output Format

Output one number, the maximum "beauty".

2 3
010
000
2
3 4
0101
0001
0000
3

Hint

Sample Explanation

For the matrix in the first sample, write the "beauty" of each element in matrix form:

:::align{center} 1 0 1

2 1 2 :::

As we can see, there are two matrix elements, (row 22, column 11) and (row 22, column 33), that have the maximum "beauty", which is 22.

For the second sample, its "beauty" matrix is:

:::align{center} 1 0 1 0

2 1 1 0

3 2 2 1 :::

Scoring Rules

A solution that works correctly under the constraint n=1n = 1 will get at least 3030 points.

A solution that works correctly under the constraint n=2n = 2 will get at least 3030 points.

The translation was completed by DeepSeek V3.

Translated by ChatGPT 5