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 add(i, j):
    global count, H, W
    if 0 <= i < H and 0 <= j < W:
        count[i][j] += 1

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

count = [[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, 1):
                for h in range(-1, 2, 1):
                    if k == h == 0:
                        continue
                    else:
                        add(i + k, j + h)

ans = [[""] * W for _ in range(H)]

for i in range(H):
    for j in range(W):
        if S[i][j] == "
            ans[i][j] = "
        else:
            ans[i][j] = str(count[i][j])

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

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

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

dh = [-1, -1, -1, 0, 0, 1, 1, 1]
dw = [-1, 0, 1, -1, 1, -1, 0, 1]

for i in range(H):
	for j in range(W):
		if SL[i][j] == "
			pass
		elif SL[i][j] == ".":
			c = 0
			for k in range(8):
				if 0 <= i + dh[k] and i + dh[k] <= H - 1 and 0 <= j + dw[k] and j + dw[k] <= W - 1:
					if SL[i + dh[k]][j + dw[k]] == "
						c += 1
			SL[i][j] = str(c)
		else:
			pass

for i in range(H):
	print("".join(SL[i]))

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

for i in range(1, H+1):
  for j in range(1, W+1):
    if S[i][j] == '.':
      k = 0
      k += 1 if S[i-1][j-1]=='
      k += 1 if S[i-1][j]=='
      k += 1 if S[i-1][j+1]=='
      k += 1 if S[i][j-1]=='
      k += 1 if S[i][j+1]=='
      k += 1 if S[i+1][j-1]=='
      k += 1 if S[i+1][j]=='
      k += 1 if S[i+1][j+1]=='
      print(k, end='')
    else:
      print('
  print()

Solution (Python):
g,r=map(int,input().split())
A=["."*(r+2)]
for i in range(g):
  w="."+input()+"."
  A.append(w)
A.append("."*(r+2))

for i in range(1,g+1):
  for j in range(1,r+1):
    if A[i][j]==".":
      B=[]
      for m in [i-1,i,i+1]:
        for k in [j-1,j,j+1]:
          B.append(A[m][k])
     
      A[i]=A[i][1:r+1:].replace(".",str(B.count("
      A[i]="."+A[i]+"."
for i in range(1,g+1):
  print(A[i][1:r+1:])

Solution (Python):



def main():
    """Function."""
    h, w = map(int, input().split())

    s = []
    s.append(["!" for _ in range(w+2)])
    for _ in range(h):
        dum = str(input())
        dum = list(dum)
        dum.insert(0, "!")
        dum.append("!")
        s.append(list(dum))
    s.append(["!" for _ in range(w+2)])

    for _i in range(h):
        for _j in range(w):
            i = _i + 1
            j = _j + 1
            if s[i][j] == "
                pass
            else:
                counter = 0
                if s[i-1][j-1] == "
                    counter += 1
                if s[i-1][j] == "
                    counter += 1
                if s[i-1][j+1] == "
                    counter += 1
                if s[i][j-1] == "
                    counter += 1
                if s[i][j+1] == "
                    counter += 1
                if s[i+1][j-1] == "
                    counter += 1
                if s[i+1][j] == "
                    counter += 1
                if s[i+1][j+1] == "
                    counter += 1
                s[i][j] = str(counter)

    for i in range(h):
        dum = s[i+1][1:-1]
        dum_2 = "".join(dum)
        print(dum_2)


if __name__ == "__main__":
    main()

Solution (Python):
def main():
    h, w = map(int, input().split())
    coord = []
    coord.append([0] * (w + 2))
    for _ in range(h):
        coord.append([0] + list(input()) + [0])
    coord.append([0] * (w + 2))

    dys = [-1, -1, 0, 1, 1, 1, 0, -1]
    dxs = [0, 1, 1, 1, 0, -1, -1, -1]

    for y in range(1, h + 1):
        for x in range(1, w + 1):
            if coord[y][x] == '
                continue

            coord[y][x] = 0
            for dy, dx in zip(dys, dxs):
                if coord[y + dy][x + dx] == '
                    coord[y][x] += 1

    for row in coord[1:-1]:
        print(''.join(map(str, row[1:-1])))


if __name__ == '__main__':
    main()

Solution (Python):
def listToString(_list, split=""):
    maped_list = map(str, _list)  
    mojiretu = split.join(maped_list)
    return mojiretu


h, w = [int(i) for i in input().split()]
s = [[0 for _ in range(w + 2)] for _ in range(h + 2)]
table = [[0 for _ in range(w + 2)] for _ in range(h + 2)]

for i in range(h):
    _s = input()
    s[i + 1][1:1+w] = [_s[i] for i in range(len(_s))]

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

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

def add_bomb(area, i, j):
    if i+1 < h:
        area[i+1][j] += 1
        if j+1 < w:
            area[i+1][j+1] += 1
        if j-1 >= 0:
            area[i+1][j-1] += 1

    if i-1 >= 0:
        area[i-1][j] += 1
        if j+1 < w:
            area[i-1][j+1] += 1
        if j-1 >= 0:
            area[i-1][j-1] += 1

    if j+1 < w:
        area[i][j+1] += 1
    if j-1 >= 0:
        area[i][j-1] += 1

    return area

for i in range(h):
    for j, x in enumerate(list(input())):
        if x == '
            area = add_bomb(area, i, j)
            bomb[i][j] = 0

for a_ls, b_ls in zip(area, bomb):
    print(''.join([str(a) if b == 1 else '

Solution (Python):

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


lst = ["."*(W+2)] + ["." + input() + "." for _ in range(H)] + ["."*(W+2)]


lst2 = []
for _ in range(H+2):
    lst2.append([0 for _ in range(W+2)])


for i in range(1, H+1):
    for j in range(1, W+1):
        cnt = 0
        if lst[i][j] == ".":
            cnt += lst[i-1][j-1:j+2].count("
            cnt += lst[i][j-1:j+2].count("
            cnt += lst[i+1][j-1:j+2].count("
            lst2[i][j] = str(cnt)
        else:
            lst2[i][j] = "

for i in range(1, H+1):
    print("".join(lst2[i][1:W+1]))

Solution (Python):
def draw_map(count_map, row_index, col_index) :
	for i in range(row_index-1, row_index+2) :
		if (i < 0 or len(count_map) <= i) :
			continue
		for j in range(col_index-1, col_index+2) :
			if (j < 0 or len(count_map[0]) <= j) :
				continue
			count_map[i][j] += 1
	
	return count_map
    				
    			

H,W = [int(i) for i in input().split()]
mines = []
for i in range(H) :
	mines.append(input())



count_map = [[0 for i in range(W)] for j in range(H)]
for i in range(H) :
	for j in range(W) :
		if (mines[i][j] == "
			mine_map = draw_map(count_map,i,j)

for i in range(H) :
	out_line = ""
	for j in range(W) :
		if (mines[i][j] == ".") :
			out_line += str(count_map[i][j])
		else :
			out_line += "
	print(out_line)

Solution (Python):
import copy as cp 
H, W = map(int, input().split(" "))
L = [list(input()) for i in range(H)]
a = ["
A = [cp.copy(a) for i in range(H)]
for i in range(H):
  for j in range(W):
    if L[i][j] == "
      continue
    cnt = 0
    for ii in range(-1, 2):
      for jj in range(-1, 2):
        if ii == 0 and jj == 0:
          continue
        iii = i + ii
        jjj = j + jj
        if iii < 0 or iii > H -1:
          continue
        if jjj < 0 or jjj > W -1:
          continue
        if L[iii][jjj] == "
          cnt+=1
    A[i][j] = str(cnt)
for i in range(len(A)):
  print(''.join(A[i]))

Solution (Python):
H, W = map(int, input().split(" "))
L = [list(input()) for i in range(H)]
a = ["
A = [a for i in range(H)]
ans = []
for i in range(H):
  for j in range(W):
    if L[i][j] == "
      ans.append("
      continue
    cnt = 0
    for ii in range(-1, 2):
      for jj in range(-1, 2):
        if ii == 0 and jj == 0:
          continue
        iii = i + ii
        jjj = j + jj
        if iii == i and jjj == j:
          continue
        if iii < 0 or iii > H -1:
          continue
        if jjj < 0 or jjj > W -1:
          continue
        if L[iii][jjj] == "
          cnt+=1
    ans.append(str(cnt))
    A[i][j] = str(cnt)
  print("".join(ans))
  ans = []

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


for x in range(1,H+1):
    for y in range(1,W+1):
        cnt=0
        if A[x][y]==".":
            if A[x-1][y-1]=="
                cnt+=1
            if A[x-1][y]=="
                cnt+=1
            if A[x-1][y+1]=="
                cnt+=1
            if A[x][y-1]=="
                cnt+=1
            if A[x][y]=="
                cnt+=1
            if A[x][y+1]=="
                cnt+=1
            if A[x+1][y-1]=="
                cnt+=1
            if A[x+1][y]=="
                cnt+=1
            if A[x+1][y+1]=="
                cnt+=1
            A[x][y]=str(cnt)
i=0
j=0
ans=""
for i in range(1,H+1):
    for j in range(1,W+1):
        ans+=A[i][j]
    print(ans)
    ans=""

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

for i in range(1, H + 1):
  for j in range(1, W + 1):
    if grid[i][j] == '
      ans.append('
    else:
      ans.append(str((grid[i-1][j-1:j+2]+grid[i][j-1:j+2]+grid[i+1][j-1:j+2]).count('

  ans.append('\n')
print(''.join(ans))

Solution (Python):
def around(grid,h,w,x,y):
    ans = []
    if y-1 >= 0:
        ans.append(grid[y-1][x])
        if x-1 >= 0:
            ans.append(grid[y-1][x-1])
        if x+1 < w:
            ans.append(grid[y-1][x+1])
    if x-1 >= 0:
        ans.append(grid[y][x-1])
    if x+1 < w:
        ans.append(grid[y][x+1])
    if y+1 < h:
        ans.append(grid[y+1][x])
        if x-1 >= 0:
            ans.append(grid[y+1][x-1])
        if x+1 < w:
            ans.append(grid[y+1][x+1])
    return ans
        
h,w = map(int,input().split())
grid = []
for i in range(h):
    s = list(input())
    grid.append(s)

for y in range(h):
    for x in range(w):
        if grid[y][x] != "
            tile = around(grid,h,w,x,y)
            counter = 0
            for i in range(len(tile)):
                if tile[i] == "
                    counter += 1
            grid[y][x] = counter

for i in range(h):
    for k in range(w):
        if k != w-1:
            print(grid[i][k],end="")
        else:
            print(grid[i][k])

Solution (Python):
li = []
a, b = map(int,input().split())
for i in range(a):
    li.append(list(input()))

for i in range(a):
    for j in range(b):
        if li[i][j] == '.':
            li[i][j] = 0
            if i!=0 and li[i-1][j]=='
                li[i][j]+=1
            if i!=a-1 and li[i+1][j]=='
                li[i][j]+=1
            if j!=0 and li[i][j-1]=='
                li[i][j]+=1
            if j!=b-1 and li[i][j+1]=='
                li[i][j]+=1
            if i!=0 and j!=0 and li[i-1][j-1]=='
                li[i][j]+=1
            if i!=a-1 and j!=0 and li[i+1][j-1]=='
                li[i][j]+=1
            if i!=0 and j!=b-1 and li[i-1][j+1]=='
                li[i][j]+=1
            if i!=a-1 and j!=b-1 and li[i+1][j+1]=='
                li[i][j]+=1
        print(li[i][j],end="")
    print()

Solution (Python):
import sys
H, W = map(int, raw_input().split())
S = [raw_input() for i in range(H)]
C = [[0] * W for i in range(H)]
for i in range(H):
    for j in range(W):
        for di in range(-1, 2):
            if i + di < 0 or H <= i + di:
                continue
            for dj in range(-1, 2):
                if j + dj < 0 or W <= j + dj:
                    continue
                C[i][j] += 1 if S[i + di][j + dj] == "
for i in range(H):
    for j in range(W):
        if S[i][j] == "
            sys.stdout.write("
        else:
            sys.stdout.write(str(C[i][j]))
    print ""

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

grid = [list('.'+input()+'.') for i in range(H)]

grid.insert(0, list('.' * (W + 2)))
grid.append(list('.' * (W + 2)))

ans = []

for i in range(1, H + 1):
  for j in range(1, W + 1):
    if grid[i][j] == '
      ans.append('
    else:
      
      
      
      ans.append(str((grid[i-1][j-1:j+2]+grid[i][j-1:j+2]+grid[i+1][j-1:j+2]).count('
  
  ans.append('\n')
print(''.join(ans))

Solution (Python):
h,w = map(int,input().split())
board = [input() for _ in range(h)]
dx = [-1,-1,-1, 0,0, 1,1,1]
dy = [-1, 0, 1,-1,1,-1,0,1]
ans_board = [[0 for _ in range(w)] for _ in range(h)]

for x in range(h):
    for y in range(w):
        cnt = 0
        if board[x][y] == '
            ans_board[x][y] = '
        else:
            for d in range(len(dx)):
                n_x = x+dx[d]
                n_y = y+dy[d]

                if 0 <= n_x < h and 0 <= n_y < w:
                    if board[n_x][n_y] == '
                        cnt += 1
            ans_board[x][y] = str(cnt)

for ans in ans_board:
    print('' .join(ans))

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

for row_num, padded_row in enumerate(all_info[1: -1]):
    for collumn_num, s in enumerate(padded_row[1: -1]):
        if s == '
            continue
        else:
            all_info[row_num + 1][collumn_num + 1] = str((all_info[row_num][collumn_num: collumn_num +3]
                                                                                     + all_info[row_num + 2][collumn_num: collumn_num + 3]
                                                                                     + all_info[row_num + 1][collumn_num: collumn_num + 3]).count('

for row in all_info[1: -1]:
    print(''.join(row[1: -1]))

Solution (Python):
H, W = map(int, input().split())
all_info = [list(input()) for i in range(H)]
for i, line in enumerate(all_info):
    all_info[i] = ['.'] + line + ['.']
all_info = [['.' for i in range(W+2)]] + all_info + [['.' for i in range(W+2)]]

for row_num, padded_row in enumerate(all_info[1: -1]):
    for collumn_num, s in enumerate(padded_row[1: -1]):
        if s == '
            continue
        else:
            all_info[row_num + 1][collumn_num + 1] = str((all_info[row_num][collumn_num: collumn_num +3]
                                                                           + all_info[row_num + 2][collumn_num: collumn_num + 3]
                                                                            + all_info[row_num + 1][collumn_num: collumn_num + 3]).count('

for row in all_info[1: -1]:
    print(''.join(row[1: -1]))

Solution (Python):
H, W = map(int, input().split())
all_info = [list(input()) for i in range(H)]
for i, line in enumerate(all_info):
    all_info[i] = ['.'] + line + ['.']
all_info = [['.' for i in range(W+2)]] + all_info + [['.' for i in range(W+2)]]
all_count = [[-1 for i in range(W)] for j in range(H)]

for row_num, padded_row in enumerate(all_info[1: -1]):
    for collumn_num, s in enumerate(padded_row[1: -1]):
        if s == '
            all_count[row_num][collumn_num] = '
        else:
            all_count[row_num][collumn_num] = str((all_info[row_num][collumn_num: collumn_num +3]
                                                                           + all_info[row_num + 2][collumn_num: collumn_num + 3]
                                                                            + all_info[row_num + 1][collumn_num: collumn_num + 3]).count('

for count in all_count:
    print(''.join(count))

Solution (Python):
import itertools

H, W = [int(_) for _ in input().split()]
S = [input() for _ in range(H)]
memo = [[0] * W for _ in range(H)]
for i, j in itertools.product(range(H), range(W)):
    if S[i][j] == '
        memo[i][j] = '
        for k, l in itertools.product(range(-1, 2), repeat=2):
            if 0 <= i+k and i+k<H and 0<=j+l and j+l<W and memo[i+k][j+l] != '
                memo[i+k][j+l] += 1
[print(''.join(map(str, _))) for _ in memo]

Solution (Python):
import itertools
h,w=map(int,input().split())
s=[list(input()) for i in range(h)]
l=list(itertools.product([-1,0,1],repeat=2))

for i in range(h):
    for j in range(w):
        if s[i][j]=='.':
            cnt = 0
            for k in l:
                x = i+k[0]
                y = j+k[1]
                if 0<=x<=h-1 and 0<=y<=w-1:
                    if s[x][y]=='
                        cnt+=1
            s[i][j]=str(cnt)
for i in s:
    print(''.join(i))

Solution (Python):
size = input().split(' ')
H = int(size[0])
W = int(size[1])
S = []
for k in range(H):
    row = input()
    S.append(row)

T = []
for i in range(H):
    TI = ''
    for j in range(W):
        
        count = 0
        for s in range(0,3):
            for t in range(0,3):
                
                if (0 <= i + s - 1 < H) and (0 <= j + t - 1 < W) and (S[i+s-1][j+t-1] == '
                    count +=1
        if S[i][j] == '
            TI = TI + '
        elif S[i][j] != '
            TI = TI + str(count)
    T.append(TI)
for u in range(H):
    print(T[u])

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

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

grid2 = [[0 for i in range(W)] for j in range(H)]

for i in range(H):
    for j in range(W):
        if grid[i][j] == "
            grid2[i][j] = "
            continue
        count = 0
        for k in range(3):
            for l in range(3):
                if(i+k-1 < H and  i+k-1 >= 0 and j+l-1 < W and j+l-1 >=0 and grid[i+k-1][j+l-1] == "
                    count+=1
        grid2[i][j] = str(count)

for row in grid2:
    print("".join(row))

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

s = ['-'*(w+2)]
for i in range(h):
    s += ['-'+input()+'-']
s += ['-'*(w+2)]

for hi in range(1,h+1):
    ans = ''
    for wi in range(1,w+1):
        if s[hi][wi] == '
            ans += '
        else:
            c = [s[hi-1][wi-1],s[hi][wi-1],s[hi+1][wi-1],s[hi-1][wi],s[hi+1][wi],s[hi-1][wi+1],s[hi][wi+1],s[hi+1][wi+1]]
            ans += str(c.count('
    print(ans)

Solution (Python):
import copy
H, W = map(int, input().split())
S = [input() for i in range(H)]
dd = ((-1, 1), (-1, 0), (-1, -1), (0, -1), (1, -1), (1, 0), (1, 1), (0, 1))
T = copy.deepcopy(S)
count = 0
for i in range(H):
    for j in range(W):
        if S[i][j] == '
            continue
        for dx, dy in dd:
            nx = j + dx; ny = i + dy
            if 0 <= nx < W and 0 <= ny < H:
                if S[ny][nx] == '
                    count += 1
        T[i] = list(T[i])
        T[i][j] = str(count)
        T[i] = "".join(T[i])
        count = 0
for k in range(len(T)):
    print(T[k])

Solution (Python):
H,W = map(int,input().split())
S = [list(input()) for i in range(H)] 
def judge(l,i,j):
    count = 0
    for a in [-1,0,1]:
        for b in [-1,0,1]:
            if a == 0 and b == 0:
                continue
            else:
                if (not 0 <= i+a <= H-1) or (not 0 <= j+b <= W-1):
                    continue
                elif S[i+a][j+b] == "
                    count += 1
    return count
for i in range(H):
    for j in range(W):
        if S[i][j] == ".":
            S[i][j] = judge(S,i,j)
        print(S[i][j],end = "")
    print("")

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

for i in range(H):
    for j in range(W):
        if MAP[i][j] == '
            if i != 0:
                NUM[i-1][j] += 1
                if j != 0:
                    NUM[i-1][j-1] += 1
                if j != W-1:
                    NUM[i-1][j+1] += 1
            if j != 0:
                NUM[i][j-1] += 1
            if j != W-1:
                NUM[i][j+1] += 1
            if i != H-1:
                NUM[i+1][j] += 1
                if j != W-1:
                    NUM[i+1][j+1] += 1
                if j != 0:
                    NUM[i+1][j-1] += 1
for i in range(H):
    for j in range(W):
        if MAP[i][j] == '
            NUM[i][j] = '
    else:
        print(''.join(map(str, NUM[i])))

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

src = [input() for i in range(H)]
ans = []

for row in src:
  ans.append(list(row))

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):
from itertools import product

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

for i in range(H):
    for j in range(W):
        count = 0
        if table[i][j] == '
            continue
        for y, x in product((-1, 0, 1), repeat=2):
            if not (0 <= i + y < H and 0 <= j + x < W):
                continue
            if table[i + y][j + x] == '
                count += 1
        table[i][j] = str(count)

for row in table:
    print(''.join(row))

Solution (Python):
tate, yoko = map(int, input().split())
main = [input() for i in range(tate)] 

ans = []

for row in main: 
    ans.append(list(row)) 
    
dxy = [(-1,-1), (-1,0), (-1,1), (0,-1), (0,1), (1,-1), (1,0), (1,1)]

for y in range(tate):
    for x in range(yoko):
        if main[y][x] == "
        c = 0
        for dx,dy in dxy:
            if x + dx < 0 or x + dx > yoko - 1 or y + dy < 0 or y + dy > tate - 1: continue
            if main[y + dy][x + dx] == "
                    c += 1
        ans[y][x] = c
for row in ans:
    print("".join(list(map(str, row))))

Solution (Python):
H, W = [int(i) for i in input().split()]
Map = []
for i in range(H):
    Map.append(list(input()))
for i in range(H):
    for j in range(W):
        if Map[i][j] != '.':
            print(Map[i][j], sep="", end="")
            continue
        bomb = 0
        if i > 0 and Map[i-1][j] == '
            bomb += 1
        if i < H-1 and Map[i+1][j] == '
            bomb += 1
        if j > 0 and Map[i][j-1] == '
            bomb += 1
        if j < W-1 and Map[i][j+1] == '
            bomb += 1
        if i > 0 and j > 0 and Map[i-1][j-1] == '
            bomb += 1
        if i < H-1 and j > 0 and Map[i+1][j-1] == '
            bomb += 1
        if i > 0 and j < W -1 and Map[i-1][j+1] == '
            bomb += 1
        if i < H -1 and j < W -1 and Map[i+1][j+1] == '
            bomb += 1
        Map[i][j] = str(bomb)
        print(Map[i][j], sep="", end="")
    print()

Solution (Python):
[ height , width ] = list(map(int,input().split()))

input_map = []
output_map = [[0 for j in range(width)] for i in range(height)]

def is_sharp( input_map , x, y , width , height):
	if( x==-1 or y == -1 or x+1 > width or y+1 > height  ) :return 0
	
	if( input_map[y][x] == '
		return 1
	else:
		 return 0

def count_sharp_num( input_map , x , y):
	count = 0
	count += is_sharp(input_map , x-1 , y-1 , width , height)
	count += is_sharp(input_map , x-1 , y , width , height)
	count += is_sharp(input_map , x-1 , y+1 , width , height)
	count += is_sharp(input_map , x , y-1 , width , height)
	count += is_sharp(input_map , x , y+1 , width , height)
	count += is_sharp(input_map , x+1 , y-1 , width , height)
	count += is_sharp(input_map , x+1 , y , width , height)
	count += is_sharp(input_map , x+1 , y+1 , width , height)
	return count

for i in range( height ):
	input_map.append( input() )


for x in range( width ):
	for y in range( height ):
		output_map[y][x] = count_sharp_num(input_map,x,y)
		if input_map[y][x] == '
			output_map[y][x] = '


for y in range(height):
	print( ''.join(map(str,output_map[y])))

Solution (Python):
import itertools
h, w = map(int, input().split())
cells = [[c for c in input()] for i in range(h)]

for y in range(h):
    for x in range(w):
        cnt = 0
        if cells[y][x] == '
            continue
        for dy, dx in itertools.product((-1, 0, 1), repeat=2):
            if not (0 <= y + dy < h and 0 <= x + dx < w):
                continue
            if cells[y + dy][x + dx] == '
                cnt += 1
        cells[y][x] = str(cnt)

for line in cells:
    print(''.join(line))

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

ans = []
for i in range(1,H+1):
    R = ''
    for j in range(1,W+1):
        count = 0
        if grid[i][j] == '
        else:
            loop = [(i-1,j-1), (i-1,j), (i-1,j+1), (i,j-1), (i,j+1), (i+1,j-1),(i+1,j),(i+1,j+1)]
            for tmp in loop:
                if grid[tmp[0]][tmp[1]] == '
            R += str(count)
    ans.append(R)

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

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

for h in range(H):
    for w in range(W):
        if S[h][w] == '
            continue
        else:
            R = 0
            for x in [-1, 0, 1]:
                for y in [-1, 0, 1]:
                    if x == 0 and y == 0:
                        continue
                    elif 0 <= h + x and h + x < H and 0 <= w + y and w + y < W:
                        if S[h + x][w + y] == '
                            R += 1
            S[h][w] = str(R)

for s in S:
    print(''.join(s))

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

ANS = [[] for i in range(H)]
for y in range(H) :
    for x in range(W) :
        if L[y][x] == "
            ANS[y].append("
        else :
            mine = 0
            for dx,dy in dxy :
                nx = x + dx
                ny = y + dy
                if (0 <= nx <= W-1) and (0 <= ny <= H-1) :
                    if L[ny][nx] == "
                        mine += 1
            ANS[y].append(str(mine))
for i in range(H) :
    ans = "".join(ANS[i])
    print(ans)

Solution (Python):
h, w = map(int, input().split())
s = [['.'] * (w + 2)]
s += [['.'] + list(input()) + ['.'] for _ in range(h)]
s += [['.'] * (w + 2)]
t = s
for i in range(1, h + 1):
    for j in range(1, w + 1):
        if s[i][j] == '.':
            n = 0
            for k in range(3):
                n += s[i - 1 + k][j - 1:j + 2].count('
            t[i][j] = str(n)
for i in range(1, h + 1):
    tt =''
    for j in range(1, w + 1):
        tt += t[i][j]
    print(tt)

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] == ".":
            S[i][j] = 0
            for k in range(-1, 2):
                for l in range(-1, 2):
                    if k != 0 or l != 0:
                        if 0 <= i+k < H and 0 <= j+l < W and S[i+k][j+l] == "
                            S[i][j] += 1

            S[i][j] = str(S[i][j])

    print("".join(S[i]))

Solution (Python):
h,w = map(int,input().split())
field = []
xx = [-1, -1, -1, 0, 0, 1, 1, 1]
yy = [-1, 0, 1, -1, 1, -1, 0, 1]
for i in range(h):
    field.append(input())

for i in range(h):
    line = ''
    for j in range(w):
        if field[i][j] == '
            line += '
        else:
            temp = 0
            for x,y in zip(xx,yy):
                if i+x < 0 or i+x > h-1:
                    continue
                elif j+y < 0 or j+y > w-1:
                    continue
                elif field[i+x][j+y] == '
                    temp += 1
            line += str(temp)
                
    print(line)

Solution (Python):
H, W = map(int, input().split())
S = [input() for i in range(H)]
C = [[0 for i in range(W)] for j 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 S[i][j] == '.':
      for k in range(8):
        if 0 <= j + dx[k] < W and 0 <= i + dy[k] < H:
          if S[i + dy[k]][j + dx[k]] == '
            C[i][j] += 1
    else:
      C[i][j] = '
 
for i in range(H):
  for j in range(W):
    print(C[i][j], end = '')
  print('\n')

Solution (Python):
H, W = map(int, input().split())
S = [input() for i in range(H)]
C = [[0 for i in range(W)] for j 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 S[i][j] == '.':
      for k in range(8):
        if 0 <= j + dx[k] < W and 0 <= i + dy[k] < H:
          if S[i + dy[k]][j + dx[k]] == '
            C[i][j] += 1
          else:
            pass
        else:
          pass
    else:
      C[i][j] = '
 
for i in range(H):
  for j in range(W):
    print(C[i][j], end = '')
  print('\n')

Solution (Python):
t = []
H = 0
W = 0

def is_bomb(h, w):
    if 0 <= h and h < H and 0 <= w and w < W:
        if t[h][w] == '
            return 1

    return 0


def count_bomb(h, w):
    global t

    num = 0
    if is_bomb(h - 1, w - 1):
        num += 1

    if is_bomb(h - 1, w):
        num += 1

    if is_bomb(h - 1, w + 1):
        num += 1

    if is_bomb(h, w + 1):
        num += 1

    if is_bomb(h + 1, w + 1):
        num += 1

    if is_bomb(h + 1, w):
        num += 1

    if is_bomb(h + 1, w - 1):
        num += 1

    if is_bomb(h, w - 1):
        num += 1

    t[h][w] = str(num)
        

def do_main():
    global H, W, t

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

    t = []

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

    for h in range(H):
        for w in range(W):
           if t[h][w] != '
                count_bomb(h, w) 


    for h in range(H):
        print(''.join(t[h]))


do_main()