from sequtils import map, mapIt
from strutils import split, parseInt, parseFloat

import macros
macro unpack*(input: seq; count: static[int]): untyped =
  result = quote do: ()
  when NimMinor <= 13:
    for i in 0..<count: result[0].add quote do: `input`[`i`]
  else:
    for i in 0..<count: result.add quote do: `input`[`i`]

## count == 0 のとき unpackしない(seq)
## count >  0 のとき count個分 unpack した結果の tuple を返す
type UnselectableTypeError = object of Exception
template input*(typ: typedesc; count: Natural = 0): untyped =
  let line = stdin.readLine.split
  when count == 0:
    when typ is int:    line.map(parseInt)
    elif typ is float:  line.map(parseFloat)
    elif typ is string: line
    else: raise newException(UnselectableTypeError, "You selected a type other than int, float or string")
  else:
    when typ is int:    line.map(parseInt).unpack(count)
    elif typ is float:  line.map(parseFloat).unpack(count)
    elif typ is string: line.unpack(count)
    else: raise newException(UnselectableTypeError, "You selected a type other than int, float or string")

template inputs*(typ: typedesc; count = 0; rows = 1): untyped =
  (1..rows).mapIt(input(typ, count))

# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #

template hideEcho*(x: varargs[string, `$`]) =
  for v in x: stderr.writeLine(v)

# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
from strutils import replace, join
from sequtils import newSeqWith

let
  (H, W) = input(int, 2)
  S = inputs(string, 1, H).mapIt(it.replace(".", "0")).mapIt(it.replace("#", "1")).mapIt(it.mapIt(($it).parseInt))

proc count(i, j: int): int =
  if S[i][j] == 1:
    -1
  elif H == 1 and W == 1:
    1
  elif H == 1:
    if j == 0:
      S[i][j + 1]
    elif j == W - 1:
      S[i][j - 1]
    else:
      S[i][j - 1] + S[i][j + 1]
  elif W == 1:
    if i == 0:
      S[i + 1][j]
    elif i == H - 1:
      S[i - 1][j]
    else:
      S[i - 1][j] + S[i + 1][j]
  else:
    if i == 0 and j == 0:
      S[i + 1][j] + S[i][j + 1] + S[i + 1][j + 1]
    elif i == H - 1 and j == 0:
      S[i - 1][j] + S[i][j + 1] + S[i - 1][j + 1]
    elif i == 0 and j == W - 1:
      S[i + 1][j] + S[i][j - 1] + S[i + 1][j - 1]
    elif i == H - 1 and j == W - 1:
      S[i - 1][j] + S[i][j - 1] + S[i - 1][j - 1]
    elif i == 0:
      S[i][j - 1] + S[i][j + 1] + S[i + 1][j - 1] + S[i + 1][j] + S[i + 1][j + 1]
    elif i == H - 1:
      S[i][j - 1] + S[i][j + 1] + S[i - 1][j - 1] + S[i - 1][j] + S[i - 1][j + 1]
    elif j == 0:
      S[i - 1][j] + S[i + 1][j] + S[i - 1][j + 1] + S[i][j + 1] + S[i + 1][j + 1]
    elif j == W - 1:
      S[i - 1][j] + S[i + 1][j] + S[i + 1][j - 1] + S[i][j - 1] + S[i + 1][j - 1]
    else:
      S[i - 1][j - 1] + S[i - 1][j] + S[i - 1][j + 1] + S[i][j - 1] + S[i][j + 1] + S[i + 1][j - 1] + S[i + 1][j] + S[i + 1][j + 1]


var numSeq = newSeqWith(H, newSeq[int](W))
for i, _ in numSeq:
  for j, _ in numSeq[i]:
    numSeq[i][j] = count(i, j)

var result = newSeqWith(H, newSeq[string](W))
for i, vi in numSeq:
  for j, vj in vi:
    case vj:
      of -1:
        result[i][j] = "#"
      else:
        result[i][j] = $vj

for r in result:
  echo r.join
