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 _ in range(W+2)]]
for _ in range(H):
    row = list(input())
    row.insert(0,".")
    row.append(".")
    S.append(row)
S.append(["." for _ in range(W+2)])
A = [["
for i in range(1,H+1):
    for j in range(1,W+1):
        if S[i][j]==".":
            cnt = 0
            if S[i][j+1]=="
                cnt += 1
            if S[i-1][j+1]=="
                cnt += 1
            if S[i-1][j]=="
                cnt += 1
            if S[i-1][j-1]=="
                cnt += 1
            if S[i][j-1]=="
                cnt += 1
            if S[i+1][j-1]=="
                cnt += 1
            if S[i+1][j] =="
                cnt += 1
            if S[i+1][j+1]=="
                cnt += 1
            A[i-1][j-1] = str(cnt)
for i in range(H):
    print("".join(A[i]))

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

for h in range(H):
  s=list(input())
  li.append(s)

for h in range(H):
  for w in range(W):
    if li[h][w]=='.':
      li[h][w]=0

for h in range(H):
  for w in range(W):
    if li[h][w]=='
      if h!=0 and li[h-1][w]!='
        li[h-1][w]+=1
      if h!=H-1 and li[h+1][w]!='
        li[h+1][w]+=1
      if w!=0 and li[h][w-1]!='
        li[h][w-1]+=1
      if w!=W-1 and li[h][w+1]!='
        li[h][w+1]+=1
      if h!=0 and w!=0 and li[h-1][w-1]!='
        li[h-1][w-1]+=1
      if h!=0 and w!=W-1 and li[h-1][w+1]!='
        li[h-1][w+1]+=1
      if h!=H-1 and w!=0 and li[h+1][w-1]!='
        li[h+1][w-1]+=1
      if h!=H-1 and w!=W-1 and li[h+1][w+1]!='
        li[h+1][w+1]+=1

for h in range(H):
  for w in range(W):
    if li[h][w]!='
      li[h][w]=str(li[h][w])

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

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

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


def dfs(x, y):
    s = S[x][y]
    if s == '
        ans[x][y] = '
        for dx in range(-1, 2):
            for dy in range(-1, 2):
                nx = x + dx
                ny = y + dy
                if 0 <= nx < h and 0 <= ny < w and S[nx][ny] != '
                    ans[nx][ny] += 1


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

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

Solution (Python):
h, w = map(int, input().split())
m = [[0]*(w+2)]
for _ in range(h):
    m.append([0]+[0 if s == '.' else s for s in input()]+[0])
m.append([0]*(w+2))
around = [(i, j)for i in range(-1, 2) for j in range(-1, 2)]
for i in range(1, h+1):
    for j in range(1, w+1):
        if m[i][j] == '
            for x, y in around:
                if m[i+x][j+y] != '
                    m[i+x][j+y] += 1

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

Solution (Python):
def slove():
    import sys
    input = sys.stdin.readline
    h, w = list(map(int, input().rstrip('\n').split()))
    s = [list(str(input().rstrip('\n'))) for _ in range(h)]
    t = [[0] * w for _ in range(h)]
    for i in range(h):
        for j in range(w):
            if s[i][j] == "
                tq = [[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 k, l in tq:
                    if 0 <= k < h and 0 <= l < w:
                        if s[k][l] != "
                            t[k][l] += 1
    for i in range(h):
        sp = []
        for j in range(w):
            if s[i][j] != "
                sp.append(str(t[i][j]))
            else:
                sp.append("
        print("".join(sp))


if __name__ == '__main__':
    slove()

Solution (Python):
h,w=map(int,input().split())
s=[input()for _ in range(h)]
x=[0,-1,-1,-1,0,1,1,1]
y=[1,1,0,-1,-1,-1,0,1]
for i in range(h):
    z=[]
    for j in range(w):
        if s[i][j]=='.':
            c=0
            for k in range(8):
                if 0<=i+x[k]<=h-1 and 0<=j+y[k]<=w-1:
                    if s[i+x[k]][j+y[k]]=='
                        c+=1
            z.append(c)
        else:
            z.append('
    print(*z,sep='')

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

S=[input() for _ in range(H)]

ans =[]

for i in range(H):
    s = ''
    for j in range(W):
        if S[i][j] != '
            cnt = 0
            for k in range(-1, 2):
                for m in range(-1, 2):
                    col = i + k
                    row = j + m
                    if (col < 0 or row < 0 or col > H-1 or row > W-1) or (k == 0 and m == 0):
                        continue
                    else:
                        if S[col][row] == '
                            cnt += 1
            s += str(cnt)
        else:
            s += '
    ans.append(s)

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

Solution (Python):
h, w = map(int, input().split())
s = [[] for _ in range(h+2)]
s[0] = ["." for _ in range(w+2)]
s[h+1] = ["." for _ in range(w+2)]
for i in range(1, h+1):
    s[i].append(".")
    line = list(input())
    for j in range(w):
        s[i].append(line[j])
    s[i].append(".")

def bomb(x,y):
    if s[y][x] == "
        return 1
    else:
        return 0

for y in range(1,h+1):
    for x in range(1,w+1):
        if s[y][x] == ".":
            s[y][x] = bomb(x-1,y-1) + bomb(x,y-1) + bomb(x+1,y-1) + bomb(x-1,y) + bomb(x+1,y) + bomb(x-1,y+1) + bomb(x,y+1) + bomb(x+1,y+1)

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

Solution (Python):
H,W=map(int,input().split())
C=[]
for h in range(H):
    S=input()
    C.append([0 if s=='.' else s for s in S])

for h in range(H):
    for w in range(W):
        if C[h][w]=='
            down=False if h+1>=H else True
            up=False if h-1<0 else True
            left=False if w-1<0 else True
            right=False if w+1>=W else True
            if up:
                if C[h-1][w]!="
                    C[h-1][w]+=1
                if left:
                    if C[h-1][w-1]!="
                        C[h-1][w-1]+=1
                if right:
                    if C[h-1][w+1]!="
                        C[h-1][w+1]+=1
            if down:
                if C[h+1][w]!="
                    C[h+1][w]+=1
                if left:
                    if C[h+1][w-1]!="
                        C[h+1][w-1]+=1
                if right:
                    if C[h+1][w+1]!="
                        C[h+1][w+1]+=1
            if left:
                if C[h][w-1]!="
                    C[h][w-1]+=1
            if right:
                if C[h][w+1]!="
                    C[h][w+1]+=1
        else:
            continue

for c in C:
    for n in c:
        print(n,end="")
    print()

Solution (Python):
h,w = map(int,input().split())
S = ['.'*(w+2)]+['.'+input()+'.' for i in range(h)]+['.'*(w+2)]
for i in range(1,h+1):
    for j in range(1,w+1):
        if S[i][j]=='.':
            mine = 0
            for k in range(-1,2):
                for l in range(-1,2):
                    if S[i+k][j+l]=='
                        mine += 1
            S[i] = S[i][:j]+str(mine)+S[i][j+1:]
for s in range(1,h+1):
    print(S[s][1:w+1])

Solution (Python):
h,w=map(int,input().split())
list1=[[0 for i in range(w)] for j in range(h)]
list2=[[0 for i in range(w)] for j in range(h)]
dx=[-1,-1,-1,0,0,1,1,1]
dy=[-1,0,1,-1,1,-1,0,1]

for j in range(h):
    s=input()
    for i in range(w):
        list1[j][i]=s[i]
        
for j in range(h):
    for i in range(w):
        if list1[j][i] == '
            for k in range(8):
                xk=i+dx[k]
                yk=j+dy[k]
                if xk >=0 and xk < w and yk >= 0 and yk < h:
                    list2[yk][xk] +=1
                    
for j in range(h):
    for i in range(w):
        if list1[j][i] == '
            list2[j][i] ='
        else:
            list2[j][i]=str(list2[j][i])

for j in range(h):
    print("".join(list2[j]))

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


def get_or_period(l, passed_x, passed_y):
    if passed_x in [-1, w]:
        return '.'
    if passed_y in [-1, h]:
        return '.'
    return l[passed_y][passed_x]


convertedMatrix = []

for y, row in enumerate(matrix):
    convertedMatrix.append([])
    for x, cell in enumerate(row):
        convertedCell = 0
        if matrix[y][x] == "
            convertedCell = "
            convertedMatrix[y].append(convertedCell)
            continue
        for exam_x in [-1, 0, 1]:
            for exam_y in [-1, 0, 1]:
                if get_or_period(matrix, x + exam_x, y + exam_y) == "
                    convertedCell += 1
        convertedMatrix[y].append(convertedCell)

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

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

Solution (Python):
h, w = map(int, input().split())
s_list = [list(input()) for _ in range(h)]
dx_list = [1, 1, 0, -1, -1, -1, 0, 1]
dy_list = [0, 1, 1, 1, 0, -1, -1, -1]
for i in range(h):
    for j in range(w):
        if s_list[i][j] == '
            continue
        num = 0
        for dx, dy in zip(dx_list, dy_list):
            x = j + dx
            y = i + dy
            if x < 0 or x >= w:
                continue
            if y < 0 or y >= h:
                continue
            if s_list[y][x] == '
                num += 1
        s_list[i][j] = str(num)
    answer = ''.join(s_list[i])
    print(answer)

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

for i in range(h):
    for j in range(w):
        if li[i][j] == "
            continue
        cnt = 0
        for kk in (-1,0,1):
            for ll in (-1,0,1):
                if not 0 <= i+kk < h or not 0<= j+ll < w:
                    continue
                if li[i+kk][j+ll] == "
                    cnt += 1
        li[i][j] = str(cnt)
for i in range(h):
    print("".join(li[i]))

Solution (Python):
h,w = map(int,input().split())
li,li2,li3 = [],[],[]
for i in range(h):
    li.append((input()))
li2.append("."*(w+2))
for i in range(h):
    li2.append("."+li[i]+".")
li2.append("."*(w+2))
cnt = 0
ans = [[0 for x in range(w)] for y in range(h)]
for i in range(1,h+1):
    for j in range(1,w+1):
        if li2[i][j] == ".":
            if li2[i-1][j-1] == "
                ans[i-1][j-1] += 1
            if li2[i-1][j] == "
                ans[i-1][j-1] += 1
            if li2[i-1][j+1] == "
                ans[i-1][j-1] += 1
            if li2[i][j-1] == "
                ans[i-1][j-1] += 1
            if li2[i][j+1] == "
                ans[i-1][j-1] += 1
            if li2[i+1][j-1] == "
                ans[i-1][j-1] += 1
            if li2[i+1][j] == "
                ans[i-1][j-1] += 1
            if li2[i+1][j+1] == "
                ans[i-1][j-1] += 1
        elif li2[i][j] == "
            ans[i-1][j-1] = "
for i in range(h):
    ans[i] = [str(x) for x in ans[i]]
for i in range(h):
    print("".join(ans[i]))

Solution (Python):
h, w = map(int, input().split())
s = [input() for _ in range(h)]
"""
    全ての位置が0のマップ(二次元リスト)を新しく作る
    
        位置が端なら加算は片側のみ
    8以下の数値を文字列として、9以上の数値を
"""
new_map = [[0 for _ in range(w)] for _ in range(h)]


def mine_count(i, j):
    """現在地に+9と周囲に+1"""
    new_map[i][j] += 9
    if i != 0:
        new_map[i-1][j] += 1
    if i != (h-1):
        new_map[i+1][j] += 1
    if j != 0:
        new_map[i][j-1] += 1
    if j != (w-1):
        new_map[i][j+1] += 1
    if i != 0 and j != 0:
        new_map[i-1][j-1] += 1
    if i != 0 and j != (w-1):
        new_map[i-1][j+1] += 1
    if i != (h-1) and j != 0:
        new_map[i+1][j-1] += 1
    if i != (h-1) and j != (w-1):
        new_map[i+1][j+1] += 1



for i in range(h):
    for j in range(w):
        if s[i][j] == '
            mine_count(i, j)

for i in range(h):
    for j in range(w):
        if new_map[i][j] <= 8:
            new_map[i][j] = str(new_map[i][j])
        else:
            new_map[i][j] = '

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

Solution (Python):
h,w=map(int,input().split())
s=['.'+input()+'.' for _ in range(h)]
s=['.'*(w+2)]+s+['.'*(w+2)]
for i in range(1,h+1):
    for j in range(1,w+1):
        cnt=0
        if s[i][j]=='
        else:
            if s[i-1][j-1]=='
            if s[i-1][j]=='
            if s[i-1][j+1]=='
            if s[i][j-1]=='
            if s[i][j+1]=='
            if s[i+1][j-1]=='
            if s[i+1][j]=='
            if s[i+1][j+1]=='
            print(cnt,end='')
    print()

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

Solution (Python):
h,w=map(int,input().split())
list1=[[0 for i in range(w)]for j in range(h)]
list2=[[0 for i in range(w)]for j in range(h)]
dx=[-1,-1,-1,0,0,1,1,1]
dy=[-1,0,1,-1,1,-1,0,1]
for j in range(h):
 s=input()
 for i in range(w):
  list1[j][i]=s[i]
for j in range(h):
  for i in range(w):
   if list1[j][i]=="
    for k in range(8):
     xk=i+dx[k]
     yk=j+dy[k]
     if xk>=0 and xk<w and yk>=0 and yk<h:
      list2[yk][xk]+=1
for j in range(h):
  for i in range(w):
    if list1[j][i]=="
      list2[j][i]="
    else:
      list2[j][i]=str(list2[j][i])
for j in range(h):
 print("".join(list2[j]))

Solution (Python):
h,w=(int(x) for x in input().split())
s=[]
for i in range(h):
  s.append(input())
for i in range(h):
  res=[]
  for j in range(w):
    if s[i][j]==".":
      ini_i=i if i==0 else i-1
      fin_i=i if i==h-1 else i+1
      ini_j=j if j==0 else j-1
      fin_j=j if j==w-1 else j+1
      count=0
      for x in range(ini_i,fin_i+1):
        for y in range(ini_j,fin_j+1):
          if s[x][y]=="
            count+=1
      res.append(str(count))
    else:
      res.append("
  print("".join(res))

Solution (Python):
h,w=map(int,input().split())
l=[]
for i in range(h):
    d=list(map(str,input()))
    d=['.']+d+['.']
    l.append(d)
l=[['.']*(w+2)]+l+[['.']*(w+2)]

for i in range(1,h+1):
    for j in range(1,w+1):
        if l[i][j]=='
            continue
        else:
            ans=0
            ans+=l[i-1][j-1:j+2].count('
            ans+=l[i+1][j-1:j+2].count('
            if l[i][j-1]=='
            if l[i][j+1]=='
            l[i][j]=ans
for i in range(1,h+1):
    print("".join(map(str,l[i][1:-1])))

Solution (Python):

import numpy as np
h, w = map(int, input().split())
fi = np.array([['.' for _ in range(w)] for _ in range(h)])
for i in range(h):
    si = input()
    for j, s in enumerate(si):
        fi[i, j] = s
xs = [-1, 0, 1]
ys = [-1, 0, 1]
for i in range(h):
    for j in range(w):
        if fi[i, j] == '
            continue
        else:
            cnt = 0
            for x in xs:
                for y in ys:
                    ni = i + x
                    nj = j + y
                    if (0 <= ni <= h-1) and (0 <= nj <= w-1):
                        if fi[ni, nj] == '
                            cnt += 1
            fi[i, j] = str(cnt)
for row in fi:
    print(''.join(row))

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

Solution (Python):
h,w = map(int, input().split())
S = [input() for i 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 = []
for h in range(H):
  S.append(list(input()))
  
dx = [-1,  0,  1, -1, 1, -1, 0, 1]
dy = [-1, -1, -1,  0, 0,  1, 1, 1]

for h in range(H):
  for w in range(W):
    if S[h][w] == ".":
      bomb = 0
      for nx, ny in zip(dx, dy):
        if h+nx<0 or w+ny<0 or h+nx>=H or w+ny>=W:
          pass
        else:
          if S[h+nx][w+ny] == '
            bomb+=1
      S[h][w] = str(bomb)
    else:
      pass

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

Solution (Python):
def count_neighbor_bomb(H,W,ci,cj, grid):
    count = 0
    for i in range(-1,2):
        if (ci + i)< 0 or (ci + i) > H-1:
            continue
        for j in range(-1,2):
            if (cj + j)< 0 or (cj + j) > W-1:
                continue
            if grid[ci + i][cj + j] == "
                count += 1
    return count

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

for i in range(H):
    for j in range(W):
        if grid[i][j] == "
            print("
            continue
        bomb_num = count_neighbor_bomb(H,W,i,j,grid)
        print(grid[i][j].replace(".", str(bomb_num)), end="")
    print("")

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

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

for i in range(1, h + 1):
    for j in range(1, w + 1):
        cnt = 0
        if m[i][j] == "
            ans[i - 1][j - 1] = "
            continue
        for dx in [-1, 0, 1]:
            for dy in [-1, 0, 1]:
                if m[i + dx][j + dy] == "
                    cnt += 1
        ans[i - 1][j - 1] = str(cnt)

for i in ans:
    print("".join(i))

Solution (Python):
h,w=map(int,input().split())
list1=[[0 for i in range(w)]for j in range(h)]
list2=[[0 for i in range(w)]for j in range(h)]
dx=[-1,-1,-1,0,0,1,1,1]
dy=[-1,0,1,-1,1,-1,0,1]
for j in range(h):
 s=input()
 for i in range(w):
  list1[j][i]=s[i]
for j in range(h):
  for i in range(w):
   if list1[j][i]=="
    for k in range(8):
     xk=i+dx[k]
     yk=j+dy[k]
     if xk>=0 and xk<w and yk>=0 and yk<h:
      list2[yk][xk]+=1
for j in range(h):
  for i in range(w):
    if list1[j][i]=="
      list2[j][i]="
    else:
      list2[j][i]=str(list2[j][i])
for j in range(h):
 print("".join(list2[j]))

Solution (Python):
H,W = map(int,input().split())
S = [list(input()) for _ in range(H)]
for i in range(H):
    for l in range(W):
        if S[i][l] == ".":
            s = 0
            if l <= W-2:
                if S[i][l+1] == "
                    s += 1
            if l >= 1:
                if S[i][l-1] == "
                    s += 1
            if i >= 1 and l <= W-2:
                if S[i-1][l+1] == "
                    s += 1
            if i >= 1:
                if S[i-1][l] == "
                    s += 1
            if i >= 1 and l >= 1:
                if S[i-1][l-1] == "
                    s += 1
            if i <= H-2 and l <= W-2:
                if S[i+1][l+1] == "
                    s += 1
            if i <= H-2:
                if S[i+1][l] == "
                    s += 1
            if i <= H-2 and l >= 1:
                if S[i+1][l-1] == "
                    s += 1
            S[i][l] = str(s)
for i in range(H):
    print("".join(S[i]))

Solution (Python):
def count_mine(m, i, j):
    count = 0
    for k in range(i - 1, i + 2):
        for l in range(j - 1, j + 2):
            if m[k][l] == "
                count += 1
    return count

H, W = map(int, input().split())
l = [list(map(str, input().split())) for i in range(H)]
m = []
m.append("*" * (W + 2))
for i in range(len(l)):
    add_colum = "*" + ''.join(l[i]) + "*"
    m.append(add_colum)
m.append("*" * (W + 2))
answer = []
for i in range(1, H + 1):
    answer_column = ""
    for j in range(1, W + 1):
        if m[i][j] == ".":
            answer_column += str(count_mine(m, i, j))
        else:
            answer_column += "
    answer.append(answer_column)
for i in range(H):
    print(answer[i])

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

grid = []

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





dir = [[0,1],[-1,1],[-1,0],[-1,-1],[0,-1],[1,-1],[1,0],[1,1]]

for i in range(h):
    for j in range(w):
        if grid[i][j] == '.':
            bomb=0
            
            for k in range(8):
                if 0 <= i + dir[k][0] <= h-1 and 0 <= j + dir[k][1] <=w-1:
                    if grid[i + dir[k][0]][j + dir[k][1]] == '
                        bomb += 1
            grid[i][j] = str(bomb)




for i in range(h):
    result = ''.join(grid[i])
    print(result)

Solution (Python):
H,W=map(int,input().split())
S=[input() for _ in range(H)]
for i in range(H):
  T=''
  for j in range(W):
    if S[i][j] == '
      T+='
    else:
      T+=str(sum([x[max(0,j-1):min(j+2,W)].count('
    
  print(T)

Solution (Python):



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 n in range(8):
      nx=x+dx[n]
      ny=y+dy[n]
        
      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 i in range(H):
  print(''.join(field[i]))

Solution (Python):

H,W=map(int,input().split())
boxes=list()
pad=[0]*(W+2)
boxes.insert(0,pad)
for i in range(H):
  x=input()
  box=list()
  box.append(0)
  for j in range(W):
    if x[j]=='.':
      box.append(0)
    elif x[j]=='
      box.append(1)
  box.append(0) 
  boxes.insert(i+1,box)
boxes.insert(H+1,pad)
answers=list()
padd=[0]*W
for n in range(H):
  answer=list()
  for k in range(W):
    count=0
    if boxes[n][k]==1:
      count+=1
    if boxes[n][k+1]==1:
      count+=1
    if boxes[n][k+2]==1:
      count+=1
    if boxes[n+1][k]==1:
      count+=1
    if boxes[n+1][k+2]==1:
      count+=1
    if boxes[n+2][k]==1:
      count+=1
    if boxes[n+2][k+1]==1:
      count+=1
    if boxes[n+2][k+2]==1:
      count+=1
    if boxes[n+1][k+1]==1:
      count='
    else:
      count=str(count)
    answer.insert(k,count)
  answers.insert(n,answer)
for v in range(H):
  answe=''.join(answers[v])
  print(answe)

Solution (Python):
h,w = map(int,input().split())
s = [list(input()) for i in range(h)]
vec = [-1,0,1]
for i in range(h): 
    for j in range(w): 
        if s[i][j] == '.':
            s[i][j] = '0'
        if s[i][j] != '
            for m in vec: 
                for n in vec: 
                    ver = i + m
                    hor = j + n
                    if 0 <= ver and ver <= h-1 and 0 <= hor and hor <= w-1 and s[ver][hor] == '
                        s[i][j] = str(1+int(s[i][j]))
for i in range(h):
    print(''.join(s[i]))

Solution (Python):
h,w=map(int,input().split())
s=[['.'*(w+2)]]+[list('.'+input()+'.') for i 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]=str(s[i-1][j-1:j+2].count('
    print(''.join(s[i][1:w+1]))

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

for i in range(1, h + 1):
    ans = []
    for j in range(1, w + 1):
        ans += str(s[i][j])
    print(''.join(ans))

Solution (Python):
H, W = map(int, input().split())
S = [['.'] + list(input()) + ['.'] for i in range(H)]
l = [0, -1, 1]

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

for i in range(H):
    for j in range(W):
        temp = 0
        if S[i + 1][j + 1] == '.':
            for k in l:
                for kk in l:
                    if S[i + 1 + k][j + 1 + kk] == '
                        temp += 1
            S[i + 1][j + 1] = str(temp)

for i in range(H):
    print(''.join(S[i + 1][1:W + 1]))

Solution (Python):
H , W = map (int, input().split())
S = [input() for _ in range(H)]
S_prime = ['0'] * (H+2)
ans = []
for i in range(H+2):
  if i == 0 or i == H+1:
    S_prime[i] = '0' * (W+2)
  else:
    S_prime[i] = '0' + S[i-1] + '0'
def f(x, y):
  aro = [S_prime[x][y], S_prime[x][y+1], S_prime[x][y+2], S_prime[x+1][y], S_prime[x+1][y+2], S_prime[x+2][y], S_prime[x+2][y+1], S_prime[x+2][y+2]]
  t = aro.count('
  return str(t)
for i in range(H):
  X = ''
  for j in range(W):
    if S[i][j] == '
      X = X + '
    else:
      X = X + f(i,j)
  ans.append(X)
for i in range(H):
	print(ans[i])

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


def main():
    H, W = map(int, input().split())
    S = [list(input()[:-1]) for _ in range(H)]

    C = [['0']*W for _ in range(H)]
    for i in range(H):
        for j in range(W):
            if S[i][j]=='
                C[i][j] = '
                for op in itertools.product([-1, 0, +1], repeat=2):
                    ni = i + op[0]
                    nj = j + op[1]
                    if ni>=0 and ni<H and nj>=0 and nj<W and S[ni][nj]!='
                        C[ni][nj] = str(int(C[ni][nj]) + 1)
    
    for i in range(H):
        print(''.join(C[i]))


if __name__ == '__main__':
    main()

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

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

t = [['']*w for i in range(h)]


dx = [0, 0, 1, -1, 1, 1, -1, -1]
dy = [1, -1, 0, 0, 1, -1, 1, -1]
for y in range(h):
    for x in range(w):
        if s[y][x] == '
            t[y][x] = '
        else:
            count = 0
            for i in range(8):
                if x + dx[i] < 0 or x + dx[i] >= w or y + dy[i] < 0 or y + dy[i] >= h:
                    continue
                nx = x + dx[i]
                ny = y + dy[i]
                if s[ny][nx] == '
                    count += 1
            t[y][x] = str(count)

for i in range(len(t)):
    print(''.join(t[i]))

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

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

mapping = {'.':0, '

for i in range(H):
    for j in range(W):
        if Ss[i][j] == '
            ans[i][j] = '
            continue
            
        if i>0 and j>0:
            ans[i][j] += mapping[Ss[i-1][j-1]]
        if j>0:
            ans[i][j] += mapping[Ss[i+0][j-1]]
        if i<H-1 and j>0:
            ans[i][j] += mapping[Ss[i+1][j-1]]
            
        if i>0:
            ans[i][j] += mapping[Ss[i-1][j+0]]
        if i<H-1:
            ans[i][j] += mapping[Ss[i+1][j+0]]
            
        if i>0 and j<W-1:
            ans[i][j] += mapping[Ss[i-1][j+1]]
        if j<W-1:
            ans[i][j] += mapping[Ss[i+0][j+1]]
        if i<H-1 and j<W-1:
            ans[i][j] += mapping[Ss[i+1][j+1]]
        
for i in range(H):
  ans_=''
  for j in range(W):
    ans_ += str(ans[i][j])
  print(ans_)

Solution (Python):
h,w=map(int,input().split())
l=[]
ll=[[1,0],[-1,0],[0,-1],[0,1],[1,1],[-1,1],[1,-1],[-1,-1]]
ans=[[0]*w for i in range(h)]
s=set()
for i in range(h):
    tmp=input()
    l.append(tmp)
    for j in range(w):
        if tmp[j]=="
            ans[i][j]="
            for x,y in ll:
                if 0<=i+x<h and 0<=j+y<w:
                    if ans[i+x][j+y]!="
                         ans[i+x][j+y]+=1
for i in range(h):
    tmp=""
    for j in range(w):
        tmp+=str(ans[i][j])
    print(tmp)

Solution (Python):
h,w = map(int, input().split())
m = []
for y in range(h):
  s = input()
  l = []
  for i in s:
    if i == '
      l.append(1)
    else:
      l.append(0)
  m.append(l)

for y in range(h):
  s = ''
  for x in range(w):
    bomb = 0
    for i,j in ((-1,-1),(-1,0),(-1,1),(0,-1),(0,1),(1,-1),(1,0),(1,1)):
      if y+j < 0 or x+i < 0 or x+i >= w or y+j >= h:
        continue
      if m[y+j][x+i] == 1:
        bomb += 1
    if m[y][x] == 1:
      bomb = '
    s += str(bomb)
  print(s)

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

def mine(i,j):
  if i<0 or j<0 or i>=H or j>=W:
    return 0
  else:
    if S[i][j]=="
      return 1
    else:
      return 0

ij=[-1,0,1]
    
    
for i in range(H):
  for j in range(W):
    if S[i][j]==".":
      tmp=0
      for ii in ij:
        for jj in ij:
          if ii==jj and ii==0:
            1
          else:
            tmp+=mine(i+ii,j+jj)
      S[i][j]=tmp


for i in range(H):
  print("".join(map(str,S[i])))

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

for i in range(H):
    S_list[i] = ',' + S_list[i] + ','

U_list = [',' * (W + 2)]
S_newlist = U_list + S_list + U_list

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

comb_result = ''
for s in pre_result:
    comb_result += s

result = [comb_result[n: n + W] for n in range(0, len(comb_result), W)]

ans = '\n'.join(result)
print(ans)