This page is still under construction.

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

Planet X

Interview

Time limit1sMemory limit1024 MB

Summary
Given a grid where some cell heights are known and adjacent cells differ by at most 1, fill in every cell whose height is forced by the constraints.
Level

Medium7 of 10

Topics
Graph, BFS, Shortest path, Implementation
Solved
No attempts yet

Problem

The year is 2109, and a group of researchers has just discovered "Planet X", a previously unknown planet in our own solar system, beyond Pluto's orbit. The research group immediately sends out a probe to take measurements, and shortly afterwards receives the measurement data back.

The researchers are specifically interested in what the surface of Planet X looks like. We represent the surface here as an N×MN \times M grid, where each cell has a height between 0 and 9.

A measuring instrument on the probe has managed to measure the exact height of some, but not all, cells. From the chemical composition of the surface we know that the planet is not particularly steep: the heights of two adjacent cells (cells that share an edge) can never differ by more than one.

Now the researchers need your help to get as much information as possible out of this data. More precisely, given the heights of some of the cells, they want you to find the heights of all the other cells whose heights can be determined uniquely.

Input

The first line contains two integers 1≤N,M≤101 \le N,M \le 10, the height and the width of the grid respectively. Then follow NN lines with MM characters each. The jj:th character on line ii is a . if no value exists for this cell, and is a digit between 0 and 9 corresponding to the height of the cell otherwise.

Output

The program shall print NN lines with MM characters each: the grid as it looks after correct heights are filled in for all cells whose height can be determined.

Examples3

  1. Example 1

    Input
    2 3
    ..6
    3..
    
    Expected output
    456
    345
    
  2. Example 2

    Input
    1 8
    .2.3..6.
    
    Expected output
    .2.3456.
    
  3. Example 3

    Input
    4 5
    ..3..
    ...5.
    .6...
    ....2
    
    Expected output
    .434.
    .5454
    .6543
    .5432