This page is still under construction.

Parts of this page are still being built. What you see may change.

Bitmap

Time limit1sMemory limit128 MB

Summary
For each black pixel in an n by m bitmap, output the Manhattan distance to the closest white pixel.
Level

Medium5 of 10

Topics
BFS, Graph, Dynamic programming
Solved
No attempts yet

Problem

You are given a rectangular bitmap of size n×mn \times m. Each pixel of the bitmap is either white or black, and at least one pixel is white. The pixel in row ii and column jj is denoted (i,j)(i, j).

The 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)=∣i1−i2∣+∣j1−j2∣.d(p_1, p_2) = |i_1 - i_2| + |j_1 - j_2|.

Write a program that, for every pixel, computes the distance to the nearest white pixel.

Input

The first line contains two integers nn and mm separated by a single space (1≤n≤1821 \le n \le 182, 1≤m≤1821 \le m \le 182).

Each of the following nn lines contains one string of length mm made of the characters 0 and 1, describing one row of the bitmap. In the string that describes row ii of the bitmap, the jj-th character is 1 if and only if the pixel (i,j)(i, j) is white (1≤i≤n1 \le i \le n, 1≤j≤m1 \le j \le m).

Output

Print nn lines. The ii-th line must contain mm integers f(i,1),…,f(i,m)f(i, 1), \dots, f(i, m) separated by single spaces, where f(i,j)f(i, j) is the distance from the pixel (i,j)(i, j) to the nearest white pixel.

Examples4

  1. Example 1

    Input
    3 4
    0001
    0011
    0110
    
    Expected output
    3 2 1 0
    2 1 0 0
    1 0 0 1
    
  2. Example 2

    Input
    1 1
    1
    
    Expected output
    0
    
  3. Example 3

    Input
    1 5
    00100
    
    Expected output
    2 1 0 1 2
    
  4. Example 4

    Input
    2 2
    11
    11
    
    Expected output
    0 0
    0 0