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 = []
for i in range(h):
    s.append(list(input()))

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

def prt_bomb(a, b):
    if(s[a][b] == '
        mine_s[a][b] = '
    else:
        ret = 0
        for i in range(max(0, a-1), min(a+1, h-1)+1):
            for j in range(max(0, b-1), min(b+1, w-1)+1):
                if(s[i][j] == '
                    ret += 1
        mine_s[a][b] = str(ret)

for i in range(h):
    for j in range(w):
        prt_bomb(i, j)

for i in range(h):
    print(''.join(mine_s[i]))

Solution (Python):








import numpy as np



h,w = map(int,input().split())
board = [[] for _ in range(h)]
for i in range(h):
    board[i] = list(input())

moves = [(-1,-1),(-1, 0), (-1, 1), (0, -1), (0, 1), (1, -1), (1, 0),(1,1)]
for ih in range(h):
    for iw in range(w):
        
        if board[ih][iw] == "
            continue
        else:
            board[ih][iw] = 0
            for move in moves:
                mh = ih+move[0]
                mw = iw+move[1]
                if mh>=0 and mh<h and mw>=0 and mw<w:
                    if board[mh][mw] == "
                        board[ih][iw] += 1
                    else:
                        continue
                else:
                    continue
            continue

for row in board:
    print("".join(map(str,row)))

Solution (Python):
h, w = map(int, input().split())
s0 = ["."] * (w + 2)
s = [["."] + list(input()) + ["."] for _ in range(h)]
s.insert(0, s0)
s.append(s0)
v = [[-1, 1], [0, 1], [1, 1], [1, 0], [1, -1], [0, -1], [-1, -1], [-1, 0]]
ans = []
for i in range(1, h + 1):
    a = []
    for j in range(1, w + 1):
        c = 0
        if s[i][j] == ".":
            for u in v:
                if s[i + u[0]][j + u[1]] == "
                    c += 1
            a.append(str(c))
        else:
            a.append("
    ans.append(a)
for b in ans:
    print("".join(b))

Solution (Python):
h,w = map(int,input().split())
s0 = "."*(w+2)
S = [s0]

for i in range(h):
    S.append("."+input()+".")
    
S.append(s0)

for i in range(1,h+1):
    for j in range(1,w+1):
        if S[i][j] == ".":
            num = 0
            for k in range(3):
                if S[i-1][j-1+k] == "
                    num += 1
            if S[i][j-1] == "
                num += 1
            if S[i][j+1] == "
                num += 1
            for k in range(3):
                if S[i+1][j-1+k] == "
                    num += 1
            S[i] = list(S[i])
            S[i][j] = str(num)
            S[i] = "".join(S[i])
            
for i in range(1,h+1):
    print(S[i][1:w+1])

Solution (Python):

h,w=map(int,input().split())
maze=[]
for i in range(h):
    maze.append(list(input()))



move=[[1,0],[0,1],[-1,0],[0,-1],[-1,1],[-1,-1],[1,1],[1,-1]]
for i in range(h):
    for j in range(w):
        if maze[i][j]=="
            continue
        else:
            cnt=0
            for dy,dx in move:
                y=i+dy
                x=j+dx
                if (0<=y<h and 0<=x<w)==False:
                    continue
                else:
                    if maze[y][x]=="
                        cnt+=1
            maze[i][j]=str(cnt)
for i in range(h):
    ans=""
    for j in range(w):
        ans+=maze[i][j]
    print(ans)

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

for h in range(H):
    for w in range(W):
        if qu[h][w] == "
            for i in range(3):
                for j in range(3):
                    if ans[h+i][w+j] != "
                        ans[h+i][w+j] += 1
            ans[h+1][w+1] = "

for h in range(H):
    result = map(str, ans[h+1][1:W+1])
    print(''.join(result))

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

for i in range(H):
    field.append(list(input()))



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


for y in range(H):
    for x in range(W):

        if field[y][x] == "
            ans[y][x] = "
            continue

        cnt = 0
        for k in range(8):
            ny = y + dy[k]
            nx = x + dx[k]

            if 0 <= ny and ny < H and 0 <= nx and nx < W:
                if field[ny][nx] == "
                    cnt += 1

        ans[y][x] = str(cnt)

for h in range(H):
    print("".join(ans[h]))

Solution (Python):
i = list(map(int,input().split()))
H = i[0]
W = i[1]




masu = []
for i in range(H+2):
    sublist = []
    if(i == 0)or(i == H+1):
        sublist = ['.']*(W+2)
        masu.append(sublist)
        continue
    a = input()
    for k in a:
        sublist.append(k)

    sublist.insert(0,'.')
    sublist.append('.')
    masu.append(sublist)



count = 0
for j in range(1,H+1):
    for k in range(1,W+1):
        if(masu[j-1][k-1] == '
            count += 1

        if(masu[j-1][k] == '
            count += 1

        if(masu[j-1][k+1] == '
            count += 1

        if(masu[j][k-1] == '
            count += 1

        if(masu[j][k+1] == '
            count += 1

        if(masu[j+1][k-1] == '
            count += 1

        if(masu[j+1][k] == '
            count += 1

        if(masu[j+1][k+1] == '
            count += 1

        if(masu[j][k] != '
            masu[j][k] = count

        count = 0

for i in range(1,H+1):
    masu[i].pop(0)
    masu[i].pop(-1)
    result = ''.join(map(str,masu[i]))
    print(result)

Solution (Python):
'''
8方向走査

'.' は 空き、 '
Minesweeperのように、空きのマスに周囲8方向にある爆弾の数を埋める。
コンテスト中に実装すると、間違えることがあるので、dy や dx それらを使う部分は
予めスニペットなどで直ぐに読み出せるように準備しておく。
8方向走査の他、4方向走査、2方向走査も出題されるので準備しておく。
「文字列」 は 作成後に変更することができない。

ここでは、「文字列」 を 文字 の 「リスト」 とすることで 変更を可能にしている。
行継続文字はバックスラッシュ(円記号)

例えば if 条件 が長くなった場合、バックスラッシュを入れることで
その後の改行が無視されて行が継続していると見なされる。
上記のプログラム
if ny < 0 or H <= ny\
or nx < 0 or W <= nx:
バックスラッシュ無しで改行の場合、SyntaxError になる。
''(空文字).join(「リスト」)

「リスト」の 各要素 を 区切り文字 なし で結合して出力することができる。

' '(半角スペース).join(「リスト」)とすれば
「リスト」の 各要素 を 区切り文字 半角スペース で結合して出力することができる。
'''


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

H, W = map(int, input().split())
field = []

for i in range(H):
    field.append(list(input()))

for y in range(H):
    for x in range(W):

        if field[y][x] == '
            continue

        cnt = 0

        for k in range(8):
            ny = y + dy[k]
            nx = x + dx[k]

            if ny < 0 or H <= ny\
                    or nx < 0 or W <= nx:
                continue

            if field[ny][nx] == '
                cnt += 1

        field[y][x] = str(cnt)

for y in range(H):
    print("".join(field[y]))

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

for h in range(H):
    for w in range(W):
        if qu[h][w] == "
            for i in range(3):
                for j in range(3):
                    if ans[h+i][w+j] != "
                        ans[h+i][w+j] += 1
            ans[h+1][w+1] = "

for h in range(H):
    result = map(str, ans[h+1][1:W+1])
    print(''.join(result))

Solution (Python):
h, w = map(int, input().split())

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


for i in range(1, h+1):
    for j in range(1, w+1):
        
        C = []
        if A[i][j] == "
            print("
        else:
            for k in range(-1, 2):
                for l in range(-1, 2):
                    C.append(A[i+k][j+l])
            print(C.count("
    print()

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

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 board[i][j]==".":
            cnt=0
            for k in range(8):
                x=i+dx[k]
                y=j+dy[k]
                if 0<=x<H and 0<= y <W:
                    if board[x][y]=='
                        cnt+=1
            board[i][j]=str(cnt)
        
        
for b in board:
    b = map(str, b)
    print("".join(b))

Solution (Python):
x,y=list(map(int, input().split()))
s=[list(map(str,list(input()))) for i in range(x)]

count=0
for i in range(x):
	for j in range(y):
		count=0
		if s[i][j]=='.':
			if i>0 and j>0 and s[i-1][j-1]=='
				count=count+1
			if i>0 and s[i-1][j]=='
				count=count+1
			if i>0 and j<y-1 and s[i-1][j+1]=='
				count=count+1
			if j>0 and s[i][j-1]=='
				count=count+1
			if j<y-1 and s[i][j+1]=='
				count=count+1
			if i<x-1 and j>0 and s[i+1][j-1]=='
				count=count+1
			if i<x-1 and s[i+1][j]=='
				count=count+1
			if i<x-1 and j<y-1 and s[i+1][j+1]=='
				count=count+1
			s[i][j]=count
for a in s:
	print(*a,sep='')

Solution (Python):
x,y = [int(i) for i in input().split()]
ans = []
for i in range(x):
	ans.append(list(input()))
for i in range(x):
	for j in range(y):
		if ans[i][j] == '.':
			d = 0
			for k in range(i-1,i+2):
				for l in range(j-1,j+2):
					if 0 <= k and k < x and 0 <= l and l < y:
						if ans[k][l] == '
							d += 1
							ans[i][j] = str(d)
for i in range(x):
	for j in range(y):
		if ans[i][j] == '.':
			ans[i][j] = '0'
for i in ans:
	print(''.join(i))

Solution (Python):
H, W = list(map(int, input().split()))
S = []
for _ in range(H):
  S.append([0 if s == '.' else s for s in input()])
  
posi = [(-1,-1), (-1,0), (-1,1), (0, -1), (0, 1), (1, -1), (1,0), (1,1)]
for h in range(H):
  for w in range(W):
    koushin = [1]*8
    if S[h][w] == '
      if h == 0:
        koushin[0],koushin[1],koushin[2] = 0,0,0
      if h == H-1:
        koushin[5],koushin[6],koushin[7] = 0,0,0
      if w == 0:
        koushin[0],koushin[3],koushin[5] = 0,0,0
      if w == W-1:
        koushin[2],koushin[4],koushin[7] = 0,0,0
      for i in range(8):
        if koushin[i]:
          if S[h+posi[i][0]][w+posi[i][1]] != '
            S[h+posi[i][0]][w+posi[i][1]] += 1

for Si in S:
  print(''.join(map(str, Si)))

Solution (Python):

s = input().split()
t = [int(p) for p in s]

a = []
H = t[0]
W = t[1]
for i in range(H):
    a.append(input())













b = ''
for i in range(W+2):
    b += '.'


'''
c = 'rei'
c = '.' + c
print(c)

'''

a.insert(0, b)
for i in range(1, 1+H):
    a[i] = '.' + a[i] + '.'
a.append(b)







def countbomb(l, h, w):
    count = 0
    for i in range(w-1, w+2):
        if(l[h-1][i] == '
            count += 1
        if(l[h+1][i] == '
            count += 1
    if(l[h][w-1] == '
        count += 1
    if(l[h][w+1] == '
        count += 1
    return count





for i in range(1, H+1):
    d = ''
    for j in range(1, W+1):
        if(a[i][j] == '
            d += '
        else:
            d += (str(countbomb(a, i, j)))
    print(d)

Solution (Python):


import sys
 
stdin = sys.stdin
inf = 1 << 60
mod = 1000000007
 
ni = lambda: int(ns())
nin = lambda y: [ni() for _ in range(y)]
na = lambda: list(map(int, stdin.readline().split()))
nan = lambda y: [na() for _ in range(y)]
nf = lambda: float(ns())
nfn = lambda y: [nf() for _ in range(y)]
nfa = lambda: list(map(float, stdin.readline().split()))
nfan = lambda y: [nfa() for _ in range(y)]
ns = lambda: stdin.readline().rstrip()
nsn = lambda y: [ns() for _ in range(y)]
ncl = lambda y: [list(ns()) for _ in range(y)]
nas = lambda: stdin.readline().split()

H, W = na()
M = nsn(H)
ans = [[-1] * W for _ in range(H)]

for i in range(H):
    for j in range(W):
        if M[i][j] != '
            cnt = 0
            for a in range(-1, 2):
                for b in range(-1, 2):
                    ni, nj = i + a, j + b
                    if ni >= 0 and ni < H and nj >= 0 and nj < W and M[ni][nj] == '
                        cnt += 1
            ans[i][j] = cnt

for i in range(H):
    for j in range(W):
        if ans[i][j] == -1:
            print("
        else:
            print(ans[i][j], end="")
    print()

Solution (Python):
h, w = map(int, input().split())
squares = [input() for i in range(h)]
squares_target = [[0 for i in range(w)] for i in range(h)]

for i in range(h):
    for j in range(w):
      if squares[i][j] == '
          if not j - 1 < 0:
              squares_target[i][j-1] += 1
          if not j - 1 < 0 and not i - 1 < 0:
              squares_target[i-1][j-1] += 1
          if not i - 1 < 0:
              squares_target[i-1][j] += 1
          if not j + 1 > w -1 and not i - 1 < 0:
              squares_target[i-1][j+1] += 1
          if not j + 1 > w -1 :
              squares_target[i][j+1] += 1
          if not j + 1 > w -1 and not i + 1 > h -1:
              squares_target[i+1][j+1] += 1
          if not i + 1 > h - 1:
              squares_target[i+1][j] += 1
          if not j - 1 < 0 and not i + 1 > h - 1:
              squares_target[i+1][j-1] += 1

for i in range(h):
    for j in range(w):
        if squares[i][j] == '
            squares_target[i][j] = '
    print(''.join(map(str, squares_target[i])))

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

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


for i in range(1,h+1):
    for g in range(1,w+1):
        b=0
        for l in [-1,0,1]:
            for m in [-1,0,1]:
                if not(l==0 and m==0) and s[i+l][g+m]=="
                    b+=1
        if s[i][g]==".":
            ans[i-1][g-1]=b
        else:
            ans[i-1][g-1]="

for i in range(h):
    st=""
    for g in range(w):
        st+=str(ans[i][g])
    print(st)

Solution (Python):









def resolve():


    H, W = map(int, input().split())
    maze = [input() for _ in range(H)]
    temp = [[0] * W for _ in range(H)]



    for h in range(H):
        for w in range(W):
            if maze[h][w] == ".":
                cnt=0
                for k, l in ([1, 0], [-1, 0], [0, 1], [0, -1],[1,1],[-1,1],[-1,-1],[1,-1]):

                    nh, nw = h + k, w + l
                    if nh < 0 or nh >= H or \
                            nw < 0 or nw >= W:
                        continue

                    elif maze[nh][nw] == "
                        cnt+=1

                temp[h][w]=cnt
            else:
                temp[h][w]='


    for i in temp:
        print(*i,sep='')

resolve()

Solution (Python):
def minesweeper(grid, x, y):
    if grid[x][y] == '.':
        cnt = 0
        for i in range(x-1, x+2): 
            if i < 0 or H-1 < i:
                pass
            else :
                for j in range(y-1, y+2):
                    if j < 0 or W - 1 < j:
                        pass
                    else :
                        if grid[i][j] == '
                            cnt += 1
        grid[x][y] = str(cnt)
    return grid


H, W = [int(i) for i in input().split()]

grid = []
for h in range(H):
    grid.append([l for l in input()])
    
for x in range(H):
    for y in range(W):
        grid = minesweeper(grid, x, y)
        
for row in grid:
    print(''.join(row))

Solution (Python):
a,b=map(int,input().split())
c=[]
for i in range(a):
  c+=[list(input())]
for i in range(a):
  for j in range(b):
    if c[i][j]=='.':
      d=0
      for k in range(i-1,i+2):
        for l in range(j-1,j+2):
          if 0<=k and k<a and 0<=l and l<b:
            d+=c[k][l]=='
      c[i][j]=str(d)
for i in c:
  print(''.join(i))

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

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

for i in range(h):
    for j in range(w):
        if s[i][j] == '.':
            bomb = 0
            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]] == '
                        bomb += 1
            s[i][j] = bomb
for i in s:
    print(*i, sep='')

Solution (Python):
h, w = map(int, input().split())
s = [list(input()) for _ in range(h)]
for i in range(h):
    s[i].insert(0,'9')
    s[i].append('9')
wall = ['9' for _ in range(w+2)]
s.insert(0, wall)
s.append(wall)
ans = [[0 for i in range(w)] for j in range(h)]
for i in range(1,h+1):
    for j in range(1, w+1):
        if s[i][j] == '.':
            cnt = 0
            for k in range(i-1,i+2):
                for l in range(j-1,j+2):
                    if s[k][l] == '
                        cnt += 1
            s[i][j] = cnt
        ans[i-1][j-1] = s[i][j]
for i in ans:
    print(*i, sep='')

Solution (Python):
inH, inW = map(int, input().split())
inMap = list()

for i in range(inH):
  inMap.append(list(input()))

def cntB(x,y):

  mv = [-1, 0, 1]
  cnt=0
  for dx in mv:
    if x+dx < 0 or x+dx>inH-1:
      continue
    for dy in mv:
      if y+dy<0 or y+dy>inW-1:
        continue
      if inMap[x+dx][y+dy]=="
        cnt+=1
      dy+=1
    dx+=1
    
  return cnt

for i in range(inH):
  s=""
  for j in range(inW):
    if inMap[i][j] == ".":
      s+=str(cntB(i,j))
    else:
      s+=str((inMap[i][j]))
  print(s)

Solution (Python):

dy = [-1, -1, -1, 0, 0, 1, 1, 1]
dx = [-1, 0, 1, -1, 1, -1, 0, 1]
field = []
H, W = map(int, input().split())
for _ in range(H):
    field.append(list(input()))
for y in range(H):
    for x in range(W):
        if field[y][x] == '
            continue
        count = 0
        for k in range(8): 
            nx = x + dx[k]
            ny = y + dy[k]
            if nx < 0 or W <= nx or ny < 0 or H <= ny: 
                continue
            if field[ny][nx] == '
                count += 1
        field[y][x] = str(count)
for h in range(H):
    print("".join(field[h]))

Solution (Python):
def minesweeper(grid, x, y):
    if grid[x][y] == '.':
        cnt = 0
        for i in range(x-1, x+2): 
            if i < 0 or H-1 < i:
                pass
            else :
                for j in range(y-1, y+2):
                    if j < 0 or W - 1 < j:
                        pass
                    else :
                        if grid[i][j] == '
                            cnt += 1
        grid[x][y] = str(cnt)
    return grid


H, W = [int(i) for i in input().split()]

grid = []
for h in range(H):
    grid.append([l for l in input()])
    
for x in range(H):
    for y in range(W):
        grid = minesweeper(grid, x, y)
        
for row in grid:
    print(''.join(row))

Solution (Python):
def resolve():
    h, w = map(int, input().split())
    L = []
    dx = [-1, -1, -1, 0, 0, 1, 1, 1]
    dy = [-1, 0, 1, -1, 1, -1, 0, 1]
    for i in range(h):
        L.append(list(input()))

    for i in range(h):
        for j in range(w):
            if L[i][j] == '
                pass
            else:
                count = 0
                for k in range(8):
                    x = i + dx[k]
                    y = j + dy[k]

                    if x < 0 or x > h - 1:
                        pass
                    elif y < 0 or y > w - 1:
                        pass
                    else:
                        if L[x][y] == "
                            count += 1
                L[i][j] = count

    for i in range(h):
        print(''.join(list(map(str, L[i]))))

if __name__ == '__main__':
    resolve()

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


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

for i in range(H):
  ans = ''
  for j in range(W):
    if S[i][j] == '
      ans += '
    else:
      count = 0  
      for d in range(8):
        x = i + dx[d]
        y = j + dy[d]
        if x in range(0,H) and y in range(0,W):
          if S[x][y] == '
            count += 1
      ans += str(count)
  print(ans)

Solution (Python):
H,W  = [int(x) for x in input().split()]
meiro= [list(input()) for _ in range(H)]
ans = [["0"]*W for _ in range(H)]
for i in range(H):
  for j in range(W):
    s = meiro[i][j]
    if s=="
      for a in range(3):
        for b in range(3):
          if 0<=i-1+a<H and 0<=j-1+b<W and ans[i-1+a][j-1+b]!="
            ans[i-1+a][j-1+b]= str(int(ans[i-1+a][j-1+b])+1)
      ans[i][j] ="
for i in range(H):
  print("".join(ans[i]))

Solution (Python):


h,w = list(map(int,input().split()))
s = [list(input()) for _ in range(h)]

d = [[1,0],[1,1],[0,1],[-1,1],[-1,0],[-1,-1],[0,-1],[1,-1]]
for i in range(h):
    for j in range(w):
        
        if s[i][j] == ".":
            count = 0
            for k in range(8):
                
                if 0 <= i+d[k][0] <= h-1 and 0 <= j+d[k][1] <= w-1:
                    
                    if s[i+d[k][0]][j+d[k][1]] == "
                        count += 1
            
            s[i][j] = str(count)


s = ["".join(s[i]) for i in range(h)]
for i in range(h):
    print(s[i])

Solution (Python):



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


def main():
    h, w = map(int, input().split())
    grid = []

    for i in range(h):
        grid.append(list(input()))

    

    for i in range(h):
        for j in range(w):
            

            if grid[i][j] == '
                continue

            cnt = 0

            for k in range(8):
                y = i + dy[k]
                x = j + dx[k]
                

                if y >= 0 and y < h and x >= 0 and x < w:
                    if grid[y][x] == '
                        cnt += 1
                else:
                    continue
            grid[i][j] = cnt
        

    for i in range(h):
        for j in range(w):
            print(grid[i][j], end="")

        print('')


main()

Solution (Python):
import sys
input = sys.stdin.readline
import copy

H, W = map(int, input().split(' '))
S = [list(input().rstrip('\r\n')) for _ in range(H)]
ans = copy.copy(S)

def count_bomb(h, w):
    hd_list = [-1, 0, 1]
    wd_list = [-1, 0, 1]
    count = 0
    for hd in hd_list:
        if h + hd < 0 or h + hd >= H:
            continue
        for wd in wd_list:
            if w + wd < 0 or w + wd >= W:
                continue
            if hd == 0 and wd == 0:
                continue
            if S[h+hd][w+wd] == '
                count += 1
    return count

for h in range(H):
    for w in range(W):
        if S[h][w] == '
            continue
        ans[h][w] = str(count_bomb(h, w))
for h in range(H):
    print(''.join(ans[h]))

Solution (Python):
h, w = map(int, input().split())

s = [list(input()) for _ in range(h)]
ans = [["

DX = [0, -1, -1, -1,  0,  1, 1, 1]
DY = [1,  1,  0, -1, -1, -1, 0, 1]

for i in range(h):
    for j in range(w):
        if s[i][j] == "
            continue
        count = 0
        for k in range(8):
            dx, dy = j+DX[k], i+DY[k]
            if not(0<=dx<=w-1 and 0<=dy<=h-1):
                continue
            if s[dy][dx] == "
                count += 1
        ans[i][j] = count
for a in ans:
    print(*a,sep="")

Solution (Python):
def resolve():
    h, w = map(int, input().split())
    L = [list(input()) for i in range(h)]
    dx = [-1, 0, 1, -1, 1, -1, 0, 1]
    dy = [-1, -1, -1, 0, 0, 1, 1, 1]
    count = 0
    for i in range(h):
        for j in range(w):
            if L[i][j] == "
                pass
            else:
                count = 0
                for k in range(8):
                    m = i + dx[k]
                    n = j + dy[k]
                    if m < 0 or n < 0:
                        pass
                    elif m >= h or n >= w:
                        pass
                    else:
                        if L[m][n] == "
                            count += 1
                L[i][j] = count
    for i in range(len(L)):
        print(''.join(map(str, L[i])))

if __name__ == '__main__':
    resolve()

Solution (Python):
H, W = map(int, input().split())
S = [input() for _ in range(H)]
ans = [[-1]*W for _ in range(H)]
for i in range(H):
  for j in range(W):
    cnt = 0
    if S[i][j] == "
      ans[i][j] = "
      continue
    for n in range(-1, 2):
      for m in range(-1, 2):
        if i+n < 0 or i+n > H-1: 
          continue
        if j+m < 0 or j+m > W-1:
          continue
        if S[i+n][j+m] == "
          cnt += 1
    ans[i][j] = cnt
for i in range(H):
  print(*ans[i], sep="")

Solution (Python):
H,W = map(int,input().split())
grid = [[-float("inf")]*W for _ in range(H)]
for i in range(H):
  s = input()
  for j in range(W):
    if s[j]==".":
      grid[i][j]=0
dire = [[-1,-1],[-1,0],[-1,1],[0,-1],[0,1],[1,-1],[1,0],[1,1]]
for r in range(H):
  for c in range(W):
    if grid[r][c] == -float("inf"):
      for d in dire:
        if 0 <= r+d[0] and r+d[0] < H and 0 <= c+d[1] and c+d[1] < W:
          grid[r+d[0]][c+d[1]] += 1
for r in range(H):
  for c in range(W):   
    if grid[r][c] == -float("inf"):
      grid[r][c] = "
    else:
      grid[r][c] = str(grid[r][c])
  print("".join(grid[r]))

Solution (Python):
h,w = map(int,input().split())
ls = [input() for _ in range(h)]
ll = [['
for x in range(h):
  for y in range(w):
    if ls[x][y]=='.':
      cnt = 0
      for dx,dy in ((1,0),(0,1),(-1,0),(0,-1),(1,1),(-1,1),(-1,-1),(1,-1)):
        nx,ny = x+dx,y+dy
        if not(0<=nx<h and 0<=ny<w): continue
        if ls[nx][ny]=='
      ll[x][y] = cnt
for l in ll:
  print(''.join(map(str,l)))

Solution (Python):
H,W=[int(s) for s in input().split()]
game=[input() for i in range(H)]
ans=[[] for i in range(H)]
direction=[[1,1],[1,0],[0,1],[1,-1,],[-1,1],[-1,0],[0,-1],[-1,-1]]
for i in range(H):
  for j in range(W):
    if game[i][j]=='
      ans[i].append('
      continue
    count=0
    for e in direction:
      y=i+e[0]
      x=j+e[1]
      if (x<0 or x>=W or y<0 or y>=H):
        continue
      if game[y][x]=='
        count+=1
    ans[i].append(count)
for i in range(H):
  print(*ans[i],sep='')

Solution (Python):
H, W = map(int, input().split())
grid = ['.'*(W+2)]
for _ in range(H):
    tmp = '.' + input() + '.'
    grid.append(tmp)
grid.append('.'*(W+2))
ans = []
for i in range(1,H+1):
    tmp = ''
    for k in range(1,W+1):
        if grid[i][k] == '
            tmp = tmp + '
        else:
            t = 0
            if grid[i][k-1] == '
                t += 1
            if grid[i][k+1] == '
                t += 1
            if grid[i+1][k] == '
                t += 1
            if grid[i-1][k] == '
                t += 1
            if grid[i-1][k-1] == '
                t += 1
            if grid[i+1][k+1] == '
                t += 1
            if grid[i+1][k-1] == '
                t += 1
            if grid[i-1][k+1] == '
                t += 1
            tmp = tmp + str(t)
    ans.append(tmp)
for i in ans:
    print(i)

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

dxy = [(-1, -1), (-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1)]
for y in range(H):
    for x in range(W):
        if src[y][x] == '
        c = 0
        for dx, dy in dxy:
            if x + dx < 0 or x + dx > W - 1 or y + dy < 0 or y + dy > H - 1: continue
            if src[y + dy][x + dx] == '
        ans[y][x] = c

for row in ans:
    print(''.join(list(map(str, row))))

Solution (Python):
import numpy as np
h,w=map(int,input().split())
L=[[i for i in input()] for i in range(h)]
L_first=np.zeros((2,w),dtype=str)
L_first2=np.zeros((1,1),dtype=str)
L=np.array(L,dtype=str)
L=np.insert(L_first,1,L,axis=0)
L=np.insert(L,0,L_first2,axis=1)
L=np.insert(L,w+1,L_first2,axis=1)
ans=np.zeros((h,w),dtype=str)
for i in range(1,h+1):
    for j in range(1,w+1):
        num = 0
        if L[i][j]==".":
            L2=L[i-1:i+2,j-1:j+2]
            ans[i-1][j-1]=np.sum(L2=="
        else:ans[i-1][j-1]="
for i in range(h):
    print("".join(ans[i]))

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

array = [input() for i in range(H)]

ans = [[0 for j in range(M)] for i in range(H)]

for i in range(H):
  for j in range(M):
    if array[i][j] == '.':
      if i != 0 and array[i-1][j] == '
        ans[i][j] += 1
      if i != 0 and j != 0 and array[i-1][j-1] == '
        ans[i][j] += 1
      if i != 0 and j != M-1 and array[i-1][j+1] == '
        ans[i][j] += 1
      if j != 0 and array[i][j-1] == '
        ans[i][j] += 1
      if j != M-1 and array[i][j+1] == '
        ans[i][j] += 1
      if i != H-1 and array[i+1][j] == '
        ans[i][j] += 1
      if i != H-1 and j != 0 and array[i+1][j-1] == '
        ans[i][j] += 1
      if i != H-1 and j != M-1 and array[i+1][j+1] == '
        ans[i][j] += 1
    else:
      ans[i][j] = '
for i in range(H):
  print(''.join(list(map(str,ans[i]))))

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

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

dr = [-1, -1, -1, 0, 1, 1, 1, 0]
dc = [-1, 0, 1, 1, 1, 0, -1, -1]

for i in range(h):
    for j in range(w):
        cnt = 0
        for k in range(8):
            nr = i + dr[k]
            nc = j + dc[k]
            if 0 <= nr < h and 0 <= nc < w:
               if s[nr][nc] == '
                   cnt += 1
        if s[i][j] == '
            ans[i][j] = '
        else:
            ans[i][j] = str(cnt)

for i in range(h):
    print(''.join(ans[i]))

Solution (Python):

h,w=map(int,input().split())
s=['.'*(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 s[i][j]=='.':
            s[i][j]=sum([s[i+_i][j+_j]=='
    print(*s[i][1:w+1],sep='')