Dr. Alice and Ants

0

0 votes
Problem

Dr. Alice, a myrmecologist, was studying the traveling pattern of ants in ant colonies. She represented an ant colony in the form of a rectangular array of size NM. Each cell of this rectangular array is either 0 or 1.  0 represents that an ant is present in the cell and 1 represents that sugar is present in the cell. A cell in ith row and jth column is represented by (i,j). In the study, Dr. Alice made an interesting observation: each ant tries to go the sugar cell that is nearest to it. She wants to determine the nearest distance to a sugar cell for each cell.

Here the distance between two cells a(ai,aj) and b(bi,bj) is defined as:

Dist(a,b)=|aibi|+|ajbj|

It is given that at least one cell contains sugar.

Input

The first line contains 2 space-separated Integers N and M denoting the number of rows and columns respectively.

Then N lines follows. Each contains M space-separated values (either 1 or 0).

Output

The output should be a 2-D matrix of N rows and M columns.In the ith line , 1iN, there should be written M integers d(i,j) separated by single spaces, where d(i,j) is the distance from the cell (i,j) to the nearest sugar cell.

Constraints

  • 1N150
  • 1M150
  • Each entry of the input matrix is either 1 or 0.
Sample Input
3 4
0 0 0 1
0 0 1 1
0 1 1 0
Sample Output
3 2 1 0 
2 1 0 0 
1 0 0 1 
Time Limit: 5
Memory Limit: 256
Source Limit:
Editor Image

?