Under construction

Still being built. Things may change

Nearest number - 2

Interview

Time limit1sMemory limit128 MB

Summary
Replace each zero with the nearest nonzero value using Manhattan distance, but leave zeros that are tied between two or more nearest values unchanged.
Level

Medium5 of 10

Topics
BFS, Graph, Matrix, Implementation
Solved
No attempts yet

Problem

You are given an N×NN \times N matrix AA of non-negative integers.

The distance between two elements Ai,jA_{i,j} and Ap,qA_{p,q} is defined as ∣i−p∣+∣j−q∣|i - p| + |j - q|.

Replace every zero element of the matrix with the value of the nearest non-zero element. If two or more non-zero elements are tied for the minimum distance, leave that zero unchanged.

Input

The first line contains the integer NN. It is followed by N2N^2 integers giving the matrix in row-major order.

Output

Print the resulting matrix. Output each row on its own line, with the NN integers of a row separated by single spaces.

Constraints

  • 1≤N≤2001 \le N \le 200, 0≤Ai,j≤10000000 \le A_{i,j} \le 1000000

Examples6

  1. Example 1

    Input
    3
    0 0 0
    1 0 2
    0 3 0
    
    Expected output
    1 0 2
    1 0 2
    0 3 0
    
  2. Example 2

    Input
    1
    7
    
    Expected output
    7
    
  3. Example 3

    Input
    1
    0
    
    Expected output
    0
    
  4. Example 4

    Input
    3
    0 0 0
    0 0 0
    0 0 0
    
    Expected output
    0 0 0
    0 0 0
    0 0 0
    
  5. Example 5

    Input
    3
    0 0 0
    0 5 0
    0 0 0
    
    Expected output
    5 5 5
    5 5 5
    5 5 5
    
  6. Example 6

    Input
    3
    5 0 5
    0 0 0
    0 0 0
    
    Expected output
    5 0 5
    5 0 5
    5 0 5