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):
import sys
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
read = sys.stdin.buffer.read
sys.setrecursionlimit(10 ** 7)
INF = float('inf')

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

directions = [
    (-1, 1),(0, 1),(1, 1),
    (-1, 0),(1, 0),
    (-1, -1),(0, -1),(1, -1)
    ]

new_masu = []
for h in range(H):
    tmp = []
    for w in range(W):
        if masu[h][w] == "
            tmp.append("
            continue

        bombs = 0
        
        for direction in directions:
            x = w + direction[0]
            y = h + direction[1]
            if 0 <= x < W and 0 <= y < H:
                try:
                    if masu[y][x] == "
                        bombs += 1
                except Exception:
                    pass
        tmp.append(bombs)
    new_masu.append(tmp)

for line in new_masu:
    
    print(*line, sep="")

Solution (Python):
import sys

input = sys.stdin.readline


def count(S, i, j):
    res = 0
    for x, y in [(0, -1), (1, -1), (1, 0), (1, 1), (0, 1), (-1, 1), (-1, 0), (-1, -1)]:
        if S[i + y][j + x] == "
            res += 1
    return res


def main():
    H, W = map(int, input().split())
    S = [None] * (H + 2)
    S[0] = S[-1] = "." * (W + 2)
    for i in range(1, H + 1):
        S[i] = "".join([".", input().rstrip(), "."])

    ans = [[None] * W for _ in range(H)]
    for i in range(H):
        for j in range(W):
            if S[i + 1][j + 1] == "
                ans[i][j] = "
            else:
                ans[i][j] = count(S, i + 1, j + 1)

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


if __name__ == "__main__":
    main()

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

for i in range(H):
  for j in range(W):
    ans  = 0
    if S[i][j] == "
      continue
    else:
      if j != 0 and S[i][j-1] =="
        ans += 1
      if j != 0 and i != 0 and S[i-1][j-1] == "
          ans += 1
      if j != 0 and i != H-1 and S[i+1][j-1] == "
          ans += 1
      if j != W-1 and S[i][j+1] =="
        ans += 1
      if j != W-1 and i != 0 and S[i-1][j+1] == "
          ans += 1
      if j != W-1 and i != H-1 and S[i+1][j+1] == "
          ans += 1
      if i != 0 and S[i-1][j] == "
        ans += 1
      if i != H-1 and S[i+1][j] == "
        ans += 1
      S[i][j] = str(ans)
      ans = 0 
for i in range(H):
	print("".join(S[i]))

Solution (Python):

import collections
import itertools as it
import math

 


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

masu = [list(input()) for i in range(h)]




dumy = ['.'] * w
masu.append(dumy)
masu.insert(0, dumy)


for i in range(h+2):
    masu[i] = ['.'] + masu[i] + ['.']

nbomb = [[0] * (w + 2) for _ in range(h+2)]

for i, line in enumerate(masu[1:-1]):
    for j, m in enumerate(line[1:-1]):
        if m == '
            i_ = i + 1
            j_ = j + 1
            nbomb[i_-1][j_-1] += 1
            nbomb[i_-1][j_-0] += 1
            nbomb[i_-1][j_+1] += 1
            nbomb[i_-0][j_-1] += 1
            nbomb[i_-0][j_+1] += 1
            nbomb[i_+1][j_-1] += 1
            nbomb[i_+1][j_-0] += 1
            nbomb[i_+1][j_+1] += 1

for ml, bl in zip(masu[1:-1], nbomb[1:-1]):
    ml = ml[1:-1]
    bl = bl[1:-1]

    for m_, b_ in zip(ml, bl):
        if m_ == '.':
            print(b_, end='')
        else:
            print('
    print()

Solution (Python):


H, W = map(int, input().split())
S = [list('.'*(W+2))]

for i in range(H):
    S.append(list('.' + input() + '.'))

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

A = []


for i in range(1, H+1):
    AA = []
    for j in range(1, W+1):
        cnt = 0
        if S[i][j] == '
            AA.append('
            continue
        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] == '
        AA.append(cnt)
    A.append(AA)

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

Solution (Python):
H, W = map(int, input().split())
S = [input() for i in range(H)]
B = []
B.append('.' * (W + 2))
for i in range(H):
    B.append('.' + S[i] + '.')
B.append('.' * (W + 2))
for i in range(1, H + 1):
    for j in range(1, W + 1):
        if B[i][j] == '
            print('
            continue
        c = 0
        if B[i - 1][j - 1] == '
            c += 1
        if B[i - 1][j + 1] == '
            c += 1
        if B[i + 1][j - 1] == '
            c += 1
        if B[i + 1][j + 1] == '
            c += 1
        if B[i][j - 1] == '
            c += 1
        if B[i][j + 1] == '
            c += 1
        if B[i + 1][j] == '
            c += 1
        if B[i - 1][j] == '
            c += 1
        print(c, end='')
    print('')

Solution (Python):
import numpy as np


H, W = map(int, input().split())
S = []
for i in range(H):
    S.append(list(map(str, input().rstrip())))


li = []
for i in range(H+2):
    li.append(["."]*(W+2))


for i in range(1, H+1):
    li[i][1:-1] = S[i-1]


ans = []
for i in range(H+2):
    ans.append([0]*(W+2))
ans = np.array(ans)


for h in range(1, H+1):
    for w in range(1, W+1):
        if li[h][w]=="
            ans[h-1:h+2, w-1:w+2] += 1


ans = ans.astype("str")
ans = ans[1:-1, 1:-1]


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

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


str_list.append(["."]*w)
str_list.insert(0,["."]*w)
for i in range(h+2):
    str_list[i].append(".")
    str_list[i].insert(0,".")

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

baku = 0

for i in range(h):
    for j in range(w):
        if str_list[i+1][j+1] == "
            ans[i][j] = "
            continue
        
        baku = 0
        for a in range(3):
            for b in range(3):

                if a == 1 and b==1:
                    continue
                if str_list[i+a][j+b]=="
                    baku += 1
        
        ans[i][j] = baku
            

for i in range(h):
    a = ""
    a = str(a)
    for j in ans[i]:
        a += str(j)

    print(a)

Solution (Python):
H, W= [int(x) for x in input().split()]
s_list = []
for h in range(H):
    s_list.append(input())
s_list.insert(0, "."*W)
s_list.append("."*W)

ans = []

for h in range(1, H+1):
    for w in range(W):
        count = 0
        if s_list[h][w]=="
            count = "
        else:   
            if w!=W-1:
                if s_list[h-1][w+1]=="
                    count += 1
                if s_list[h][w+1]=="
                    count += 1
                if s_list[h+1][w+1]=="
                    count += 1
            if w!=0:
                if s_list[h-1][w-1]=="
                    count += 1
                if s_list[h][w-1]=="
                    count += 1
                if s_list[h+1][w-1]=="
                    count += 1
            
            if s_list[h+1][w]=="
                    count += 1
            if s_list[h-1][w]=="
                count += 1
        
        ans.append(count)

for n in range(H):
    for a in ans[n*W:n*W+W]:
        print(a, end='')
    print()

Solution (Python):
def main():
    for x in range(H):
        for y in range(W):
            if S[x][y] == ".":
                tmp = 0
                if x > 0 and S[x - 1][y] == "
                    tmp = tmp + 1
                if x < H - 1 and S[x + 1][y] == "
                    tmp = tmp + 1
                if y > 0 and S[x][y - 1] == "
                    tmp = tmp + 1
                if y < W - 1 and S[x][y + 1] == "
                    tmp = tmp + 1
                if x > 0 and y > 0 and S[x - 1][y - 1] == "
                    tmp = tmp + 1
                if x > 0 and y < W - 1 and S[x - 1][y + 1] == "
                    tmp = tmp + 1
                if x < H - 1 and y > 0 and S[x + 1][y - 1] == "
                    tmp = tmp + 1
                if x < H - 1 and y < W - 1 and S[x + 1][y + 1] == "
                    tmp = tmp + 1
                S[x][y] = str(tmp)

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


if __name__ == "__main__":
    H, W = map(int, input().split())
    S = [list(input()) for _ in range(H)]
    main()

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

for x in range(H):
    for y in range(W):
        if S[x][y] == ".":
            tmp = 0
            if x > 0 and S[x - 1][y] == "
                tmp = tmp + 1
            if x < H - 1 and S[x + 1][y] == "
                tmp = tmp + 1
            if y > 0 and S[x][y - 1] == "
                tmp = tmp + 1
            if y < W - 1 and S[x][y + 1] == "
                tmp = tmp + 1
            if x > 0 and y > 0 and S[x - 1][y - 1] == "
                tmp = tmp + 1
            if x > 0 and y < W - 1 and S[x - 1][y + 1] == "
                tmp = tmp + 1
            if x < H - 1 and y > 0 and S[x + 1][y - 1] == "
                tmp = tmp + 1
            if x < H - 1 and y < W - 1 and S[x + 1][y + 1] == "
                tmp = tmp + 1
            S[x][y] = str(tmp)

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

Solution (Python):

import sys
sys.setrecursionlimit(10**9)
INF=10**18
MOD=10**9+7
input=lambda: sys.stdin.readline().rstrip()
YesNo=lambda b: bool([print('Yes')] if b else print('No'))
YESNO=lambda b: bool([print('YES')] if b else print('NO'))
int1=lambda x:int(x)-1

def main():
    H,W=map(int,input().split())
    S=[[] for _ in range(H+2)]
    S[0]=S[H+1]=['.']*(W+2)
    for i in range(1,H+1):
        S[i]=['.']+list(input())+['.']
    ans=[['']*W for _ in range(H)]
    for i in range(1,H+1):
        for j in range(1,W+1):
            if S[i][j]=='
                ans[i-1][j-1]='
            else:
                c=0
                for dy in range(-1,2):
                    for dx in range(-1,2):
                        if dy==dx==0:
                            continue
                        c+=(S[i+dy][j+dx]=='
                ans[i-1][j-1]=str(c)
    for row in ans:
        print(''.join(row))

if __name__ == '__main__':
    main()

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] == '.':
            l = max(0, j - 1)
            r = min(j + 2, w)
            if i == 0:
                if h == 1:
                    s[i][j] = s[i][l:r].count('
                else:
                    s[i][j] = s[i][l:r].count('
            elif i == h - 1:
                s[i][j] = s[i - 1][l:r].count('
            else:
                s[i][j] = s[i - 1][l:r].count('
    print(*s[i], sep='')

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

def check(i,j):
    cnt = 0
    for i2 in [-1,0,1]:
        for j2 in [-1,0,1]:
            if (i2!=0 or j2!=0) and 0<=i+i2<h and 0<=j+j2<w:
                if S[i+i2][j+j2]=='
                    cnt+=1
    return cnt

for i in range(h):
    for j in range(w):
        if S[i][j]=='.':
            B[i][j]=str(check(i,j))
        else:
            B[i][j]='
[print(''.join(x)) for x in B]

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

a = []  

for i in range(H):
    a.append(input())

res  =  [ [0]*W for i in range(H)]

mx = [-1, -1, -1, 1, 1, 1, 0, 0]  
my = [-1, 0, 1, -1, 0, 1, -1, 1]  

for h in range(H):
    for w in range(W):
        cnt = 0  
        if a[h][w] == '
            res[h][w] = '
        else:
            for i in range(len(mx)):  
                    x = h + mx[i]
                    y = w + my[i]
                    if (0 <= x <= H-1) and (0 <= y <= W-1):
                        if a[x][y] == '
                            cnt += 1
            res[h][w] = cnt


for i in range(H):
    for j in range(W):
        print("{}".format(res[i][j]),end="")
    print()

Solution (Python):



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







for i in range(h):
    array = list(input())   
        
        

    for j in range(w):
        s[i][j] = array[j]
        

dxdy = [[1,1],[1,0],[1,-1],[0,1],[0,-1],[-1,1],[-1,0],[-1,-1]]


for i in range(h):
    for j in range(w):
        count = 0
        if s[i][j] == '.':
            for k in range(8):
                dx = dxdy[k][0]
                dy = dxdy[k][1]
                xx = i + dx
                yy = j + dy

                if xx <0 :
                    continue
                if xx > h-1:
                    continue
                if yy <0 :
                    continue
                if yy > w-1:
                    continue

                if s[xx][yy] == '
                    count += 1

            s[i][j] = count

            

for i in range(h):
    text = ''
    for j in range(w):
        text += str(s[i][j])
    print(text)

Solution (Python):
H, W = map(int,input().split())
Sq = []
Haround = []
Slast = []
for i in range(W+2) :
    Haround.append('0')

Sq.append(Haround)

for i in range(H) : 
    l = list(str(input()))
    S = ['0']
    for j in range(W) :
            S.append(l[j])
    S.append('0')
    Sq.append(S)
Sq.append(Haround)

around = [[0,1],[1,1],[1,0],[1,-1],[0,-1],[-1,-1],[-1,0],[-1,1]]
for i in range(H+2) : 
    for j in range(W+2) : 
        c = 0
        if Sq[i][j] == '.' :
            for k in range(8) :
                K = around[k]
                if Sq[i + K[0]][j + K[1]] == '
                    c += 1
            Sq[i][j] = str(c)

for i in range(1,H+1) :
    Sq[i].pop(0)
    Sq[i].pop(W)
    ans = ''.join(Sq[i])
    print(ans)

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

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

for i in range(h):
    for j in range(w):
        if s[i][j]=="
            continue

        count=0
        for k in range(8):
            di=i+dx[k]
            dj=j+dy[k]

            if (di<0)|(di>h-1):
                continue
            if (dj<0)|(dj>w-1):
                continue

            if s[di][dj]=="
                count+=1

        s[i][j]=str(count)

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

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

for i in range(h):
    for j in range(w):
        if ans[i][j] !="
            count = 0
            for p in range(max(0,i-1), min(i+2, h)):
                for q in range(max(0,j-1), min(j+2, w)):
                    if ans[p][q] == "
                        count += 1
            ans[i][j] = str(count)

for l in ans:
    for c in l:
        print(c, end="")
    print()

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

field = [[0 if x == '.' else 1 for x in xx] for xx in s]
result = [[''] * w for _ in range(h)]

for i in range(h):
    for j  in range(w):
        if field[i][j] == 1:
            result[i][j] = '
            continue
        
        total = 0
        for ii in range(i-1, i+2):
            for jj in range(j-1, j+2):
                if ii < 0 or ii >= h:
                    continue
                if jj < 0 or jj >= w:
                    continue
                total += field[ii][jj]
        result[i][j] = str(total)

for r_line in result:
    print("".join(r_line))

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

search = [[-1,-1],[-1,0],[-1,1],[0,-1],[0,1],[1,-1],[1,0],[1,1]]

for i in range(h):
    for j in range(w):
        if l[i][j] == '.':
            tmp = 0
            for k in range(len(search)):
                if 0 <= i + search[k][0] <= h-1 and 0 <= j + search[k][1] <= w-1:
                    if l[i + search[k][0]][j + search[k][1]] == '
                        tmp += 1
            l[i][j] = str(tmp)

for k in range(h):
    print(''.join(l[k]))

Solution (Python):
h,w = map(int,input().split())
s_list = [list(input()) for nesya in range(h)]
k = 0
for s in s_list:
  m = 0
  for a in s:
    if a == '
      pass
    else:
      c = []
      if m != 0:
        c.append(s_list[k][m-1])
        if k != 0:
          c.append(s_list[k-1][m-1])
        if k != h-1:
          c.append(s_list[k+1][m-1])
      if m != w-1:
        c.append(s_list[k][m+1])
        if k != 0:
          c.append(s_list[k-1][m+1])
        if k != h-1:
          c.append(s_list[k+1][m+1])
      if k != 0:
        c.append(s_list[k-1][m])
      if k != h-1:
        c.append(s_list[k+1][m])
      hoge = c.count('
      s_list[k][m] = str(hoge)
    m += 1
  k += 1
for ans in s_list:
  print(''.join(ans))

Solution (Python):
import collections
import math

H, W = map(int, input().split())
bomb_dict = collections.defaultdict(bool)
count_dict = collections.defaultdict(int)
for i in range(H):
    S = input()
    for j in range(len(S)):
        if S[j] == '
            bomb_dict[i, j] = True
            count_dict[i - 1, j - 1] += 1
            count_dict[i - 1, j] += 1
            count_dict[i - 1, j + 1] += 1

            count_dict[i, j - 1] += 1
            count_dict[i, j + 1] += 1

            count_dict[i + 1, j - 1] += 1
            count_dict[i + 1, j] += 1
            count_dict[i + 1, j + 1] += 1

for i in range(H):
    for j in range(W):
        if bomb_dict[i, j]:
            print('
        else:
            print(count_dict[i, j], end='')

    print()

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

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

        if S[y][x] == "
            continue

        cnt = 0

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

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

            if S[ny][nx] == "
                cnt += 1

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

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

Solution (Python):
H,W = map(int,input().split())
S = [input() for _ in range(H)]
answer = [[0 if cell == '.' else '
 
import itertools
for i in range(H):
    for j in range(W):
        if S[i][j] != '
            continue
        for dx,dy in itertools.product([-1,0,1],repeat=2):
            x = i+dx
            y = j+dy
            if x < 0 or x >= H:
                continue
            if y < 0 or y >= W:
                continue
            if S[x][y] == '
                continue
            answer[x][y] += 1
            
for row in answer:
    print(*row,sep='')

Solution (Python):






H, W = map(int, input().split())
S = [list("_"*(W+2))]
for _ in range(H):
    S.append(list("_"+input()+"_"))
S.append(list("_"*(W+2)))





import copy





ans = copy.deepcopy(S)

for h in range(len(S)):


    for w in range(len(S[h])):

        if S[h][w] == ".":
            s = S[h-1][w-1:w+2] + S[h][w-1:w+2] + S[h+1][w-1:w+2]

            ans[h][w] = str(s.count("

for h in range(H):
    row = "".join(ans[h+1])
    print(row[1:-1])

Solution (Python):
h, w = map(int, input().split())
s = [input() for _ in range(h)]
ans = [[0 for _ in range(w)] for _ in range(h)]
for i in range(h):
    for j in range(w):
        if s[i][j] == '
            ans[i][j] = '
        else:
            left = max(0, j-1)
            right = min(w-1, j+1)
            top = max(0, i-1)
            bottom = min(h-1, i+1)
            for k in range(top, bottom+1):
                ans[i][j] += s[k][left:right+1].count('
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 = [input() for _ in [0]*h]
cnt = [[0]*w for _ in [0]*h]
for i in range(h):
    for j in range(w):
        if i>0:
            if s[i-1][j]=='
            if j>0:
                if s[i-1][j-1]=='
            if j<w-1:
                if s[i-1][j+1]=='
        if i<h-1:
            if s[i+1][j]=='
            if j>0:
                if s[i+1][j-1]=='
            if j<w-1:
                if s[i+1][j+1]=='
        if j>0:
            if s[i][j-1]=='
        if j<w-1:
            if s[i][j+1]=='
for i in range(h):
    for j in range(w):
        if s[i][j]=='
            print('
        else:
            print(cnt[i][j], end='')
    print()

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

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

for h in range(H):
  for w in range(W):
    if S[h][w] == "
      continue
    cnt = 0
    for i in range(8):
      y = h + dy[i]
      x = w + dx[i]
      if 0 <= y < H and 0 <= x < W:
        if S[y][x] == "
          cnt += 1
    S[h][w] = str(cnt)
for s in S:
  print("".join(s))

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

for i in range(H):
    for j in range(W):
        if S[i][j] == '
            for k in range(3):
                for l in range(3):
                    A[i + k][j + l] += 1

for i in range(H):
    for j in range(W):
        if S[i][j] == '
            A[i + 1][j + 1] = '

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

Solution (Python):



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



for i in range(1,h+1):   
    array = list(input())   
        
        
        

    for j in range(1,w+1):
        s[i][j] = array[j-1]    



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

            s[i][j] = count

            

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

Solution (Python):
h,w = map(int,input().split())
grid = [['.']*(w+2)] + [['.'] + [s for s in input()] + ['.'] for i in range(h)] + [['.']*(w+2)]

ans = [[0]*w for i in range(h)]
for i in range(1,h+1):
    for j in range(1,w+1):
        if grid[i][j] == '.':
            for dx in range(-1,2):
                for dy in range(-1,2):
                    if grid[i+dx][j+dy] == '
                        ans[i-1][j-1] += 1
        else:
            ans[i-1][j-1] = '

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

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

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

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] == '.':
            num = 0
            if i != 0 and s[i-1][j] == '
                num += 1
            if j != 0 and s[i][j-1] == '
                num += 1
            if i != h-1 and s[i+1][j] == '
                num += 1
            if j != w-1 and s[i][j+1] == '
                num += 1
            if i != 0 and j != 0 and s[i-1][j-1] == '
                num += 1
            if i != h-1 and j != w-1 and s[i+1][j+1] == '
                num += 1
            if i != h-1 and j != 0 and s[i+1][j-1] == '
                num += 1
            if i != 0 and j != w-1 and s[i-1][j+1] == '
                num += 1

            s[i][j] = str(num)

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

Solution (Python):




h, w = map(int, input().split())
s_input = [input() for _ in range(h)]
s = [['.' for _ in range(w+2)] for _ in range(h+2)]
for i in range(1, h+1):
    for j in range(1, w+1):
        s[i][j] = s_input[i-1][j-1]

def sweep(i, j, s): 
    global h; global w
    ans = 0 
    
    if 0 < i < h+1:
        if 0 < j < w+1:
            sweeps = [s[i][j+1], s[i+1][j+1], s[i+1][j], s[i+1][j-1], s[i][j-1], s[i-1][j-1], s[i-1][j], s[i-1][j+1]]
            for sw in sweeps:
                if sw == '
                    ans += 1
    return ans 


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

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

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

Solution (Python):


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

def sweep(i, j, s):
    global h; global w
    ans = 0
    
    if 0 < i < h-1:
        if 0 < j < w-1:
            sweeps = [s[i][j+1], s[i+1][j+1], s[i+1][j], s[i+1][j-1], s[i][j-1], s[i-1][j-1], s[i-1][j], s[i-1][j+1]]
            for sw in sweeps:
                if sw == '
                    ans += 1
    
    if w > 1:
        if i == 0:
            if j == 0:
                sweeps = [s[i][j+1], s[i+1][j+1], s[i+1][j]]
                for sw in sweeps:
                    if sw == '
                        ans += 1
            elif j == w-1:
                sweeps = [s[i][j-1], s[i+1][j-1], s[i+1][j]]
                for sw in sweeps:
                    if sw == '
                        ans += 1
            else:
                sweeps = [s[i][j-1], s[i+1][j-1], s[i+1][j], s[i+1][j+1], s[i][j+1]]
                for sw in sweeps:
                    if sw == '
                        ans += 1
        elif i == h-1:
            if j == 0:
                sweeps = [s[i-1][j], s[i-1][j+1], s[i][j+1]]
                for sw in sweeps:
                    if sw == '
                        ans += 1
            elif j == w-1:
                sweeps = [s[i-1][j], s[i-1][j-1], s[i][j-1]]
                for sw in sweeps:
                    if sw == '
                        ans += 1
            else:
                sweeps = [s[i][j-1], s[i-1][j-1], s[i-1][j], s[i-1][j+1], s[i][j+1]]
                for sw in sweeps:
                    if sw == '
                        ans += 1
        else:
            if j == 0:
                sweeps = [s[i-1][j], s[i-1][j+1], s[i][j+1], s[i+1][j+1], s[i+1][j]]
                for sw in sweeps:
                    if sw == '
                        ans += 1
            elif j == w-1:
                sweeps = [s[i-1][j], s[i-1][j-1], s[i][j-1], s[i+1][j-1], s[i+1][j]]
                for sw in sweeps:
                    if sw == '
                        ans += 1

    
    else:
        if i == 0:
            sweeps = [s[i+1][0]]
            for sw in sweeps:
                if sw == '
                    ans += 1
        elif i == h-1:
            sweeps = [s[i-1][0]]
            for sw in sweeps:
                if sw == '
                    ans += 1
        else:
            sweeps = [s[i-1][0], s[i+1][0]]
            for sw in sweeps:
                if sw == '
                    ans += 1

    return ans

def sweep1line(j, s):
    global w
    ans = 0
    if w > 1:
        if j == 0:
            if s[0][j+1] == '
                ans += 1
        elif j == w-1:
            if s[0][j-1] == '
                ans += 1
        else:
            sweeps = [s[0][j-1], s[0][j+1]]
            for sw in sweeps:
                if sw == '
                    ans += 1
    return ans


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

if h != 1:
    for i in range(h):
        for j in range(w):
            if s[i][j] == '
                ans[i][j] = '
            if s[i][j] == '.':
                ans[i][j] = str(sweep(i, j, s))
else:
    for j in range(w):
        if s[0][j] == '
            ans[0][j] = '
        if s[0][j] == '.':
            ans[0][j] = str(sweep1line(j, s))


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

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

for i in range(0, H):
    s.append(input())

for y in range(0, H):
    for x in range(0, W):
        c=0
        for dx in range(-1, 2):
            for dy in range(-1, 2):
                xx = x + dx
                yy = y + dy
                if 0<=xx and xx<W and 0<=yy and yy<H:
                    if s[yy][xx]=="
                        c+=1

        if s[y][x]=="
            print("
        else:
            print(c, end='')
    print("")

Solution (Python):
import sys










stdin = sys.stdin
sys.setrecursionlimit(10 ** 7)
MIN = -10 ** 9
MOD = 10 ** 9 + 7
INF = float("inf")
IINF = 10 ** 18



h,w = map(int, stdin.readline().rstrip().split())
s = [list(map(str, stdin.readline().rstrip().split())) for _ in range(h)]

dx = [-1,0,1,-1,1,-1,0,1]
dy = [-1,-1,-1,0,0,1,1,1]
ans = []
for i in range(h):
    an = []
    for j in range(w):
        if s[i][0][j] == ".":
            count = 0
            for d in range(8):
                if h > i + dy[d] >= 0 and w > j + dx[d] >= 0:
                    if s[i + dy[d]][0][j + dx[d]] == "
                        count += 1
            a = str(count)
        else:
            a = "
        an += a
    print("".join(an))

Solution (Python):
import sys










stdin = sys.stdin
sys.setrecursionlimit(10 ** 7)
MIN = -10 ** 9
MOD = 10 ** 9 + 7
INF = float("inf")
IINF = 10 ** 18



h,w = map(int, stdin.readline().rstrip().split())
s = [list(map(str, stdin.readline().rstrip().split())) for _ in range(h)]

dx = [-1,0,1,-1,1,-1,0,1]
dy = [-1,-1,-1,0,0,1,1,1]
ans = []
for i in range(h):
    an = []
    for j in range(w):
        if s[i][0][j] == ".":
            count = 0
            for d in range(8):
                if h > i + dy[d] >= 0 and w > j + dx[d] >= 0:
                    if s[i + dy[d]][0][j + dx[d]] == "
                        count += 1
                    else:
                        pass
                else:
                    pass
            a = str(count)
        else:
            a = "
        an += a
    ans.append(an)

for i in range(h):
    p = "".join(ans[i])
    print(str(p))