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):
def func():
    h,w = map(int, input().split())
    S = ['.' + input() + '.' for _ in range(h)]
    S.insert(0,'.'*(w+2))
    S.append('.'*(w+2))
    
    for r in range(1, h+1):
        for c in range(1, w+1):
            if S[r][c] == '.':
                count = 0
                count += S[r-1][c-1:c+2].count('
                count += S[r][c-1:c+2].count('
                count += S[r+1][c-1:c+2].count('
                print(count, end='')
            else:
                print('
        print()

if __name__ == '__main__':
    func()

Solution (Python):
h,w = map(int, input().split())
S = ['.' + input() + '.' for _ in range(h)]
S.insert(0,'.'*(w+2))
S.append('.'*(w+2))

for r in range(1, h+1):
    for c in range(1, w+1):
        if S[r][c] == '.':
            count = 0
            count += S[r-1][c-1:c+2].count('
            count += S[r][c-1:c+2].count('
            count += S[r+1][c-1:c+2].count('
            print(count, end='')
        else:
            print('
    print()

Solution (Python):
h,w = map(int, input().split())
S = ['.' + input() + '.' for _ in range(h)]
S.insert(0,'.'*(w+2))
S.append('.'*(w+2))

for r in range(1, h+1):
    for c in range(1, w+1):
        if S[r][c] == '
            print('
            continue
        else:
            count = 0
            count += S[r-1][c-1:c+2].count('
            count += S[r][c-1:c+2].count('
            count += S[r+1][c-1:c+2].count('
            print(count, end='')
    print()

Solution (Python):
h,w = map(int, input().split())
S = [input() for _ in range(h)]
for r in range(h):
    l = ''
    for c in range(w):
        if S[r][c] == '
            l += '
        else:
            l += str(sum([x[max(0,c-1):min(c+2,w)].count('
    print(l)

Solution (Python):
h,w = map(int, input().split())
S = [input() for _ in range(h)]
for r in range(h):
    for c in range(w):
        count = 0
        if S[r][c] == '
            print('
        else:
            for x in S[max(0,r-1):min(r+2,h)]:
                count += x[max(0,c-1):min(c+2,w)].count('
            print(count, end='')
    print()

Solution (Python):
h,w = map(int, input().split())
S = ['.' + input() + '.' for _ in range(h)]
S.insert(0,'.'*(w+2))
S.append('.'*(w+2))

for r in range(1, h+1):
    for c in range(1, w+1):
        if S[r][c] == '
            print('
            continue
        else:
            count = 0
            count += S[r-1][c-1:c+2].count('
            count += S[r][c-1:c+2].count('
            count += S[r+1][c-1:c+2].count('
            print(count, end='')
    print()

Solution (Python):
h, w = map(int, input().split())
table = [[False] * w for _ in range(h)]
for i in range(h):
    s = input().rstrip()
    for j, c in enumerate(s):
        if c == '
            table[i][j] = True
res = [[] for _ in range(h)]
for i in range(h):
    for j in range(w):
        if table[i][j]:
            res[i].append('
            continue
        count = 0
        for k in range(-1, 2):
            for l in range(-1, 2):
                if i + k < 0 or i + k >= h or j + l < 0 or j + l >= w:
                    continue
                if k == 0 and l == 0:
                    continue
                if table[i + k][j + l]:
                    count += 1
        res[i].append(str(count))
for l in res:
    print(''.join(l))

Solution (Python):
import itertools
import sys

input = sys.stdin.readline


class AtCoder:
    def main(self):
        H, W = map(int, input().split())
        S = [input().rstrip() for _ in range(H)]

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

                cnt = 0
                for a in itertools.product((-1, 0, 1), repeat=2):
                    x, y = h + a[0], w + a[1]
                    if 0 <= x < H and 0 <= y < W and S[x][y] == '
                        cnt += 1
                S[h] = S[h][:w] + str(cnt) + S[h][w + 1:]
        print(*S, sep='\n')



if __name__ == '__main__':
    AtCoder().main()

Solution (Python):
h, w = map(int,input().split())
a = ["."+input()+"." for _ in range(h)]
a.insert(0,"."*(w+2))
a.append("."*(w+2))
y = [1,1, 1,-1,-1,-1,0, 0]
x = [0,1,-1, 0, 1,-1,1,-1]
for i in range(1,h+1):
    l =[]
    for j in range(1,w+1):
        if a[i][j] == "
            l += "
        else: l += str(sum([1 for k in range(8) if a[i+y[k]][j+x[k]] == "
    print("".join(l))

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

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

def dfs(i, j):
  if s[i][j] == '
    return '
  else:
    cnt = 0
    moves = [[1,0],[0,1],[-1,0],[0,-1],[1,1],[-1,1],[-1,-1],[1,-1]]
    for move in moves:
      if 0 <= i + move[0] < h and 0 <= j + move[1] <w:
        if s[i + move[0]][j + move[1]] == '
          cnt += 1
    return str(cnt)

for i in range(h):
  for j in range(w):
    print(dfs(i, j), end='')
  print()

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

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

for h in range(H):
    for w in range(W):
        if S[h][w] == "
            continue
        num = 0
        for d in range(8):
            hd, wd = h + dy[d], w + dx[d]

            if hd < 0 or H <= hd: continue
            if wd < 0 or W <= wd: continue
            if S[hd][wd] == "
        S[h][w] = str(num)

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

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

def f(m, i, j):
    if 0 <= i-1:
        start = j-1 if 0 <= j-1 else j
        end = j+2 if j+2 <= W else j+1
        for k in range(start, end):
            if m[i-1][k] != "
                m[i-1][k] = str(int(m[i-1][k]) + 1)
    if i+1 < H:
        start = j-1 if 0 <= j-1 else j
        end = j+2 if j+2 <= W else j+1
        for k in range(start, end):
            if m[i+1][k] != "
                m[i+1][k] = str(int(m[i+1][k]) + 1)
    if 0 <= j-1:
        if m[i][j-1] != "
            m[i][j-1] = str(int(m[i][j-1]) + 1)
    if j+1 < W:
        if m[i][j+1] != "
            m[i][j+1] = str(int(m[i][j+1]) + 1)
    return m

for h in range(H):
    for w in range(W):
        if S[h][w] == "
            S = f(S, h, w)

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

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

S = [list(input().replace(".", "0")) for _ in range(H)]  

def fill_cells_around_mine(i,j): 
    for y in range(max(0,i-1),min(H,i+2)):
        for x in range(max(0, j-1), min(W,j+2)):
            if S[y][x] != "
                S[y][x] = str(int(S[y][x])+1)

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

for idx in range(len(S)):
    print("".join(S[idx]))

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

S = []
for _ in range(H):
    S.append(list(str(input().replace(".", "0"))))  

def fill_blank_around_mine(i,j): 
    for y in range(max(0,i-1),min(H,i+2)):
        for x in range(max(0, j-1), min(W,j+2)):
            if S[y][x] != "
                S[y][x] = str(int(S[y][x])+1)

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

for idx in range(len(S)):
    print("".join(S[idx]))

Solution (Python):
import pprint
H, W = list(map(int, input().split()))
S = []
for h in range(H):
    S.append([c for c in input()])

for h in range(H):
    for w in range(W):
        if S[h][w] == ".":
            count = 0
            for pos_y in [-1, 0, 1]:
                for pos_x in [-1, 0, 1]:
                    y = h + pos_y
                    x = w + pos_x
                    if x < 0 or x >= W or y < 0 or y >= H:
                        continue
                    if S[y][x] == "
                        count += 1
            S[h][w] = str(count)

S = ["".join(s) for s in S]
S = "\n".join(S)
print(S)

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

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):
                for l in range(3):
                    x = k-1
                    y = l-1
                    if S[i+x][j+y]=="
                        num += 1
            S[i] = S[i][:j] + str(num) + S[i][j+1:]
for i in range(1,H+1):
    s = S[i]
    print(s[1:W+1])

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

grid = [['x'] * (w + 2)]

for _ in range(h):
    new_row = list(input())
    new_row.insert(0, 'x')
    new_row.append('x')
    grid.append(new_row)
grid.append(['x'] * (w + 2))

for i in range(1, h + 1):
    for j in range(1, w + 1):
        if grid[i][j] == '.':
            mine_num = 0
            for krow in range(-1, 2):
                for kcol in range(-1, 2):
                    if grid[i + krow][j + kcol] == '
                        mine_num += 1
            grid[i][j] = str(mine_num)

for i in range(1, h + 1):
    print(''.join(grid[i][1:w + 1]))

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

s = []
for i in range (h):
	s.append(list(input()))

for i in range (h):

	for n in range (w):

		if (s[i][n] == "

			r = "

		else:

			cnt = 0
			r = 0

			for a in ([-1, 1]):
				if(i + a < 0) or (i + a > h - 1):
					continue
				for t in ([-1, 0, 1]):
					if ((n + t) < 0) or ((n + t) > (w - 1)):
						continue
					if (s[i + a][n + t] == "
						cnt += 1
			for t in ([-1, 1]):
				if ((n + t) < 0) or ((n + t) > (w - 1)):
					continue
				if (s[i][n + t] == "
					cnt += 1

			r = cnt

		print (r, end="")

	print ("")

Solution (Python):
H, W = map(int, input().split())
S = [input() for _ in range(H)]
A = []
for i in range(H):
    A.append([0]*(W))
for i in range(H):
    for j in range(W):
        if S[i][j] == '
            A[i][j] = '
            if i-1 >= 0 and j-1 >= 0 and A[i-1][j-1] != '
            if i-1 >= 0 and A[i-1][j] != '
            if i-1 >= 0 and j+1 < W and A[i-1][j+1] != '
            if j-1 >= 0 and A[i][j-1] != '
            if j+1 < W and A[i][j+1] != '
            if i+1 < H and j-1 >= 0 and A[i+1][j-1] != '
            if i+1 < H and A[i+1][j] != '
            if i+1 < H and j+1 < W and A[i+1][j+1] != '
for i in range(H):
    line = []
    for j in range(W):
        line.append(str(A[i][j]))
    print(''.join(line))

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

padding = ['.']*(W+2)
S.insert(0, padding)
S.append(padding)
for i in range(1, H+1):
    S[i].insert(0, '.')
    S[i].append('.')

for i in range(1, H+1):
    for j in range(1,W+1):
        if S[i][j]=='.':
            S_part= S[i-1][j-1:j+2]+S[i][j-1:j+2]+S[i+1][j-1:j+2]
            S_cnt = S_part.count('
            S[i][j]=S_cnt
for i in range(1,H+1):
    print(*S[i][1:W+1],sep='')

Solution (Python):
import sys
import math
from fractions import gcd





def lcm(a, b):
    return a * b // gcd(a, b)

def combination_count(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

def permutations_count(n, r):
    return math.factorial(n) // math.factorial(n - r)

big_prime = 1000000007

"""


 =  sys.stdin.readline().rstrip()
 =  list(sys.stdin.readline().rstrip())


 =  int(sys.stdin.readline())
 =  map(int, sys.stdin.readline().split())
 =  list(map(int, sys.stdin.readline().split()))
 =  [list(map(int,list(sys.stdin.readline().split()))) for i in range(N)]
"""


H, W = map(int, sys.stdin.readline().split())
x = ["x" for _ in range(W + 2)]
DL = [x]

for _ in range(H):
    L = list(sys.stdin.readline().rstrip())
    L = ["x"] + L + ["x"]
    DL.append(L)
DL.append(x)

for i in range(1, H + 1):
    for j in range(1, W + 1):
        if DL[i][j] == ".":
            cnt = 0
            for k in range(i - 1, i + 2):
                for l in range(j - 1, j + 2):
                    if DL[k][l] == "
                        cnt += 1
            DL[i][j] = cnt

ans = []
for d in DL[1:len(DL) - 1]:
    print(''.join([str(x) for x in d if x != "x"]))

Solution (Python):
H, W = map(int,input().split())
S = [list(input()) for i in range(H)]
ansm = [[""]*W for i in range(H)]
for i in range(H):
    for j in range(W):
        cnt = 0
        if S[i][j]=="
            ansm[i][j]="
        else:
            if i>0 and j>0:
                if S[i-1][j-1]=="
                    cnt +=1
            if i>0:
                if S[i-1][j]=="
                    cnt+=1
            if i>0 and 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 and j>0:
                if S[i+1][j-1]=="
                    cnt+=1
            if i<H-1:
                if S[i+1][j]=="
                    cnt+=1
            if i<H-1 and j<(W-1):
                if S[i+1][j+1]=="
                    cnt+=1
            ansm[i][j]=str(cnt)
ans = [""]*H
for i in range(H):
    for j in range(W):
        ans[i]=ans[i]+ansm[i][j]
for i in range(H):
    print(ans[i])

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

for i in range(H):
    l = ''
    for j in range(W):
        if S[i][j] == '
            l += '
        else:
            l += str(sum([s[max(0, j-1):min(W, j+2)].count('
    print(l)

Solution (Python):
h, w = map(int,input().split())
a = [list(input()) for _ in range(h)]
b = [[0 for _ in range(w)]for _ in range(h)]
H = [-1,-1,-1, 0, 0, 1, 1, 1]
W = [-1, 0, 1,-1, 1,-1, 0, 1]
for i in range(h):
    for j in range(w):
        if a[i][j] == "
            b[i][j] = "
            for k in range(len(H)):
                if i+H[k] < 0 or i+H[k] >= h:continue
                elif j+W[k] < 0 or j+W[k] >= w:continue
                elif a[i+H[k]][j+W[k]] == "
                b[i+H[k]][j+W[k]] += 1
for i in range(h):
    print(*b[i],sep="")

Solution (Python):
h,w = map(int,input().split())
grid = ["."*(w+2)]+["."+input()+"." for _ in range(h)]+["."*(w+2)]
for y in range(1,h+1):
	for x in range(1,w+1):
		if grid[y][x]=="
			print("
		else:
			print(sum(grid[i][j]=="
	print("")

Solution (Python):
h, w = map(int,input().split())
a = [input() for _ in range(h)]
for i in range(h):
    l =""
    for j in range(w):
        if a[i][j] == "
            l += "
        else:
            l+=str(sum([s[max(0,j-1):min(w,j+2)].count("
    print(l)

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

move=[[-1,0],[-1,-1],[0,-1],[1,-1],[1,0],[1,1],[0,1],[-1,1]]

for y in range(h):
	for x in range(w):
		cnt=0
		if s[y][x]=='
		
		for i in range(8):
			yi=y+move[i][0]
			xi=x+move[i][1]
			if 0<=xi<w and 0<=yi<h:
				if s[yi][xi]=='
			else:continue
		sl=list(s[y])
		sl[x]=str(cnt)
		s[y]=''.join(sl)
print("\n".join(s))

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

SS='.'*(W+2)
S=['.'+input()+'.' for _ in range(H)]

SS=['.'*(W+2)]

SS.extend(S)

SE=['.'*(W+2)]

SS.extend(SE)

for i in range(1,H+1):
    for j in range(1, W+1):
        if SS[i][j]=='
            print('
        else:   
            print(str((SS[i-1][j-1]+SS[i][j-1]+SS[i+1][j-1]+SS[i-1][j]+SS[i+1][j]+SS[i-1][j+1]+SS[i][j+1]+SS[i+1][j+1]).count('
        if j==W:
            print('')

Solution (Python):
h, w = map(int, input().split())
mass = []
for i in range(h):
    x = input()
    mass.append([])
    for j in range(w):
        mass[i].append(x[j])

init = []
for i in range(h):
    
    init.append([])
    for j in range(w):
        init[i].append(0)

for i in range(h):
    for j in range(w):
        if mass[i][j] != "
            
            if i + 1 < h and mass[i + 1][j] == "
                init[i][j] += 1
                
            if i - 1 >= 0 and mass[i - 1][j] == "
                init[i][j] += 1
                
            if j + 1 < w and mass[i][j + 1] == "
                init[i][j] += 1
                
            if j - 1 >= 0 and mass[i][j - 1] == "
                init[i][j] += 1
            if j + 1 < w and i + 1 < h and mass[i + 1][j + 1] == "
                init[i][j] += 1

            if i - 1 >= 0 and j - 1 >= 0 and mass[i - 1][j - 1] == "
                init[i][j] += 1

            if i + 1 < h and j - 1 >= 0 and mass[i + 1][j - 1] == "
                init[i][j] += 1

            if i - 1 >= 0 and j + 1 < w and mass[i - 1][j + 1] == "
                init[i][j] += 1
        else:
            init[i][j] = "
res = []
for i in range(h):
    for j in range(w):
        res.append(str(init[i][j]))
    print(''.join(res))
    res = []

Solution (Python):
h,w=[int(i) for i in input().split()]
l=['n'*(w+2),'n'*(w+2)]
for _ in range(h):
	a=input()
	l.insert(1,'n'+a+'n')
l.reverse()
ans=['
for i in range(1,h+1):
	for j in range(1,w+1):
		cnt=0
		if l[i][j]=='
		if l[i][j+1]=='
		if l[i][j-1]=='
		if l[i-1][j-1]=='
		if l[i-1][j]=='
		if l[i-1][j+1]=='
		if l[i+1][j-1]=='
		if l[i+1][j]=='
		if l[i+1][j+1]=='
		ans_=list(ans[i-1])
		ans_[j-1]=str(cnt)
		ans_="".join(ans_)
		ans[i-1]=ans_
print('\n'.join(ans))

Solution (Python):
H,W=map(int,input().split());S=[input()for _ in range(H)];T=[""]*H
for i,l in enumerate(S):
 for j,c in enumerate(l):
  T[i]+=str(sum(S[i+p][j+q]=="
print("\n".join(T))

Solution (Python):

import sys


def ILI(): return list(map(int, sys.stdin.readline().rstrip().split()))
def ISI(): return map(int, sys.stdin.readline().rstrip().split())
def II(): return int(sys.stdin.readline().rstrip())
def ISS(): return sys.stdin.readline().rstrip().split()
def IS(): return sys.stdin.readline().rstrip()


dx = [-1,  0,  1, -1, 1, -1, 0, 1]
dy = [-1, -1, -1,  0, 0,  1, 1, 1]
H, W = ISI()
l = [list(IS()) for i in range(H)]
for i in range(H):
    for j in range(W):
        if l[i][j] != "
            cnt = 0
            for d in range(8):
                ni = i + dy[d]
                nj = j + dx[d]
                if 0 > ni or ni >= H: continue
                if 0 > nj or nj >= W: continue
                if l[ni][nj] == "
                    cnt += 1
            l[i][j] = str(cnt)
for i in range(H):
    print("".join(l[i]))

Solution (Python):
import itertools
H,W = map(int, input().strip().split(' '))

S = [list('.'*(W+2))]
S += [list('.'+input().strip()+'.') for _ in range(H)]
S.append(list('.'*(W+2)))

for x,y in itertools.product(range(1,W+1), range(1,H+1)):
  if S[y][x] == '
  S[y][x] = sum([S[y+dy][x+dx] == '

for y in range(1,H+1):
  print(*S[y][1:-1], sep='')

Solution (Python):
if __name__ == "__main__":
    H, W = map(int, input().split())
    
    bomb = "
    empty = "."
    empty_W = empty * (W + 2)
    
    s = []
    s.append(empty_W)
    for i in range(H):
        s_sub = input()
        s_sub = empty + s_sub + empty
        s.append(s_sub)
    s.append(empty_W)
    
    t = ["" for i in range(H)]
    for i in range(1, H + 1):
        for j in range(1, W + 1):
            if s[i][j] == empty:
                sij = 0
                for k in range(i - 1, i + 2):
                    for l in range(j - 1, j + 2):
                        if s[k][l] == bomb:
                            sij += 1
                t[i - 1] = t[i - 1] + str(sij)
            else:
                t[i - 1] = t[i - 1] + bomb
            
    for i in range(H):
        print(t[i])

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

Solution (Python):
def resolve():
    H, W = map(int, input().split())
    s_list = []
    answer = [[0] * W for i in range(H)]
    for i in range(H):
        s_list.append(list(input()))

    for h, s_h in enumerate(s_list):
        for w, s_h_w in enumerate(s_h):
            count = 0
            if s_h_w == '.':
                continue
            else:
                for x in range(-1,2):
                    target_x = w + x
                    if target_x < 0 or target_x >= W:
                        continue
                    for y in range(-1,2):
                        target_y = h + y
                        if target_y < 0 or target_y >= H:
                            continue
                        if s_list[target_y][target_x] != '
                            answer[target_y][target_x] += 1
                        else:
                            answer[target_y][target_x] = '

    for i in answer:
        print("".join(map(str,i)))

resolve()

Solution (Python):
import itertools

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

for i in range(h):
    for j in range(w):
        cnt = 0
        if s[i][j] != "
            for dx, dy in itertools.product([-1, 0, 1], repeat=2):
                y = i+dy
                x = j+dx
                if y < 0 or y >= h:
                    continue
                if x < 0 or x >= w:
                    continue
                if s[y][x] == "
                    cnt += 1
            s[i][j] = str(cnt)

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

Solution (Python):
H, W = map(int, input().split())
S = [input() for i in range(H)]
a = [[0 for i in range(W)] for j in range(H)]
for i in range(H):
    for j in range(W):
        if S[i][j]== '
            a[i][j] = '
            for p in [-1, 0, 1]:
                for q in [-1, 0, 1]:
                    if i + p >= 0 and j + q >= 0 and i + p < H and j + q < W and S[i + p][j + q] != "
                        a[i + p][j + q] += 1
for i in range(H):
    print(''.join(map(str, a[i])))

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

def main():
    H, W = map(int, input().split())
    s = [input().strip() for i in range(H)]

    ans = [[0] * W for i in range(H)]
    direction = [
        [-1, -1], [0, -1], [1, -1],
        [-1, 0],           [1, 0],
        [-1, 1],  [0, 1],  [1, 1]
    ]
    for y in range(H):
        for x in range(W):
            if s[y][x] == ".":
                for dx, dy in direction:
                    tx, ty = x+dx, y+dy
                    if 0 <= tx and tx < W and 0 <= ty and ty < H:
                        if s[ty][tx] == "
                            ans[y][x] += 1
    for y in range(H):
        for x in range(W):
            if s[y][x] == "
                print("
            else:
                print(ans[y][x], end="")
        print()


if __name__ == "__main__":
    main()

Solution (Python):



def output_string8(table, x, y):

    string8 = []

    string8.append(table[x-1][y+1])
    string8.append(table[x][y+1])
    string8.append(table[x+1][y+1])
    string8.append(table[x-1][y])
    string8.append(table[x+1][y])
    string8.append(table[x-1][y-1])
    string8.append(table[x][y-1])
    string8.append(table[x+1][y-1])

    return ''.join(string8)


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

S.append('X' * (W+2))

for _ in range(H):
    line = 'X' + input() + 'X'
    S.append(line)

S.append('X' * (W+2))

BOMB = '

for y in range(1, H + 1):
    for x in range(1, W + 1):

        ch = S[y][x]

        if ch == BOMB:
            print(BOMB, end='')
            continue

        st = output_string8(S, y, x)
        print(st.count(BOMB), end='')

    print(end='\n')

Solution (Python):
h,w=map(int,input().split())
s=[]
for i in range(h):
    s.append(input())
def cnt(i,j):
    tmp=0
    if i!=0:
        if s[i-1][j]=='
            tmp+=1
    if i!=h-1:
        if s[i+1][j]=='
            tmp+=1
    if j!=0:
        if s[i][j-1]=='
            tmp+=1
    if j!=w-1:
        if s[i][j+1]=='
            tmp+=1
    if i!=0 and j!=0:
        if s[i-1][j-1]=='
            tmp+=1
    if i!=0 and j!=w-1:
        if s[i-1][j+1]=='
            tmp+=1
    if i!=h-1 and j!=0:
        if s[i+1][j-1]=='
            tmp+=1
    if i!=h-1 and j!=w-1:
        if s[i+1][j+1]=='
            tmp+=1
    return tmp

t=[]
for i in range(h):
    tmp=[]
    for j in range(w):
        if s[i][j]=='.':
            tmp.append(cnt(i,j))
        else:
            tmp.append('
    t.append(tmp)
for i in range(h):
    print(*t[i],sep='')

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

for i in range(h):
    mines_id = [m_i for m_i, word in enumerate(s[i]) if word == '
    if mines_id == []:
        continue
    else:
        for mine_id in mines_id:
            if i - 1 >= 0:
                ans[i - 1][mine_id] += 1
            if i + 1 <= h - 1:
                ans[i + 1][mine_id] += 1
            if mine_id - 1 >= 0:
                ans[i][mine_id - 1] += 1
            if mine_id + 1 <= w - 1:
                ans[i][mine_id + 1] += 1
            if i - 1 >= 0 and mine_id - 1 >= 0:
                ans[i - 1][mine_id - 1] += 1
            if i + 1 <= h - 1 and mine_id + 1 <= w - 1:
                ans[i + 1][mine_id + 1] += 1
            if i + 1 <= h - 1 and mine_id - 1 >= 0:
                ans[i + 1][mine_id - 1] += 1
            if i - 1 >= 0 and mine_id + 1 <= w - 1:
                ans[i - 1][mine_id + 1] += 1

for i in range(h):
    mines_id = [m_i for m_i, word in enumerate(s[i]) if word == '
    if mines_id == []:
        continue
    else:
        for mine_id in mines_id:
            ans[i][mine_id] = '

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

Solution (Python):
H, W=map (int, input().split(' '))
z={}

for i in range(H):
    z[i]=str(input())



for h in range(H):
    for w in range(W):
        if z[h][w:w+1] == "
            print('
            continue
        p=0
        for dy in [-1, 0, 1]:
            y=h+dy
            if h + dy < 0 or h + dy >= H:
                continue
            for dx in [-1, 0, 1]:
                x=w+dx
                if x < 0 or x >= W:
                    continue
                if x == w and y ==h:
                    continue
                
                if z[y][x:x+1] == "
                    p+=1
        print(p,end="")
    print("\n", end="")

Solution (Python):
h,w=map(int,input().split())
s=[list(input()) for i in range(h)]
map=[[ int(0) for i in range(w)] for j in range(h)]
for i in range(h):
	for j in range(w):
		if s[i][j]=='
			map[i][j]='
		else:
			for k in range(-1,2):
				for p in range(-1,2):
					if 0<=i+k and i+k<h and 0<=j+p and j+p<w and s[i+k][j+p]=='
						map[i][j]+=1
			map[i][j]=str(map[i][j])
for i in range(h):	
	print(''.join(map[i]))

Solution (Python):
h,w = map(int, input().split())
L = [list(input()) for _ in range(h)] 
for i in range(h):
  for k in range(w):
    if L[i][k]=="
      continue
    count=0
    for v in range(-1, 2):
      if i+v < 0 or i+v > h-1:
        continue
      for t in range(-1, 2):
        if k+t < 0 or k+t > w-1:
          continue
        if L[i+v][k+t]=="
          count+=1
    L[i][k] = str(count)
for c in range(h):
  print("".join(L[c]))

Solution (Python):
from itertools import product

def resolve():
    h,w=map(int,input().split())
    A=[list('.'+input()+'.') for _ in range(h)]
    A=[['.']*(w+2)]+A+[['.']*(w+2)]

    for i,j in product(range(1,h+1),range(1,w+1)):
        if A[i][j]=='.':
            A[i][j]=((A[i-1][j-1]=='
            +(A[i][j-1]=='
            +(A[i+1][j-1]=='

    for i in range(1,h+1):
        print(*A[i][1:w+1],sep='')
resolve()

Solution (Python):
H, W = map(int, input().split())
S = [[] for _ in range(H)]
for i in range(H):
    S[i] = list(input())
dx = [1, 0, -1, 0, 1, -1, -1, 1]
dy = [0, 1, 0, -1, 1, 1, -1, -1]
for i in range(H):
    for j in range(W):
        if S[i][j] == '.':
            num = 0
            for d in range(8):
                ni = i + dx[d]
                nj = j + dy[d]
                if ni < 0 or H <=ni:
                    continue
                if nj < 0 or W <=nj:
                    continue
                if S[ni][nj] == '
                    num += 1
            S[i][j] = num
for i in range(H):
    a = map(str, S[i])
    print("".join(a))

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

h,w = map(int,input().split())
S = [[] for _ in range(h)]
for i in range(h):
    S[i] = list(input())
ANS = [[0]*(w+2) for _ in range(h+2)]
for i in range(h):
    for j in range(w):
        if S[i][j] == "
            ANS[i+1][j+1] = "
            for k,l in [(i,j),(i,j+1),(i,j+2),(i+1,j),(i+1,j+2),(i+2,j),(i+2,j+1),(i+2,j+2)]:
                if not ANS[k][l] == "
for i in range(1,h+1):
    a = map(str,ANS[i][1:-1])
    print("".join(a))

Solution (Python):

import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**6)

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

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

for i in range(h):
    for j in range(w):
        if (s[i][j] == '
            for k in range(-1, 2):
                for l in range(-1, 2):
                    if (0 <= i+k < h and 0 <= j+l < w and ans[i+k][j+l] != '
                        ans[i+k][j+l] += 1
            ans[i][j] = '

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

Solution (Python):
H, W = map(int, input().split())
List1 = [list(map(str, input())) for row in range(H)]
List2 = [[0 for col in range(W)] for row in range(H)]
for row in range(H):
    for col in range(W):
        if List1[row][col] == "
            List2[row][col] = "
        else:
            c = 0
            for rdf in range(-1, 2):
                if 0 <= row + rdf < H:
                    for cdf in range(-1, 2):
                        if 0 <= col + cdf < W:
                            if List1[row + rdf][col + cdf] == "
                                c += 1
            List2[row][col] = c
for row in range(H):
    print(*List2[row], sep="")