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())
MAP = []
tx = [1,1,0,-1,-1,-1,0,1]
ty = [0,1,1,1,0,-1,-1,-1]
for _ in range(H):
    MAP.append(list(input()))
for x in range(H):
    for y in range(W):
        if MAP[x][y] == '.':
            cnt = 0
            for i in range(8):
                nx = x + tx[i]
                ny = y + ty[i]
                if 0 <= nx <= H - 1 and 0 <= ny <= W - 1 and MAP[nx][ny] == '
                    cnt += 1
            MAP[x][y] = str(cnt)
for i in range(H):
    print(''.join(MAP[i]))

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

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

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

        cnt = 0
        if w < W - 1 and s[h][w + 1] == '
            cnt += 1

        if w < W - 1 and h < H - 1 and s[h + 1][w + 1] == '
            cnt += 1

        if h < H - 1 and s[h + 1][w] == '
            cnt += 1

        if h < H - 1 and w > 0 and s[h + 1][w - 1] == '
            cnt += 1

        if w > 0 and s[h][w - 1] == '
            cnt += 1

        if w > 0 and h > 0 and s[h - 1][w - 1] == '
            cnt += 1

        if h > 0 and s[h - 1][w] == '
            cnt += 1

        if h > 0 and w < W - 1 and s[h - 1][w + 1] == '
            cnt += 1

        t += str(cnt)

    print(t)

Solution (Python):

h, w = map(int, input().split())

s = [list(input()) for _ in range(h)]
ans = [[0] * w for _  in range(h)]
for i in range(h):
    for j in range(w):
        ans[i][j] += s[i][max(j - 1, 0):min(j + 2, w)].count("
        if i != 0:
            ans[i][j] += s[i - 1][max(j - 1, 0):min(j + 2, w)].count("
        if i != h - 1:
            ans[i][j] += s[i + 1][max(j - 1, 0):min(j + 2, w)].count("

        
            

for i in range(h):
    for j in range(w):
        if s[i][j] == "
            print(s[i][j], end ="")
        else:
            print(ans[i][j], end = "")
    print()

Solution (Python):
h,w = map(int,input().split())
grid = []
grid.append(["."]*(w+2))
for i in range(h):
    a = ["."]
    s = list(input())
    a.extend(s)
    a.append(".")
    grid.append(a)
grid.append(["."]*(w+2))
for i in range(1, h+1):
    for j in range(1, w+1):
        if grid[i][j] == ".":
            ans = 0
            for k in range(i-1, i+2):
                for l in range(j-1, j+2):
                    if grid[k][l] == "
                        ans += 1
            grid[i][j] = str(ans)
for i in range(1, h+1):
    print("".join(grid[i][1:-1]))

Solution (Python):
h, w = map(int, input().split())
s = ["."*(w+2)] + ["."+input()+"." for i in range(h)] + ["."*(w+2)] 
ans = [[0]*(w+2) for i in range(h+2)]
for i in range(1, h+1):
    for j in range(1, w+1):
        if s[i][j] == "
            for x in [-1, 0, 1]:
                for y in [-1, 0, 1]:
                    if ans[i+x][j+y] != "
                        ans[i+x][j+y] += 1 
            ans[i][j] = s[i][j]
for i in range(1, h+1):
    print(*(ans[i][1:w+1]), sep="")

Solution (Python):
H,W = map(int,input().split())
S = [[0 for j in range(W)] for i in range(H)]
for i in range(H):
    S[i] = list(input())
for i in range(H):
    for j in range(W):
        if (S[i][j] == '.'):
            count = 0
            for a in range(-1,2):
                for b in range(-1,2):
                    if(0<=i+a<=H-1 and 0<=j+b<=W-1):
                        if(S[i+a][j+b] == '
                            count += 1
            S[i][j] = count
    print(*S[i], sep='')

Solution (Python):
h, w = map(int, input().split())
s = [[i for i in input()] for _ in range(h)]
count = 0
if h==1 and w==1:
    if s[0][0]=='
        print('
    else:
        print(0)
    exit(0)
if h==1:
    for j in range(w):
        if s[0][j]=='.':
            if j == 0:
                if s[0][j+1]=='
                    count += 1
                s[0][j]=str(count)
                count=0
            elif j == w-1:
                if s[0][j-1]=='
                    count += 1
                s[0][j]=str(count)
                count=0
            else:
                if s[0][j+1]=='
                    count += 1
                if s[0][j-1]=='
                    count += 1
                s[0][j]=str(count)
                count=0
    print(''.join(s[0]))
    exit(0)
if w==1:
    for i in range(h):
        if s[i][0]=='.':
            if i == 0:
                if s[i+1][0]=='
                    count += 1
                s[i]=str(count)
                count=0
            elif i == h-1:
                if s[i-1][0]=='
                    count += 1
                s[i]=str(count)
                count=0
            else:
                if s[i+1][0]=='
                    count += 1
                if s[i-1][0]=='
                    count += 1
                s[i][0]=str(count)
                count=0
    for i in range(h):
        print(''.join(s[i][0]))
    exit(0)

for i in range(h):
    for j in range(w):
        if s[i][j] == '.':
            if i == 0:
                if j == 0:
                    if s[i][j+1]=='
                        count+=1
                    if s[i+1][j]=='
                       count+=1
                    if s[i+1][j+1]=='
                        count+=1
                    s[i][j]=str(count)
                    count = 0
                elif j == w-1:
                    if s[i][j-1]=='
                        count+=1
                    if s[i+1][j]=='
                        count+=1
                    if s[i+1][j-1]=='
                        count+=1
                    s[i][j]=str(count)
                    count = 0
                else:
                    if s[i][j-1]=='
                        count+=1
                    if s[i][j+1]=='
                        count+=1
                    for k in range(3):
                        if s[i+1][j-1+k]=='
                            count+=1
                    s[i][j]=str(count)
                    count = 0
            elif i == h-1:
                if j == 0:
                    if s[i][j+1]=='
                        count+=1
                    if s[i-1][j]=='
                        count+=1
                    if s[i-1][j+1]=='
                        count+=1
                    s[i][j]=str(count)
                    count = 0
                elif j == w-1:
                    if s[i][j-1]=='
                        count+=1
                    if s[i-1][j]=='
                        count+=1
                    if s[i-1][j-1]=='
                        count+=1
                    s[i][j]=str(count)
                    count = 0
                else:
                    if s[i][j-1]=='
                        count+=1
                    if s[i][j+1]=='
                        count+=1
                    for k in range(3):
                        if s[i-1][j-1+k]=='
                            count+=1
                    s[i][j]=str(count)
                    count = 0
            else:
                if j==0:
                     if s[i+1][j]=='
                         count+=1
                     if s[i-1][j]=='
                         count+=1
                     for k in range(3):
                         if s[i-1+k][j+1]=='
                             count+=1
                     s[i][j]=str(count)
                     count = 0
                elif j==w-1:
                    if s[i+1][j]=='
                        count+=1
                    if s[i-1][j]=='
                        count+=1
                    for k in range(3):
                        if s[i-1+k][j-1]=='
                            count+=1
                    s[i][j]=str(count)
                    count = 0
                else:
                    if s[i][j-1]=='
                        count+=1
                    if s[i][j+1]=='
                        count+=1
                    for k in range(3):
                        if s[i+1][j-1+k]=='
                            count+=1
                        if s[i-1][j-1+k]=='
                            count+=1

                    s[i][j]=str(count)
                    count = 0
for i in range(h):
    print(''.join(s[i]))

Solution (Python):

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

for i in range(h):
    for j in range(w):
        
        yd = [-1, -1, 0, 1, 1, 1, 0, -1]
        xd = [0, 1, 1, 1, 0, -1, -1, -1]
        if matrix[i][j] == '.':
            bomb = 0
            for k in range(8):
                y = i + yd[k]
                x = j + xd[k]
                if 0 <= y < h and 0 <= x < w and matrix[y][x] == '
                    bomb += 1
            matrix[i][j] = str(bomb)

for r in matrix:
    print(''.join(r))

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

                            mass += 1
            S[i][j] = str(mass)
for i in range(H):
    l = "".join(S[i])
    print(l)

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

for a in range(H):
    for b in range(W):
        if L[a][b]=='.':
             num=0
             for k in range(-1,2):
                 for l in range(-1,2):
                    if 0<=a+k<=H-1 and 0<=b+l<=W-1 and L[a+k][b+l]=='
                        num+=1
             L[a][b]=num
    print(*L[a],sep='')

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

for i in range(h):
    s_l[i].insert(w,'*')
    s_l[i].insert(0,'*')

s_add = ['*']*(w+2)
s_l.insert(h,s_add)
s_l.insert(0,s_add)

for i in range(1,h+1):
    for j in range(1,w+1):
        if s_l[i][j] == '.':
            count = 0
            for k in range(i-1,i+2):
                for l in range(j-1,j+2):
                    if s_l[k][l] == '
                        count += 1
            s_l[i][j] = str(count)

s_l_result = s_l[1:h+1]
for i in range(h):
    print(''.join(s_l_result[i][1:w+1]))

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

for i in range(h):
    s_l[i].insert(w,'*')
    s_l[i].insert(0,'*')

s_add = ['*']*(w+2)
s_l.insert(h,s_add)
s_l.insert(0,s_add)

for i in range(1,h+1):
    for j in range(1,w+1):
        if s_l[i][j] == '.':
            count = 0
            for k in range(i-1,i+2):
                for l in range(j-1,j+2):
                    if s_l[k][l] == '
                        count += 1
            s_l[i][j] = str(count)

s_l_result = s_l[1:h+1]
for i in range(h):
    print(''.join(s_l_result[i][1:w+1]))

Solution (Python):
H,W = map(int,input().split())
S = [[0 for j in range(W)] for i in range(H)]
for i in range(H):
    S[i] = list(input())
for i in range(H):
    for j in range(W):
        if (S[i][j] == '.'):
            count = 0
            for a in range(-1,2):
                for b in range(-1,2):
                    if(0<=i+a<=H-1 and 0<=j+b<=W-1):
                        if(S[i+a][j+b] == '
                            count += 1
            S[i][j] = count
    print(*S[i], sep='')

Solution (Python):
h,w = map(int,input().split())
masu = [list(input()) for a in range(h)]
masu2 =  [[0 for a in range(w)] for a in range(h)]
for i in range(h):
    for j in range(w):
        a = 0
        if i > 0:
            a += masu[i-1][max(0,j-1):min(w,j+2)].count("
        a += masu[i][max(0,j-1):min(w,j+2)].count("
        if i < h - 1:
            a += masu[i+1][max(0,j-1):min(w,j+2)].count("
        if masu[i][j] =="
            masu2[i][j]="
        else:
            masu2[i][j]=str(a)
for a in masu2:
    print("".join(a))

Solution (Python):
H, W = map(int, input(). split())
S = [input() for _ in range(H)]
new_S = [[0 for _ in range(W)] for _ in range(H)]
point = []
for h in range(H):
  for w in range(W):
    if S[h][w] == '
      point.append([h,w])
      if h > 0:
        new_S[h-1][w] += 1
        if w > 0:
          new_S[h-1][w-1] += 1
        if w < W-1:
          new_S[h-1][w+1] += 1
      if h < H-1:
        new_S[h+1][w] += 1
        if w > 0:
          new_S[h+1][w-1] += 1
        if w < W-1:
          new_S[h+1][w+1] += 1
      if w > 0:
        new_S[h][w-1] += 1
      if w < W-1:
        new_S[h][w+1] += 1

for p in point:
  new_S[p[0]][p[1]] = '

for s in new_S:
  print(''.join(map(str, s)))

Solution (Python):
h,w=map(int,input().split())
masu=[list(input()) for a in range(h)]
masu2=[[0 for a in range(w)] for a in range(h)]
for i in range(h):
    for j in range(w):
        a=0
        if i>0:
            a+=masu[i-1][max(0,j-1):min(w,j+2)].count("
        a+=masu[i][max(0,j-1):min(w,j+2)].count("
        if i<h-1:
            a+=masu[i+1][max(0,j-1):min(w,j+2)].count("
        if masu[i][j]=="
            masu2[i][j]="
        else:
            masu2[i][j]=str(a)
for a in masu2:
    print("".join(a))

Solution (Python):
def check_current_posi(s_i_list, h, w):
  _height = len(s_i_list)
  _width = len(s_i_list[0])
  if h == -1 or w == -1 or h >= _height or w >= _width:
    return False

  else:
    if s_i_list[h][w] == "
      
      return True

    else:
      
      return False


def check_around_bomb_num(s_i_list, h, w):

  check_posi_h = h - 1
  check_posi_w = w - 1

  bomb_counter = 0

  for i in range(3):

    for j in range(3):

      if check_current_posi(s_i_list, check_posi_h, check_posi_w):
        bomb_counter += 1

      check_posi_w += 1

    check_posi_h += 1
    check_posi_w = w - 1
  
  return bomb_counter
      


_input = list(map(int, input().split(" ")))

height = _input[0]
width = _input[1]

s_i_list = []

for i in range(height):
  _input_row = list(input())
  s_i_list.append(_input_row)



for row in range(height):
  for column in range(width):
    if s_i_list[row][column] == "
      continue

    s_i_list[row][column] = check_around_bomb_num(s_i_list, row, column)

for row in s_i_list:
  output_row = "".join(list(map(str,row)))
  print(output_row)

Solution (Python):
h, w = [int(x) for x in input().split()]
S = ['+' * (w + 2)]



def func(i, j):
    cnt = S[i-1][j-1:j+2].count('
    cnt += S[i+1][j-1:j+2].count('
    cnt += S[i][j-1:j+2:2].count('
    
    return str(cnt)

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

for i in range(1,h+1):
    for j in range(1,w+1):
        if S[i][j] == '.':
            S[i][j] = func(i,j)

for s in S[1:h+1]:
    print(''.join(s[1:w+1]))

Solution (Python):
import numpy as np
 
H, W = map(int, input().split())
S = np.array([list(input()) for _ in range(H)])
 
for i in range(H):
  for j in range(W):
    if S[i, j] == '.':
      S[i, j] = np.sum(S[max(i-1, 0):i+2, max(j-1, 0):j+2] == '
for s in S:
  print(''.join(s))

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


arr = [[0 for c in range(W)] for r in range(H)]


for r in range(H):
    Sh=input()
    for c in range(W):
        arr[r][c]=Sh[c]


for r in range(H):
    for c in range(W):
        if arr[r][c]=='.':
            
            
            counter=0 
            
            
            if r-1>=0:
                
                
                if c-1>=0:
                    if arr[r-1][c-1]=='
                        counter+=1
                
                if arr[r-1][c]=='
                    counter+=1
                
                
                if c+1<=W-1:
                    if arr[r-1][c+1]=='
                        counter+=1
            
            
            
            if c-1>=0:
                if arr[r][c-1]=='
                    counter+=1
            
            
            if c+1<=W-1:
                if arr[r][c+1]=='
                    counter+=1
            
            
            if r+1<=H-1:
                
                
                if c-1>=0:
                    if arr[r+1][c-1]=='
                        counter+=1
                
                if arr[r+1][c]=='
                    counter+=1
                
                
                if c+1<=W-1:
                    if arr[r+1][c+1]=='
                        counter+=1
            
            arr[r][c]=counter


for r in range(H):
    for c in range(W):
        print(arr[r][c],end='')
    print()

Solution (Python):
def main():
    H, W = map(int, input().split())
    s = [input() for _ in range(H)]
    ctr = [[0] * W for _ in range(H)]
    for r, row in enumerate(s):
        for c, cell in enumerate(row):
            if cell == '
                for dr in (-1, 0, 1):
                    for dc in (-1, 0, 1):
                        if 0 <= r + dr < H and 0 <= c + dc < W and s[r + dr][c + dc] == '.':
                            ctr[r + dr][c + dc] += 1
    for r in range(H):
        for c in range(W):
            print('
        print()


if __name__ == '__main__':
    main()

Solution (Python):
h,w = map(int,input().split(' '))
S = []
i = 0
while i < h:
    S.append(input())
    i += 1
ans = ''
p = 0
while p < h:
    q = 0
    while q < w:
        s0 = S[p][q]
        if s0 == '
            ans += '
        else:
            count = 0
            if p-1 >= 0:
                if S[p-1][q] == '
                    count += 1
                if q-1 >= 0 and S[p-1][q-1] == '
                    count += 1
                if q+1 < w and S[p-1][q+1] == '
                    count += 1
            if q-1 >= 0 and S[p][q-1] == '
                count += 1
            if q+1 < w and S[p][q+1] == '
                count += 1
            if p+1 < h:
                if S[p+1][q] == '
                    count += 1
                if q-1 >= 0 and S[p+1][q-1] == '
                    count += 1
                if q+1 < w and S[p+1][q+1] == '
                    count += 1
            ans += str(count)
        q += 1
    ans += '\n'
    p += 1
print(ans)

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

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

for i in range(1,a+1):
    for j in range(1,b+1):
        cnt = 0
        if bo[i][j] == "
            continue
        for k in range(8):
            ch = bo[i-dx[k]][j-dy[k]]
            if ch == "
                cnt += 1
        bo[i][j] = str(cnt)

for i in range(1,a+1):
    print("".join(bo[i][1:-1]))

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

ans = board
dist = [(1, 1), (1, 0), (1, -1), (0, -1), (-1, -1), (-1, 0), (-1, 1), (0, 1)]

for i in range(H):
    for j in range(W):
        if board[i][j] == '
            continue
        count = 0
        for x, y in dist:
            now_x = j + x
            now_y = i + y
            if now_x >= W or now_x < 0:
                continue
            if now_y >= H or now_y < 0:
                continue
            if board[now_y][now_x] == '
                count += 1
        ans[i][j] = str(count)

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

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

def main():
    H, W = map(int, input().split())
    grid = [list(input().strip("\n")) for _ in range(H)]
    ans = [[0] * W for _ in range(H)]
    for h in range(H):
        for w in range(W):
            if grid[h][w] == "
                ans[h][w] = "
                continue
            count = 0
            if h > 0:
                if w > 0 and grid[h-1][w-1] == "
                    count += 1
                if grid[h-1][w] == "
                    count += 1
                if w < W - 1 and grid[h-1][w+1] == "
                    count += 1
            if w > 0 and grid[h][w-1] == "
                count += 1
            if w < W - 1 and grid[h][w+1] == "
                count += 1
            if h < H - 1:
                if w > 0 and grid[h+1][w-1] == "
                    count += 1
                if grid[h+1][w] == "
                    count += 1
                if w < W - 1 and grid[h+1][w+1] == "
                    count += 1
            ans[h][w] = count

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

if __name__ == '__main__':
    main()

Solution (Python):
import numpy as np
H,W = map(int,input().split())

S = [list(input()) for _ in range(H)]
point_list = np.array([['
dy = [0,0,1,1,1,-1,-1,-1]
dx = [1,-1,0,1,-1,0,1,-1]

for h in range(H):
  for w in range(W):
    count = 0
    for i in range(8):
      y = h+dy[i]
      x = w+dx[i]
      if y < 0 or y >= H or x < 0 or x >= W:
        continue
      if S[h][w] == '.' and S[y][x] == '
        count += 1
    if S[h][w] == '.':
      point_list[h][w] = count

for j in range(H):
  print(''.join(point_list[j]))

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

ans = board
dist = [(1,1), (1,0),(1,-1),(0,-1),(-1,-1),(-1,0),(-1,1),(0,1) ]
for i in range(H):
    for j in range(W):
        if board[i][j] == '
            continue
        count = 0
        for x, y in dist:
            now_x = j + x
            now_y = i + y
            if now_x >= W or now_x < 0:
                continue
            if now_y >= H or now_y < 0:
                continue
            if board[now_y][now_x] == '
                count += 1
        ans[i][j] = str(count)


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

Solution (Python):
import numpy as np
from collections import defaultdict
 
H, W = map(int, input().split())
S = np.array([list(input()) for _ in range(H)])
 
for i in range(H):
  for j in range(W):
    if S[i, j] == '.':
      d = defaultdict(int)
      S[i, j] = np.sum(S[max(i-1, 0):i+2, max(j-1, 0):j+2] == '
for s in S:
  print(''.join(s))

Solution (Python):
icase=0
if icase==0:
    s=[]
    h,w=map(int,input().split())
    t=[["
    s.append(["."]*(w+2))
    for i in range(h):
        si=list("."+input()+".")
        s.append(si)
    s.append(["."]*(w+2))
    for i in range(h):
        for j in range(w):
            if s[i+1][j+1]==".":
                scnt=0
                for jj in range(j,j+3):
                    if s[i][jj]=="
                        scnt=scnt+1
                    if s[i+2][jj]=="
                        scnt=scnt+1
                if s[i+1][j]=="
                    scnt=scnt+1
                if s[i+1][j+2]=="
                    scnt=scnt+1
                t[i][j]=str(scnt)
    for i in range(h):
        si=""
        for j in range(w):                    
            si=si+t[i][j]
        print(si)

Solution (Python):



H, W, *S = open(0).read().split()
H, W = int(H), int(W)
S = [tuple(s) for s in S]

ans = [[0]*W for _ in [0]*H]
dirs = [(-1, -1), (-1, 0), (-1, 1), (0, -1), (0, 1), (1, -1), (1, 0), (1, 1)]

for y, _s in enumerate(S):
    for x, s in enumerate(_s):
        if s == '
            ans[y][x] = '
            for dy, dx in dirs:
                if 0 <= x + dx < W and 0 <= y + dy < H and S[y+dy][x+dx] != "
                    ans[y+dy][x+dx] += 1

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

Solution (Python):
import sys
import numpy as np 

h, w = map(int, sys.stdin.readline().split())
grid = np.array([list(sys.stdin.readline().rstrip()) for _ in range(h)], dtype='U1')
grid = np.pad(grid, 1, 'constant')

def main():
  d = [(-1, 0), (0, -1), (1, 0), (0, 1)]
  cnt = np.zeros_like(grid, dtype=np.int8)
  for i in range(1, h + 1):
    for j in range(1, w + 1):
      if grid[i][j] == '.': continue
      for dy, dx in [(dy, dx) for dy in range(-1, 2) for dx in range(-1, 2)]:
        cnt[i+dy][j+dx] += 1
  cnt = cnt.astype('U1')
  cnt[grid == '
  cnt = cnt[1:-1, 1:-1]
  for i in range(h):
    print(''.join(cnt[i]))
    
if __name__ == '__main__':
  main()

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

if h==1 and w==1:
  if s[0]=="
    print("
  else:
    print(0)
elif h==1 and w>1:
  for i in range(w):
    t=0
    if i==0:
      if s[0][i+1]=="
    elif 0<i<w-1:
      if s[0][i+1]=="
      if s[0][i-1]=="
    elif i==w-1:
      if s[0][i-1]=="
    if s[0][i]=="
      a[0][i]="
    else:
      a[0][i]=t
  tt=""
  for aa in a:
    for aaa in aa:
      tt+=str(aaa)
  print(str(tt))

elif w==1 and h>1:
  for i in range(h):
    t=0
    if i==0:
      if s[i+1]=="
    elif 0<i<h-1:
      if s[i+1]=="
      if s[i-1]=="
    elif i==h-1:
      if s[i-1]=="
    if s[i]=="
      a[i][0]="
    else:
      a[i][0]=t
  for aa in a:
    print(*aa)
elif w>1 and h>1:
  for i in range(h):
    for j in range(w):
      t=0
      if i==0:
        if j==0:
          if s[i][j+1]=="
          if s[i+1][j+1]=="
          if s[i+1][j]=="
        elif 0<j<w-1:
          if s[i][j+1]=="
          if s[i+1][j+1]=="
          if s[i+1][j]=="
          if s[i+1][j-1]=="
          if s[i][j-1]=="
        elif j==w-1:
          if s[i+1][j]=="
          if s[i+1][j-1]=="
          if s[i][j-1]=="
      elif 0<i<h-1:
        if j==0:
          if s[i-1][j]=="
          if s[i-1][j+1]=="
          if s[i][j+1]=="
          if s[i+1][j+1]=="
          if s[i+1][j]=="
        elif 0<j<w-1:
          if s[i][j+1]=="
          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-1][j-1]=="
          if s[i-1][j]=="
          if s[i-1][j+1]=="
        elif j==w-1:
          if s[i+1][j]=="
          if s[i+1][j-1]=="
          if s[i][j-1]=="
          if s[i-1][j-1]=="
          if s[i-1][j]=="
      elif i==h-1:
        if j==0:
          if s[i][j+1]=="
          if s[i-1][j+1]=="
          if s[i-1][j]=="
        elif 0<j<w-1:
          if s[i][j+1]=="
          if s[i-1][j+1]=="
          if s[i-1][j]=="
          if s[i-1][j-1]=="
          if s[i][j-1]=="
        elif j==w-1:
          if s[i-1][j]=="
          if s[i-1][j-1]=="
          if s[i][j-1]=="
      if s[i][j]=="
        a[i][j]="
      else:
        a[i][j]=t
  for aa in a:
    tt=""
    for aaa in aa:
      tt+=str(aaa)
    print(tt)

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

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

def numbering(x, y):
    if field[y][x] == ".":
        c = 0
        for i in range(8):
            nx = x + dx[i]
            ny = y + dy[i]
            if 0 <= nx and nx < w and 0 <= ny and ny < h:
                if field[ny][nx] == "
                    c += 1
        field[y][x] = str(c)

for X in range(w):
    for Y in range(h):
        numbering(X, Y)

for Y in range(h):
    for X in range(w):
        print(field[Y][X], end = "")
    print()

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

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

for i in range(h):
    for j in range(w):
        c = 0
        if s[i][j] == '
            g[i].append('
            continue
        for n, m in ([-1,1],[-1,0],[-1,-1],[0,1],[0,-1],[1,1],[1,0],[1,-1]):
            new_h, new_w = i + n, j + m
            if new_h < 0 or new_h >= h or new_w < 0 or new_w >= w:
                continue
            if s[new_h][new_w] == '
                c += 1
        g[i].append(str(c))

ans = ''
for i in range(h):
    c = ''.join(g[i])
    ans += c
    if i != h-1:
        ans += '\n'

print(ans)

Solution (Python):
import pprint
h,w = map(int,input().split())
s = [["!"]*(w+2)] + [list("!" + input() + "!") for _ in range(h)] + [["!"]*(w+2)]
next = [[-1,-1],[-1,0],[-1,1],[0,-1],[0,1],[1,-1],[1,0],[1,1]]
for i in range(1,h+1):
    for j in range(1,w+1):
        if s[i][j] == "
            continue
        count = 0
        for d1,d2 in next:
            count += (s[i + d1][j+d2] == "
        s[i][j] = str(count)
ans = []
for k in s:
    k = [x for x in k if not x =="!"]
    if k:
        ans.append(k)
for adf in ans:
    print("".join(adf))

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

area = []
d = []

h += 2
w += 2

for i in range(h):
    if i == 0 or i == h-1:
        array = '.' * w
        area.append(array)
    else:
        array = input()
        array = '.' + array + '.'
        area.append(array)


def check(i, j):
    cnt = 0
    for x in range(i-1, i+2):
        for y in range(j-1, j+2):
            if area[x][y] == '
                cnt += 1
    return cnt

for i in range(1, h-1):
    for j in range(1, w-1):
        if area[i][j] == '.':
            d.append(check(i, j))
        else:
            d.append('
a = 0
for i in d:
    a += 1
    print(i, end='')
    if a == w-2:
        print()
        a = 0

Solution (Python):
h,w=map(int,input().split())
maze=[[None] for _ in range(h)]
for i in range(h):
  s=list(input())
  for j in range(w):
    if s[j]=="
      pass
    else:
      s[j]=0
  maze[i]=s

for i in range(h):
  for j in range(w):
    if maze[i][j]=="
      for ii in range(i-1,i+1+1):
        for jj in range(j-1,j+1+1):
          if 0<=ii<h and 0<=jj<w:
            if maze[ii][jj]!="
              maze[ii][jj]+=1
for i in range(h):
  for j in range(w):
    print(maze[i][j],end="")
  print()

Solution (Python):
h,w=map(int,input().split())
maze=[[None] for _ in range(h)]
for i in range(h):
  s=list(input())
  for j in range(w):
    if s[j]=="
      pass
    else:
      s[j]=0
  maze[i]=s

for i in range(h):
  for j in range(w):
    if maze[i][j]=="
      for ii in range(i-1,i+1+1):
        for jj in range(j-1,j+1+1):
          if 0<=ii<h and 0<=jj<w:
            if maze[ii][jj]!="
              maze[ii][jj]+=1
for i in range(h):
  for j in range(w):
    print(maze[i][j],end="")
  print()