Problem:
 Score : 200 points    Problem Statement You are given an H × W grid. The squares in the grid are described by H strings, S_1,...,S_H . The j -th character in the string S_i corresponds to the square at the i -th row from the top and j -th column from the left (1 \leq i \leq H,1 \leq j \leq W) .  . stands for an empty square, and # stands for a square containing a bomb.  Dolphin is interested in how many bomb squares are horizontally, vertically or diagonally adjacent to each empty square. (Below, we will simply say "adjacent" for this meaning. For each square, there are at most eight adjacent squares.) He decides to replace each . in our H strings with a digit that represents the number of bomb squares adjacent to the corresponding empty square.  Print the strings after the process.      Constraints  1 \leq H,W \leq 50  S_i is a string of length W consisting of # and . .         Input Input is given from Standard Input in the following format:  H  W  S_1  :  S_H       Output Print the H strings after the process. The i -th line should contain a string T_i of length W , where the j -th character in T_i corresponds to the square at the i -th row from the top and j -th row from the left in the grid (1 \leq i \leq H, 1 \leq j \leq W) .        Sample Input 1 3 5
.....
.#.#.
.....      Sample Output 1 11211
1#2#1
11211  For example, let us observe the empty square at the first row from the top and first column from the left. There is one bomb square adjacent to this empty square: the square at the second row and second column. Thus, the . corresponding to this empty square is replaced with 1 .       Sample Input 2 3 5
#####
#####
#####      Sample Output 2 #####
#####
#####  It is possible that there is no empty square.       Sample Input 3 6 6
#####.
#.#.##
####.#
.#..#.
#.##..
#.#...      Sample Output 3 #####3
#8#7##
####5#
4#65#2
#5##21
#4#310

Solution (Python):
H, W = map(int, input().split())
S = [list(input()) for _ in range(H)]

for i in range(H):
    for j in range(W):
        if S[i][j] != "
            cnt = 0
            if i != 0:
                if j != 0:
                    if S[i-1][j-1] == "
                        cnt += 1
                if S[i-1][j] == "
                    cnt += 1
                if j != W-1:
                    if S[i-1][j+1] == "
                        cnt += 1
            if j != 0:
                if S[i][j-1] == "
                    cnt += 1
            if j != W-1:
                if S[i][j+1] == "
                    cnt += 1
            if i != H-1:
                if j != 0:
                    if S[i+1][j-1] == "
                        cnt += 1
                if S[i+1][j] == "
                    cnt += 1
                if j != W-1:
                    if S[i+1][j+1] == "
                        cnt += 1
            S[i][j] = str(cnt)
    S_str = "".join(S[i])
    print(S_str)
    S_str = ""

Solution (Python):
H,W = map(int,input().split())
s = [input() for i in range(H)]
ans = [[0]*W for i in range(H)]
for i in range(H):
  for j in range(W):
    count = 0
    if s[i][j] != '
      if i+1 < H and s[i+1][j] == '
        count+=1
      if j+1 < W and s[i][j+1] == '
        count+=1
      if i-1 >= 0 and s[i-1][j] == '
        count+=1
      if j-1 >= 0 and s[i][j-1] == '
        count+=1
      if i+1 < H and j+1 < W and s[i+1][j+1] == '
        count+=1
      if i+1 < H and j-1 >= 0 and s[i+1][j-1] == '
        count+=1
      if i-1 >= 0 and j+1 < W and s[i-1][j+1] == '
        count+=1
      if i-1 >= 0 and j-1 >= 0 and s[i-1][j-1] == '
        count+=1
      ans[i][j]=count
    else:
      ans[i][j] = '
for i in range(H):
  tmp=''
  for j in range(W):
    tmp+=str(ans[i][j])
  print(tmp)

Solution (Python):
H,W = map(int, input().split())
ats = []
for i in range(W+2):
  ats.append('@')
grid = [ats]
for i in range(H):
  S_i = ['@']
  S_i_raw = input()
  for j in range(W):
    S_i.append(S_i_raw[j])
  S_i.append('@')
  grid.append(S_i)
grid.append(ats)

for h in range(H):
  for w in range(W):
    counter = 0
    if grid[h+1][w+1] == '.':
      if grid[h][w] == '
        counter += 1
      if grid[h][w+1] == '
        counter += 1
      if grid[h][w+2] == '
        counter += 1
      if grid[h+1][w] == '
        counter += 1
      if grid[h+1][w+2] == '
        counter += 1
      if grid[h+2][w] == '
        counter += 1
      if grid[h+2][w+1] == '
        counter += 1
      if grid[h+2][w+2] == '
        counter += 1
      grid[h+1][w+1] = str(counter)
      
for i in range(H):
  ans=''
  for j in range(W):
    ans = ans + grid[i+1][j+1]
  print(ans)

Solution (Python):
H, W = map(int, input().split())

S = []
for i in range(H) :
    S.append(list(input()))

for i in range(H) :
    for j in range(W) :
        cnt = 0
        if S[i][j] == "." :
            for a in range(-1, 2) :
                for b in range(-1, 2) :
                    if i + a >= 0 and i + a < H and j + b >= 0 and j + b < W :
                        if S[i + a][j + b] == "
                            cnt += 1
            S[i][j] = str(cnt)

for i in range(H) :
    for j in range(W) :
        print(S[i][j], end="")
    print()

Solution (Python):
h,w=map(int,input().split())
m=[[0for _ in range(w+2)]for _ in range(h+2)]
for y in range(h):
  s=input()
  for x in range(w):
    if s[x]=="
      for i in [0,1,2]:
        for j in [0,1,2]:
          m[y+j][x+i]+=1
      m[y+1][x+1]=9
for y in range(1,h+1):
  ans=""
  for x in range(1,w+1):
    if m[y][x]>8:
      ans+="
    else:
      ans+=str(m[y][x])
  print(ans)

Solution (Python):

import copy
import numpy as np
import random
import bisect 

h,w = map(int,input().split())
s = ["."*(w+2)]
for i in range(h):
    s.append("."+str(input())+".")
s.append("."*(w+2))

ans = [[0 for i in range(w)] for i in range(h)]

for i in range(h):
    for j in range(w):
        if s[i+1][j+1]=="
            ans[i][j] = "
        else:
            count = 0
            for dx in range(-1,2):
                for dy in range(-1,2):
                    
                    if dx==0 and dy==0:
                        pass
                    else:
                        if s[i+dx+1][j+dy+1] == "
                            count += 1
            ans[i][j] = str(count)
for i in range(h):
    print("".join(ans[i]))

Solution (Python):
H, W = map(int, input().split())

S = [['P' for _ in range(W+2)] for _ in range(H+2)]
for i in range(H):
    s = list(input()) 
    for j in range(W):
        S[i+1][j+1] = s[j]
for i in range(H):
    for j in range(W):
        if S[i+1][j+1]=='.':
            counter = 0
            for k in [i, i+1, i+2]:
                for l in [j, j+1, j+2]:
                    if S[k][l] == '
                        counter += 1
            S[i+1][j+1] = str(counter)
for i in range(1,H+1):
    print(''.join(S[i][1:W+1]))

Solution (Python):
h, w = [int(_) for _ in input().split()]
m = [['.']*(w+2)] + [['.']+list(input())+['.'] for _ in range(h)] + [['.']*(w+2)]

for i in range(1, h+1):
    for j in range(1, w+1):
        if m[i][j] == '.':
            m[i][j] = 0    
            for di in range(-1,2):
                for dj in range(-1,2):
                    if m[i+di][j+dj] == '
                        m[i][j] += 1
    print(''.join(map(str, m[i][1:w+1])))

Solution (Python):
from sys import stdin
h, w = [int(_) for _ in stdin.readline().rstrip().split()]
data = [list(stdin.readline().rstrip()) for _ in range(h)]

for i in range(h):
    for j in range(w):
        if data[i][j] == '.':
            count = 0
            for ii in range(i-1, i+2):
                for jj in range(j-1, j+2):
                    if 0 <= ii < h and 0 <= jj < w:
                        if data[ii][jj] == '
                           count += 1
        else:
            count = '
        print(count, end='')
    print()

Solution (Python):
H,W = map(int,input().split())
mas = [input() for i in range(H)]
dx = [-1, 0, 1, -1, 0, 1, -1, 0, 1]
dy = [-1, -1, -1, 0, 0, 0, 1, 1, 1]

ans = []
for i in range(H) :
    ans.append(['

for h in range(H) :
    for w in range(W) :
        c = 0
        if mas[h][w] == '
            continue
        for i in range(9) :
            if w + dx[i] < 0 or h + dy[i] < 0 or w + dx[i] > W - 1 or h + dy[i] > H - 1:
                continue
            if mas[h+dy[i]][w+dx[i]] == '
                c += 1
        ans[h][w] = c

for a in ans :
    b = map(str, a)
    print(''.join(b))

Solution (Python):
H,W=map(int,input().split())
L = [input() for _ in range(H)]

def grid(x,y):
  field = 0
  for dy in range(-1,2):
    for dx in range(-1,2):
      nx = x + dx
      ny = y + dy
      if 0 <= nx and nx < W and 0 <= ny and ny < H and L[ny][nx] == '
        field += 1
  return str(field)
    

for y in range(H):
  ans = ''
  for x in range(W):
    if L[y][x] != '
      ans += grid(x,y)
    else:
      ans += '
  print(ans)

Solution (Python):


h,w = map(int,input().split())
s = [0] * (h+2)
s[0] = [0] * (w+2)
for i in range(1,h+1):
    s[i] = [0] + list(input()) + [0]
s[h+1] = [0] * (w+2)

for i in range(1,w+1):
    for j in range(1,h+1):
        bomb = 0
        if s[j][i-1] == "
            bomb += 1
        if s[j+1][i-1] == "
            bomb += 1
        if s[j-1][i-1] == "
            bomb += 1
        if s[j][i] == "
            bomb += 1
        if s[j+1][i] == "
            bomb += 1
        if s[j-1][i] == "
            bomb += 1
        if s[j][i+1] == "
            bomb += 1
        if s[j+1][i+1] == "
            bomb += 1
        if s[j-1][i+1] == "
            bomb += 1
        if s[j][i] == "
            bomb = "
        s[j][i] = bomb

del s[0]
del s[-1]

for i in range(h):
    s[i] =  s[i][1:-1]
    s[i] = map(str,s[i])
    s[i] = "".join(s[i])
    print(s[i])

Solution (Python):

from collections import defaultdict
from collections import deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LS():return list(map(list, sys.stdin.readline().split()))
def S(): return list(sys.stdin.readline())[:-1]
def IR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = I()
    return l
def LIR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = LI()
    return l
def SR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = S()
    return l
def LSR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = SR()
    return l
mod = 1000000007


def A():
    a,b,c = LI()
    if a not in [b,c]:
        print(a)
    elif b not in [a,c]:
        print(b)
    else:
        print(c)
    return


def B():
    h,w = LI()
    s = SR(h)
    ans = [[0 for i in range(w)] for j in range(h)]
    d = [[0,1],[0,-1],[1,0],[-1,0],[1,1],[1,-1],[-1,1],[-1,-1]]
    for y in range(h):
        for x in range(w):
            if s[y][x] == "
                ans[y][x] += 10
                for dy,dx in d:
                    y2 = y+dy
                    x2 = x+dx
                    if 0 <= y2 < h and 0 <= x2 < w:
                        ans[y2][x2] += 1
    for y in range(h):
        for x in range(w):
            if ans[y][x] >= 10:
                print("
            else:
                print(ans[y][x],end = "")
        print()
    return


def C():
    def bfs(a,b):
        bfs_map = [1 for i in range(n)]
        bfs_map[0] = 0
        q = deque()
        q.append(0)
        s = 1
        while q:
            x = q.pop()
            for y in v[x]:
                if [x,y] != [a,b] and [x,y] != [b,a]:
                    if bfs_map[y]:
                        bfs_map[y] = 0
                        q.append(y)
                        s += 1
        return s
    n,m = LI()
    v = [deque() for i in range(n)]
    l = deque()
    for i in range(m):
        a,b = LI()
        a -= 1
        b -= 1
        v[a].append(b)
        v[b].append(a)
        l.append([a,b])
    ans = 0
    for a,b in l:
        s = bfs(a,b)
        if s < n:
            ans += 1
    print(ans)

def D():
    n,k = LI()
    v = [None for i in range(n)]
    for i in range(n):
        v[i] = LI()
        v[i] = [[None,None]]+[v[i]]
    v.sort(key = lambda x:x[1][1])
    for i in range(n):
        v[i][0][1] = i
    v.sort(key = lambda x:x[1][0])
    for i in range(n):
        v[i][0][0] = i
    po = [[None for i in range(n)] for j in range(n)]
    h = [None for i in range(n)]
    w = [None for i in range(n)]
    for i in range(n):
        po[v[i][0][0]][v[i][0][1]] = v[i][1]
        h[v[i][0][0]] = v[i][1][0]
        w[v[i][0][1]] = v[i][1][1]
    re = [[[[0 for x in range(n)] for y in range(n)] for t in range(n)] for s in range(n)]
    ans = float("inf")
    for s in range(n):
        for t in range(n):
            for y in range(s,n):
                for x in range(t,n):
                    if x == t:
                        re[s][t][y][x] = re[s][t][y-1][x]
                    else:
                        re[s][t][y][x] = max(0,re[s][t][y][x-1]+re[s][t][y-1][x]-re[s][t][y-1][x-1])
                    if po[y][x] != None:
                        re[s][t][y][x] += 1
                    if re[s][t][y][x] >= k:
                        ans = min(ans,(h[y]-h[s])*(w[x]-w[t]))
    print(ans)

def E():
    return


def F():
    return


def G():
    return


def H():
    return


if __name__ == "__main__":
    B()

Solution (Python):
h,w=map(int,input().split())
e=["*" for i in range(w+2)]
s=[e]
for i in range(h):
  s.append(["*"]+list(input())+["*"])
s.append(e)

for i in range(1,h+1):
  for j in range(1,w+1):
    if s[i][j]==".":
      s[i][j]=str(sum(s[k][j-1:j+2].count("
  print("".join(s[i][1:w+1]))

Solution (Python):
H, W = map(int, input().split())
Sn = [input() for _ in range(H)]

for h in range(0, H):
    h_strs = []
    for w in range(0, W):
        if Sn[h][w] == '
            h_strs.append('
            continue
        n = 0
        for sh in range(h-1, h+2):
            for sw in range(w-1, w+2):
                if 0 <= sh and sh < H and 0 <= sw and sw < W:
                    if Sn[sh][sw] == '
                        n += 1
        h_strs.append(str(n))
    Sn[h] = "".join(h_strs)

for S in Sn:
    print(S)

Solution (Python):
H,W=map(int,input().split())
Map=['.'*(W+2)]
for i in range(H):
    line='.'+input()+'.'
    x=[]
    for j in range(W+2):
        x.append(line[j])
    Map.append(x)
Map.append('.'*(W+2))
for h in range(1,1+H):
    for w in range(1,1+W):
        if Map[h][w]=='.':
            cnt=0
            for i,j in [(-1,-1),(-1,0),(0,-1),(1,-1),(-1,1),(1,0),(0,1),(1,1)]:
                if Map[h+i][w+j]=='
            Map[h][w]=str(cnt)
for h in range(1,1+H):
    print(''.join(Map[h][1:-1]))

Solution (Python):
h, w = map(int, input().split())
masu = [[0]*w for _ in range(h)]

for i in range(h):
    masu[i] = list(input())

for i in range(h):
    for j in range(w):
        if masu[i][j] == ".":
            count = 0
            for dx in range(-1, 2):
                for dy in range(-1, 2):
                    xx = j + dx
                    yy = i + dy
                    if 0 <= xx and xx < w and 0 <= yy and yy < h:
                        if masu[yy][xx] == "
                            count += 1
            masu[i][j] = count
for i in range(h):
    print("".join(map(str, masu[i])))

Solution (Python):
def mine(i, j):
    if masu[i][j] == ".":
        masu[i][j] = 0
    if masu[i][j] != "
        masu[i][j] += 1
    return masu[i][j]


h, w = map(int, input().split())
masu = [[0]*(w+2) for _ in range(h+2)]

for i in range(1, h+1):
    masu[i] = [0]+list(input())+[0]
for i in range(h+2):
    for j in range(w+2):
        if masu[i][j] == ".":
            masu[i][j] = 0
        if masu[i][j] == "
            masu[i-1][j-1] = mine(i-1, j-1)
            masu[i-1][j] = mine(i-1, j)
            masu[i-1][j+1] = mine(i-1, j+1)
            masu[i][j-1] = mine(i, j-1)
            masu[i][j+1] = mine(i, j+1)
            masu[i+1][j-1] = mine(i+1, j-1)
            masu[i+1][j] = mine(i+1, j)
            masu[i+1][j+1] = mine(i+1, j+1)

for i in range(1, h+1):
    print("".join(map(str, masu[i][1:-1])))

Solution (Python):
H , W = map(int,input().split())
S = [[""]*(W+2)]*(H+2)
for i in range(H):
    p = [""]+list(input())+[""]
    S[i+1] = p

def countBom(S,i,j):
    con_bom =0
    for h in range(-1,2):
        for w in range(-1,2):
            if S[i+h][j+w] == "
                con_bom += 1
    return con_bom




for i in range(H):
    for j in range(W):
        if S[i+1][j+1] ==".":
            S[i+1][j+1] = countBom(S,i+1,j+1)
    print("".join(list(map(str,(S[i+1])))))

Solution (Python):
h,w = map(int,input().split())
s = []
def calc(i,j,k,l):
    if k==0 and l==0:
        return 0
    elif i+k==-1 or j+l==-1 or i+k==h or j+l==w:
        return 0
    elif s[i+k][j+l] == '
        count[i][j] = count[i][j] + 1
for i in range(h):
    s += [input()]
count = [[0 for i in range(w)] for j in range(h)]
for i in range(h):
    for j in range(w):
        for k in range(3):
            for l in range(3):
                calc(i,j,k-1,l-1)
for i in range(h):
    s_tmp=''
    for j in range(w):
        if s[i][j]=='
            s_tmp += '
        else:
            s_tmp += str(count[i][j])
    s[i]=s_tmp
for i in range(h):
    print(s[i])

Solution (Python):
H, W = list(map(int, input().split()))
S = [input() for i in range(H)]

for h in range(H):
    for w in range(W):
        if S[h][w] == '.':
            count = 0
            for y in range(-1, 2):
                for x in range(-1, 2):
                     if 0 <= h+y and h+y < H and 0 <= w+x and w+x < W:
                         if S[h+y][w+x] == '
                             count += 1
        else:
            count = '
        print(count, end='')
    print()

Solution (Python):

H, W = map(int, input().split())
S = [list(input()) for _ in range(H)]

def aaa(i, j):
    cnt = 0
    for x in [-1, 0, 1]:
        for y in [-1, 0, 1]:
            if (not 0 <= i + y < H) or (not 0 <= j + x < W):
                continue
            if S[i + y][j + x] == '
                cnt += 1    
    return str(cnt)

for i in range(H):
    for j in range(W):
        if S[i][j] == '
            continue
        S[i][j] = aaa(i, j)
for s in S:
    print("".join(s))

Solution (Python):
h,w=map(int,input().split())
b=["n"*(w+2)]
for i in range(h):b.append("n"+input()+"n")
b.append(b[0])
def num(i,j):
  a=0
  def same(i,j):
    global b
    if b[i][j]=="
    return 0
  a+=same(i-1,j-1)
  a+=same(i-1,j)
  a+=same(i-1,j+1)
  a+=same(i,j-1)
  a+=same(i,j+1)
  a+=same(i+1,j-1)
  a+=same(i+1,j)
  a+=same(i+1,j+1)
  return a
for i in range(len(b)):
  if 0<i<h+1:
    for j in range(len(b[i])):
      if b[i][j]==".":b[i]=b[i][:j]+str(num(i,j))+b[i][j+1:]
for i in range(1,h+1):print(b[i][1:w+1])

Solution (Python):
H, W = map(int, input().split())
S = [input() for i in range(H)]

S_dim = [[0] * W for i in range(H)]
for i in range(H):
    for j in range(W):
        S_dim[i][j] = S[i][j:j + 1]

S_num = [[0] * W for i in range(H)]
for i in range(H):
    for j in range(W):
        count = 0
        if S_dim[i][j] == ".":
            for dy in [-1, 0, 1]:
                for dx in [-1, 0, 1]:
                    if i + dy < 0 or i + dy >= H or j + dx < 0 or j + dx >= W:
                        continue
                    if S_dim[i + dy][j+ dx] == "
                        count += 1
            S_num[i][j] = str(count)
        else:
            S_num[i][j] = "

for i in range(H):
    for j in range(W):
        print(S_num[i][j], end ="")
    print()

Solution (Python):
h,w=map(int,input().split())
A=list('.'*(w+2))
MAP=[A]
for _ in range(h):
    MAP.append(list('_'+input()+'_'))
MAP.append(A)
for i in range(1,h+1):
    for j in range(1,w+1):
        if MAP[i][j]=='.':
            MAP[i][j]=str((MAP[i-1][j-1]+MAP[i-1][j]+MAP[i-1][j+1]+MAP[i][j-1]+MAP[i][j+1]
                            +MAP[i+1][j-1]+MAP[i+1][j]+MAP[i+1][j+1]).count('
for i in range(1,h+1):
    print(''.join(MAP[i][1:w+1]))

Solution (Python):
H, W = map(int, (input().split()))

rows = []
for _ in range(0, H):
    rows.append(input())

x = [0, 1, -1,  0,  1, -1, 1, -1]
y = [1, 1,  1, -1, -1, -1, 0,  0]
for row_i in range(0, H):
    result_col = ""
    
    for col_i in range(0, W):
        if rows[row_i][col_i] == "
            result_col += "
            
            
            continue
        mines = 0
        for i in range(0, 8):
            dx = col_i + x[i]
            dy = row_i + y[i]
            if dx >= W or dx < 0 or dy >= H or dy < 0:
                continue
            if rows[dy][dx] == "
                mines += 1
        result_col += str(mines)
    print(result_col)

Solution (Python):
def minesweeper(H: int, W: int, S: list)->list:
    directions = [
        (-1, -1), (-1, 0), (-1, 1),
        (0, -1),           (0, 1),
        (1, -1),  (1, 0),  (1, 1),
    ]
    ret = [[''] * W for _ in range(H)]

    for h in range(H):
        for w in range(W):
            if S[h][w] == '
                ret[h][w] = '
                continue

            count = 0
            for dh, dw in directions:
                if h + dh < 0:
                    continue
                if H <= h + dh:
                    continue
                if w + dw < 0:
                    continue
                if W <= w + dw:
                    continue
                if S[h+dh][w+dw] == '
                    count += 1
            ret[h][w] = str(count)

    return ret


if __name__ == "__main__":
    H = 0
    H, W = map(int, input().split())
    S = [input() for _ in range(H)]
    ans = minesweeper(H, W, S)
    for a in ans:
        print(''.join(a))

Solution (Python):
h,w= (int(i) for i in input().split())

s= [ ["_" for i in range(w + 2)] ]
for i in range(h):
    s.append(list("_" + input() + "_"))
s.append(["_" for i in range(w + 2)])

for i in range(1,h + 1):
    for j in range(1,w + 1):
        if s[i][j] == ".":
            s[i][j] = str((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]).count("


for i in range(1, h+1):
    print ("".join(s[i]).replace("_",""))

Solution (Python):
high,width = map(int, input().split())
s_list = [list(input()) for _ in range(high)]
tmp_list = [[0 for _ in range(width)] for _ in range(high)]
dir = [[-1,-1],[0,-1],[1,-1],[1,0],[1,1],[0,1],[-1,1],[-1,0]]
for h in range(high):
    for w in range(width):
        if s_list[h][w] == '
            tmp_list[h][w] = '
            for x,y in dir:
                tate = h+x
                yoko = w+y
                if tate >= 0 and yoko >= 0 and tate < high and yoko < width:
                    if s_list[tate][yoko]=='.':
                        tmp_list[tate][yoko] += 1

for tmp in tmp_list:
    t = map(str, tmp)
    print(''.join(t))

Solution (Python):
def search(i,j):
  b = [-1,0,1]
  c = 0
  for dx in b:
    for dy in b:
      if dx == 0 and dy == 0:
        continue
      else:
        x = i + dx
        y = j + dy
        if 0 <= x < h and 0 <= y < w:
          if a[x][y] == "
            c += 1
  bomb = str(c)
  return bomb
      
h,w = map(int, input().split())
a = [list(input()) for i in range(h)]
for i in range(h):
  for j in range(w):
    if a[i][j] == "
      continue
    else:
      a[i][j] = search(i,j)
  print("".join(a[i]))

Solution (Python):
def CountMine(x, y):
    count = 0
    for k in range(3):
        for l in range(3):
            if S[x-1+k][y-1+l] == '
                count += 1
    return count
    
    

H, W = list(map(int, input().split()))
S = [[0]*(W+2)] + [[0] + list(input()) + [0] for _ in range(H)] + [[0]*(W+2)]

for i in range(H):
    for j in range(W):
        if S[i+1][j+1] == '.':
            print(CountMine(i+1, j+1), end="")
        else:
            print('
    print('\n')

Solution (Python):
OFFSETS = [(-1,-1), (-1,0), (-1,1),
           (0,-1), (0,1),
           (1,-1), (1,0), (1,1)]

H, W = map(int, input().split())

Ses = []
for _ in range(H):
    Ses.append(input())

replaced_Ses = []
for y in range(H):
    replaced_S = ""
    for x in range(W):
        if Ses[y][x] == "
            replaced_S += "
            continue

        count_bomb = 0
        for dx, dy in OFFSETS:
            now_x = x+dx
            now_y = y+dy
            if now_x < 0 or now_x >= W:
                continue
            if now_y < 0 or now_y >= H:
                continue
            if Ses[now_y][now_x] == "
                count_bomb += 1
        replaced_S += str(count_bomb)
    replaced_Ses.append(replaced_S)

for line in replaced_Ses:
    print(line)

Solution (Python):
H, W = [ int(x) for x in input().split() ]
Ss = [ input() for _ in range(H) ]

ans = Ss.copy()
for y in range(H):
    for x in range(W):
        if ans[y][x] == "
            continue
        n = 0
        for dir in [[0, 1], [0, -1], [1, 0], [1, 1], [1, -1], [-1, 0], [-1, 1], [-1, -1]]:
            x2 = x + dir[0]
            y2 = y + dir[1]
            if x2 < 0 or x2 >= W or y2 < 0 or y2 >= H:
                continue
            n += 1 if Ss[y2][x2] == "
        ans[y] = ans[y][:x] + str(n) + ans[y][x+1:]

print("\n".join(ans))

Solution (Python):
H,W = map(int,input().split())
S = ["."*(W+2)]
[S.append("."+input()+".") for _ in range(H)]
S.append("."*(W+2))
ans = []

for h in range(1,H+1):
    ans_tmp = ""
    for w in range(1,W+1):
        if S[h][w] == ".":
            tmp = 0
            for i in [-1,0,1]:
                for j in [-1,0,1]:
                    if S[h+i][w+j] == "
                        tmp += 1
            ans_tmp += str(tmp)
        else:
            ans_tmp += "
    ans.append(ans_tmp)
for s in ans:
    print(s)

Solution (Python):

h,w = map(int,input().split())
str_list = [input() for i in range(h)]
result_list = ['' for i in range(h)]

for i in range(h):
    for j in range(w):
        if str_list[i][j] == '.':
            cnt = 0
            for x in range(-1,2):
                for y in range(-1,2):
                    try:
                        if i+x >= 0 and j+y >= 0 and str_list[i+x][j+y] == '
                            cnt += 1
                    except IndexError:
                        pass
            result_list[i] += str(cnt)
        elif str_list[i][j] == '
            result_list[i] += '

for i in range(h):
    print(result_list[i])

Solution (Python):
h,w=map(int,input().split())
l=list(map(list,[input() for i in range(h)]))
for i in range(h):
    for j in range(w):
        if l[i][j]=="
            continue
        num=0

        if i!=0 and j!=0 and l[i-1][j-1]=="
            num+=1
        if i!=0 and l[i-1][j]=="
            num+=1
        if i!=0 and j!=w-1 and l[i-1][j+1]=="
            num+=1
        if j!=0 and l[i][j-1]=="
            num+=1
        if j!=w-1 and l[i][j+1]=="
            num+=1
        if i!=h-1 and j!=0 and l[i+1][j-1]=="
            num+=1
        if i!=h-1 and l[i+1][j]=="
            num+=1
        if i!=h-1 and j!=w-1 and l[i+1][j+1]=="
            num+=1

        l[i][j]=str(num)
    print("".join(l[i]))

Solution (Python):
import sys
from itertools import product

def main():
  input = sys.stdin.readline
  h, w = map(int, input().split())
  ans = [[0 for _ in range(w+2)] for _ in range(h+2)]
  for i in range(h):
    s = list(map(str, input().strip()))
    for j in range(w):
      if s[j] == '
        ans[i+1][j+1] = '
      
        for k in product([-1, 0, 1], repeat=2):
          if ans[i+1+k[0]][j+1+k[1]] != '
            ans[i+1+k[0]][j+1+k[1]] += 1
        
  for i in range(1, h+1):
    print(*ans[i][1:w+1], sep='')


if __name__ == '__main__':
  main()

Solution (Python):
H, W = map(int, input().split())
l = list(map(list, [input() for i in range(H)]))
b = []
for i in range(H):
    for j in range(W):
        if l[i][j] == '
            b.append([i+1, j+1])

for i in range(H):
    for j in range(W):
        if l[i][j] == '.':
            l[i][j] = 0
            for k in range(len(b)):
                if int(b[k][0]) - 1 <= i+1 <= int(b[k][0]) + 1 and int(b[k][1]) - 1 <= j+1 <= int(b[k][1]) + 1:
                    l[i][j] = l[i][j] + 1
        print(l[i][j], end="")
    print('')

Solution (Python):
H,W=map(int,input().split(' '))
S=[['x']*(W+2)]
for i in range(H):
  S.append(['x']+list(input())+['x'])
S.append(['x']*(W+2))

for h in range(H+2):
  for w in range(W+2):
    if S[h][w] == '.':
      S[h][w] = str([S[x][y] for x in [h-1,h,h+1] for y in [w-1,w,w+1]].count('
      
for i in range(H):
  print(''.join(S[i+1]).replace('x',''))

Solution (Python):
H, W = map(int, input().split())
S = [list(input()) for _ in range(H)]
for h in range(H):
    for w in range(W):
        if S[h][w] != ".":
            continue
        num = 0
        if h != 0 and S[h - 1][w] == "
            num += 1
        if h != H - 1 and S[h + 1][w] == "
            num += 1
        if w != 0 and S[h][w - 1] == "
            num += 1
        if w != W - 1 and S[h][w + 1] == "
            num += 1
        if h != 0 and w != 0 and S[h - 1][w - 1] == "
            num += 1
        if h != H - 1 and w != 0 and S[h + 1][w - 1] == "
            num += 1
        if h != 0 and w != W - 1 and S[h - 1][w + 1] == "
            num += 1
        if h != H - 1 and w != W - 1 and S[h + 1][w + 1] == "
            num += 1
        S[h][w] = str(num)
    print("".join(S[h]))

Solution (Python):
h, w = map(int, input().split())
s = [input() for _ in range(h)]
ans_field = []

direct_x = [1, 1, 0, -1, -1, -1, 0, 1]
direct_y = [0, 1, 1, 1, 0, -1, -1, -1]

for i in range(h):
    ans = ""
    for j in range(w):
        if s[i][j] == '.':
            count = 0
            for k in range(8):
                if 0 <= i+direct_y[k] < h and 0 <= j+direct_x[k] < w:
                    if s[i+direct_y[k]][j+direct_x[k]] == '
                        count += 1
            ans += str(count)
        else:
            ans += "
    ans_field.append(ans)

for i in range(h):
    print(ans_field[i])

Solution (Python):
h, w = map(int, input().split())
field = [list(input()) for _ in range(h)]
direct = []
for i in range(-1, 2):
    for j in range(-1, 2):
        if i or j:
            direct.append((i, j))
def count(x, y):
    ret = 0
    for dx, dy in direct:
        xa = x + dx
        ya = y + dy
        if 0 <= xa < h and 0 <= ya < w and field[xa][ya] == '
            ret += 1
    return ret
for i in range(h):
    for j in range(w):
        if field[i][j] == '.':
            print(count(i, j), end = '')
        else:
            print('
    print()

Solution (Python):
H, W = map(int, input().split())
s = [input() for i in range(H)]
cs = []
count = 0

dx = [1, 1, 0, -1, -1, -1, 0, 1]
dy = [0, 1, 1, 1, 0, -1, -1, -1]

for i in range(H):
    for j in range(W):
        if s[i][j] == '.':
            for k in range(8):
                if 0 <= i+dy[k] < H and 0 <= j+dx[k] < W:
                    if s[i+dy[k]][j+dx[k]] == '
                        count += 1
                    else:
                        pass
                else:
                    pass
            cs.append(str(count))
            count = 0
        else:
            cs.append('

for i in range(1, H*W+1):
    print(cs[i-1], end='')
    if i % W == 0 and i % (H*W) != 0:
        print('\n')

Solution (Python):
H, W = map(int, input().split())
S = []
for i in range(0, H):
    S.append(input())
for i in range(0, H):
    out = ""
    for j in range(0, W):
        if S[i][j]=="
            out = out+"
        else:
            count = 0
            for k in range(max(0, i-1), min(H, i+2)):
                for l in range(max(0, j-1), min(W, j+2)):
                    if S[k][l]=="
                        count = count+1
            out = out+str(count)
    print("{}".format(out))