#P2335. Nearest White Pixel Distance

    ID: 15609 Type: Default 1000ms 256MiB

Nearest White Pixel Distance

Nearest White Pixel Distance

We are given an n×mn \times m monochrome bitmap that contains at least one white pixel. Each pixel is represented as a character, where '1' denotes a white pixel and '0' denotes a black pixel. We denote the pixel in the ii-th row and jj-th column as (i,j)(i,j). The Manhattan distance between two pixels p1=(i1,j1)p_1=(i_1,j_1) and p2=(i2,j2)p_2=(i_2,j_2) is defined as

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

Your task is to calculate, for every pixel, the Manhattan distance to the nearest white pixel and output the resulting distances in the form of an n×mn \times m grid.

inputFormat

The first line contains two integers $n$ and $m$ separated by a space. Then follow $n$ lines, each containing a string of $m$ characters ('0' or '1'). It is guaranteed that there is at least one '1' in the bitmap.

Format:
Line 1: n m
Next n lines: strings of length m.

outputFormat

Output $n$ lines. Each line contains $m$ integers separated by a space representing the minimum Manhattan distance from the corresponding pixel to a white pixel.

Format:
Each line: d1 d2 ... dm

sample

3 4
0001
0011
0110
3 2 1 0

2 1 0 0 1 0 0 1

</p>