#P2335. [SDOI2005] 位图

[SDOI2005] 位图

Description

You are given a monochrome bitmap of size n ×mn\ \times m, which contains at least one white pixel. We use (i,j)(i,j) to denote the pixel in the ii-th row and jj-th column, and define the distance between two points p1=(i1,j1)p_1=(i_1,j_1) and p2=(i2,j2)p_2=(i_2,j_2) as:

d(p1,p2)=i1i2+j1j2.d(p_1,p_2)=|i_1-i_2|+|j_1-j_2|.

Your task is to read the bitmap and, for each pixel, compute the distance to the nearest white pixel. Output the result.

Input Format

The first line contains two space-separated integers nn and mm, where 1n1501 \le n \le 150 and 1m1501 \le m \le 150.

Each of the next nn lines contains a string of length mm consisting of characters '0' and '1'. In the ii-th of these lines, if the jj-th character is '1', then pixel (i,j)(i,j) is white; otherwise it is black.

Output Format

Output an n ×mn\ \times m table of numbers. In this table, the jj-th number in the ii-th row is f(i,j)f(i,j), which denotes the distance from pixel (i,j)(i,j) to its nearest white pixel.

3 4
0 0 0 1
0 0 1 1
0 1 1 0
3 2 1 0
2 1 0 0
1 0 0 1

Hint

Translated by ChatGPT 5