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 i in range(h):
    i = list(input())
    s.append(i)

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

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

        tmp = 0
        for t in range(8):
            ni = i + dx[t]
            nj = j + dy[t]
            if ni < 0 or h <= ni: continue
            if nj < 0 or w <= nj: continue
            if s[ni][nj] == '
                tmp += 1

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

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

Solution (Python):
h,w=map(int,input().split())
s=[input() for i in range(h)]
for i in range(h):
    s[i]=[t.replace(".","0") for t in s[i]]
    s[i]=[t.replace("
    s[i]=[int(t) for t in s[i]]
for i in range(h):
    for j in range(w):
        if s[i][j]<0:
            if i>0:
                s[i-1][j]+=1
                if j>0:
                    s[i-1][j-1]+=1
                if j<w-1:
                    s[i-1][j+1]+=1
            if i<h-1:
                s[i+1][j]+=1
                if j>0:
                    s[i+1][j-1]+=1
                if j<w-1:
                    s[i+1][j+1]+=1
            if j>0:
                s[i][j-1]+=1
            if j<w-1:
                s[i][j+1]+=1

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

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

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

for i in range(H):
    S = input()
    for j,s in enumerate(S):
        if s == '
            ans[i][j] = '
            for a in range(-1,2):
                for b in range(-1,2):
                    if i+a >= 0 and j+b >= 0\
                        and i+a < H and j+b < W\
                        and ans[i+a][j+b] != '
                        ans[i+a][j+b] = str(int(ans[i+a][j+b])+1)

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

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

Solution (Python):



def f(i,j):
    ans[i+1][j+1]+=10**9+7
    ans[i][j]+=1
    ans[i][j+1]+=1
    ans[i][j+2]+=1
    ans[i+1][j]+=1
    
    ans[i+1][j+2]+=1
    ans[i+2][j]+=1
    ans[i+2][j+1]+=1
    ans[i+2][j+2]+=1


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

ans=[[0]*(W+2) for i in range (H+2)]

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

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


for i in range (H):
    A=ans[1+i][1:-1]
    for j in range (len(A)):
        if A[j]>10**9:
            A[j]="
        else:
            A[j]=str(A[j])
    print("".join(A))

Solution (Python):
import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


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

    dxdy8 = ((-1, -1), (-1, 0), (-1, 1), (0, -1), (0, 1), (1, -1), (1, 0), (1, 1))

    for x in range(H):
        for y in range(W):
            if S[x][y] == '.':
                res = 0
                for dx, dy in dxdy8:
                    nx, ny = x + dx, y + dy
                    if 0 <= nx < H and 0 <= ny < W and S[nx][ny] == '
                        res += 1
                S[x][y] = str(res)

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

    return


if __name__ == '__main__':
    main()

Solution (Python):

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

l = [list(input()) for i in range(h)]
def possible(i, j):
    if i == -1 or i == h:
        return False
    if j == -1 or j == w:
        return False    
    return True

for i in range(h):
    for j in range(w):
        count = 0
        if l[i][j] == '.':
            if possible(i-1, j) and l[i-1][j] == '
                count += 1
            if possible(i+1, j) and l[i+1][j] == '
                count += 1
            if possible(i, j+1) and l[i][j+1] == '
                count += 1
            if possible(i, j-1) and l[i][j-1] == '
                count += 1
            if possible(i-1, j+1) and l[i-1][j+1] == '
                count += 1
            if possible(i+1, j+1) and l[i+1][j+1] == '
                count += 1
            if possible(i-1, j-1) and l[i-1][j-1] == '
                count += 1
            if possible(i+1, j-1) and l[i+1][j-1] == '
                count += 1
            l[i][j] = str(count)
for li in l:
    print("".join(li))

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

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

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



for i in range(h):
    for j in range(w):
        if(a[i][j] == "
            continue
        else:
            cnt = 0
            for k in [-1,0,1]:
                for l in [-1,0,1]:
                    if(k == 0 and l == 0):
                        continue
                    else:
                        x = i + k
                        y = j + l 
                        if(0<= x < h and 0<= y < w and a[x][y] == "
                            cnt += 1
            
            a[i][j] = str(cnt)




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

Solution (Python):
Y_DIR=[-1,0,1]
X_DIR=[-1,0,1]
def count_bomb(y,x):
  cnt=0
  for yd in Y_DIR:
    yy=y+yd
    if not (0<=yy<h):
      continue
    for xd in X_DIR:
      xx=x+xd
      if not (0<=xx<w):
        continue
      if s[yy][xx]=="
        cnt+=1
  return cnt

h,w=map(int,input().split())
s=[]
for i in range(h):
  s.append(input())
  
ans=["" for _ in range(h)]
for y in range(h):
  for x in range(w):
    if s[y][x] == ".":
      c=count_bomb(y,x)
      ans[y]+=str(c)
    else:
      ans[y]+=s[y][x]

print("\n".join(ans))

Solution (Python):

h,w = map(int,input().split())
s = []
s.append(list("." for i in range(w+2)))
for i in range(h):
    s.append(list("."+input()+"."))
s.append(list("." for i in range(w+2)))
ans = [[0 for j in range(w) ] for i in range(h) ]
    
for i in range(1,h+1):
    for j in range(1,w+1):
        if s[i][j] != "
            for m in range(i-1,i+2):
                for n in range(j-1,j+2):
                    if s[m][n] == "
                        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):
    print("".join(ans[i]))

Solution (Python):
H,W = map(int,input().split())
S = [input() for _ in range(H)]
A = [[0]*W for _ in range(H)]
B = [(1,0),(0,1),(-1,0),(0,-1),(1,1),(1,-1),(-1,1),(-1,-1)]
for i in range(H):
    for j in range(W):
        if S[i][j] == "
            A[i][j] = "
for i in range(H):
    for j in range(W):
        if A[i][j] != "
            for dx,dy in B:
                if 0 <= j+dx <= W-1 and 0 <= i+dy <= H-1:
                    if A[i+dy][j+dx] == "
                        A[i][j] += 1
for i in A:
    print(*i, sep = "")

Solution (Python):
H,W = map(int,input().split())
S = [input() for _ in range(H)]
A = []
for _ in range(H):
    A.append([0]*W)
B = [(1,0),(0,1),(-1,0),(0,-1),(1,1),(1,-1),(-1,1),(-1,-1)]
for i in range(H):
    for j in range(W):
        if S[i][j] == "
            A[i][j] = "
for i in range(H):
    for j in range(W):
        if A[i][j] != "
            for dx,dy in B:
                if 0<=j+dx<=W-1 and 0<=i+dy<=H-1:
                    if A[i+dy][j+dx] == "
                        A[i][j] += 1
for i in range(H):
    for j in range(W):
        print(A[i][j], end = "")
    print("")

Solution (Python):
H, W = list(map(int, input().split()))
S_list = []
for i in range(H):
    S_list.append(input())
for i in range(H):
    for j in range(W):
        m = S_list[i][j]
        if m == '
            print(m, end='')
        else:
            sum = 0
            if i != 0 and j != 0 and S_list[i-1][j-1] == '
            if i != 0 and S_list[i-1][j] == '
            if i != 0 and j != W-1 and S_list[i-1][j+1] == '
            if j != 0 and S_list[i][j-1] == '
            if j != W-1 and S_list[i][j+1] == '
            if i != H-1 and j != 0 and S_list[i+1][j-1] == '
            if i != H-1 and S_list[i+1][j] == '
            if i != H-1 and j != W-1 and S_list[i+1][j+1] == '
            print(sum, end='')
    print('')

Solution (Python):
h,w = map(int, input().split())
s = [list(input()) for _ in range(h)]
p = [-1,0,1]
for i in range(h):
  for j in range(w):
    count = 0
    for ph in p:
      for pw in p:
        if (ph==0 and pw==0) or not(0<=i-ph<=h-1) or not(0<=j-pw<=w-1):
          continue
        if s[max(i-ph,0)][max(j-pw,0)]=='
          count += 1
    if s[i][j]=='.': s[i][j]=str(count)
for si in s:
  print(''.join(si))

Solution (Python):
def count_bomb(x, y):
    cnt = 0
    for i in range(x-1, x+2):
        for j in range(y-1, y+2):
            if 0<=i<w and 0<=j<h:
                if s[j][i] == '
    return cnt

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

for i in range(w):
    for j in range(h):
        if s[j][i] == '
            k[j][i] = "
        else:
            k[j][i] = str(count_bomb(i, j))

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

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

field=[]
for _ in range(h):
    field.append(list(input()))

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

for i in range(h):
    for j in range(w):
        if field[i][j]=="
            continue
        count=0
        for x,y in zip(dx,dy):
            if i+x<0 or i+x>h-1 or j+y<0 or j+y>w-1:
                continue
            if field[i+x][j+y]=="
                count+=1
        field[i][j] = str(count)

for f in field:
    print(''.join(f))

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

ans = []
s=[]
s.insert(0,'.'*(w+2))
for i in range(h):
    s.append('.'+input()+'.')
s.append('.'*(w+2))

for y in range(1,h+1):
    column=[]
    for x in range(1,1+w):
        if s[y][x]=='
            column.append('
        else:
            column.append(str(s[y-1][x-1:x+2].count('
    ans.append(''.join(column))
for i in ans:
    print(i)

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


S.insert(0, "0"*W)
S.append("0"*W)
S = ["0" + s + "0" for s in S]
S = [s.replace("


delta_H = [-1, -1, -1, 0, 0, 1, 1, 1]
delta_W = [-1, 0, 1, -1, 1, -1, 0, 1]



S_new = [""]*H

for i in range(1, H+1):
    for j in range(1, W+1):
        if S[i][j] == ".":
            ans = 0
            
            for k in range(8):
                if S[i + delta_H[k]][j + delta_W[k]] != ".":
                    ans += int(S[i + delta_H[k]][j + delta_W[k]])
                    
            S_new[i-1] += str(ans)
            
        else:
            S_new[i-1] += "
            
print(*S_new, sep="\n")

Solution (Python):




import numpy as np

def int_mtx(N):
    x = []
    for _ in range(N):
        x.append(input())
    return x

H,W = map(int,input().split())
S = int_mtx(H)
Scopy = S.copy()
for i in range(0,H):
    Scopy[i] = list(Scopy[i])

for i in range(0,H):
    for j in range(0,W):

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

for i in range(0,H):
    print("".join(Scopy[i]))

Solution (Python):
h,w=map(int,input().split())
s=[[c for c in input()] for _ in range(h)]
for i in range(h):
    for j in range(w):
        if s[i][j]=='
            continue
        c=0
        for a,b in [(-1,-1),(-1,0),(-1,1),(0,-1),(0,1),(1,-1),(1,0),(1,1)]:
            if i+a<0 or i+a>h-1 or j+b<0 or j+b>w-1:
                continue
            if s[i+a][j+b]=='
                c+=1
        s[i][j]=str(c)
for i in s:
    print(''.join(i))

Solution (Python):
import itertools

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

SS = [[0 for _ in range(W)] for _ in range(H)]

for h in range(H):
	for w in range(W):
		if '

for h in range(H):
	for w in range(W):
		if SS[h][w] == -1:
			for y, x in itertools.product([-1, 0, 1], [-1, 0, 1]):
				if 0 <= h+y < H and 0 <= w+x < W:
					if SS[h+y][w+x] != -1 :
						SS[h+y][w+x] += 1

for h in range(H):
	for w in range(W):
		if SS[h][w] == -1:print('
		else : print(SS[h][w], end="")
	print()

Solution (Python):
h,w=map(int,input().split())
g=[input() for _ in range(h)]
a=[[0]*w for _ in range(h)]
for x in range(h):
  for y in range(w):
    if g[x][y]=='.': continue
    a[x][y]='
    for dx,dy in [(1,0),(0,1),(-1,0),(0,-1),(1,1),(-1,1),(-1,-1),(1,-1)]:
      nx,ny=x+dx,y+dy
      if 0<=nx<h and 0<=ny<w and g[nx][ny]=='.':
        a[nx][ny]+=1
for l in a:
  print(*l,sep='')

Solution (Python):


H,W = [int(i) for i in input().split()]
S = []
S.append('.'*(W+2))
for i in range(H):
    S.append('.'+input()+'.')
S.append(['.']*(W+2))

for y in range(1,H+1):
    for x in range(1,W+1):
        if S[y][x] == '
            print('
        else:
            print(S[y-1][x-1:x+2].count('
    print("")

Solution (Python):
import sys
def input(): return sys.stdin.readline().strip()

def main():
    H, W = map(int, input().split())
    S = ['.' * W] + [input() for _ in range(H)] + ['.' * W]
    if W == 1:
        for i in range(1, H + 1):
            if S[i] == '
                print('
                continue
            string = S[i - 1] + S[i + 1]
            cnt = string.count('
            print(str(cnt))
        return
    for i in range(1, H + 1):
        code = ""
        for j in range(W):
            if S[i][j] == '
                code += '
                continue
            if j == 0:
                string = S[i - 1][:2] + S[i][1] + S[i + 1][:2]
                cnt = string.count('
                code += str(cnt)
                continue
            if j == W - 1:
                string = S[i - 1][-2:] + S[i][-2] + S[i + 1][-2:]
                cnt = string.count('
                code += str(cnt)
                continue
            string = S[i - 1][j - 1:j + 2] + S[i][j - 1] + S[i][j + 1] + S[i + 1][j - 1:j + 2]
            cnt = string.count('
            code += str(cnt)
            continue
        print(code)


if __name__ == "__main__":
    main()

Solution (Python):
H, W = map(int, input().split())
S = [input() for _ in range(H)]
for i in range(H):
    S[i] = list(S[i])
    
if (H == 1) and (W == 1):
    if S[0][0] == '.':
        S[0] = '0'
elif (H == 1) and (W != 1):
    for col in range(W):
        if S[0][col] == '
            continue
        if col == 0:
            S[0][col] = str([S[0][col+1]].count('
        elif col == W - 1:
            S[0][col] = str([S[0][col-1]].count('
        else:
            S[0][col] = str([S[0][col-1], S[0][col+1]].count('
elif (H != 1) and (W == 1):
    for row in range(H):
        if S[row][0] == '
            continue
        if row == 0:
            S[row][0] = str([S[row+1][0]].count('
        elif row == H - 1:
            S[row][0] = str([S[row-1][0]].count('
        else:
            S[row][0] = str([S[row-1][0], S[row+1][0]].count('
else:
    for row in range(H):
        for col in range(W):
            if S[row][col] == '
                continue
            if row == 0:
                if col == 0:
                    S[row][col] = str([S[row][col+1], S[row+1][col], S[row+1][col+1]].count('
                elif col == W - 1:
                    S[row][col] = str([S[row][col-1], S[row+1][col-1], S[row+1][col]].count('
                else:
                    S[row][col] = str([S[row][col-1], S[row][col+1], S[row+1][col-1],
                               S[row+1][col], S[row+1][col+1]].count('
            elif row == H - 1:
                if col == 0:
                    S[row][col] = str([S[row-1][col], S[row-1][col+1], S[row][col+1]].count('
                elif col == W - 1:
                    S[row][col] = str([S[row-1][col-1], S[row-1][col], S[row][col-1]].count('
                else:
                    S[row][col] = str([S[row-1][col-1], S[row-1][col], S[row-1][col+1],
                                   S[row][col-1], S[row][col+1]].count('
            else:
                if col == 0:
                    S[row][col] = str([S[row-1][col], S[row-1][col+1],
                                   S[row][col+1],
                                   S[row+1][col], S[row+1][col+1]].count('
                elif col == W - 1:
                    S[row][col] = str([S[row-1][col-1], S[row-1][col],
                                   S[row][col-1], 
                                   S[row+1][col-1], S[row+1][col]].count('
                else:
                    S[row][col] = str([S[row-1][col-1], S[row-1][col], S[row-1][col+1],
                                   S[row][col-1], S[row][col+1],
                                   S[row+1][col-1], S[row+1][col], S[row+1][col+1]].count('
for i in range(len(S)):
    print(''.join(S[i]))

Solution (Python):
from itertools import product

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

for y, x in product(range(H), range(W)):
    if S[y][x] == "
        continue

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

print("\n".join("".join(r) for r in S))

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

def kazu(h,w):
    houkou = [[1,0],[1,-1],[0,-1],[-1,-1],[-1,0],[-1,1],[0,1],[1,1]]
    count = 0
    for ya in houkou:
        if 0 <= h+ya[0] < H and 0 <= w+ya[1] < W:
            if masu[h+ya[0]][w+ya[1]] == '
                count += 1

    return count

for w in range(W):
    for h in range(H):
        if masu[h][w]=='.':
            masu[h][w] = kazu(h,w)

for l in masu:print(*l,sep='')

Solution (Python):
h,w=map(int,input().split())
s=[input() for _ in range(h)]
ans=[[] for _ in range(h)]
for i in range(h):
    for j in range(w):
        if s[i][j]==".":
            cnt=0
            for x,y in [(0,1),(1,0),(0,-1),(-1,0),(1,1),(1,-1),(-1,1),(-1,-1)]:
                if 0<=i+x<h and 0<=j+y<w:
                    if s[i+x][j+y]=="
                        cnt+=1
            ans[i].append(str(cnt))
        else:
            ans[i].append("
for i in range(h):
    print("".join(ans[i]))

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


for h in range(H):
    for w in range(W):
        x, y = w, h
        if S[y][x] == ".":
            c = 0
            for i in range(max(x-1, 0), min(x+2, W)):
                for j in range(max(y-1, 0), min(y+2, H)):
                    if S[j][i] == "
                        c += 1
            S[y][x] = str(c)

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

Solution (Python):
H, W = map(int, input().split(' '))
count = [['0']*W for i in range(H)]

def update_count(i, j, count):
    count[i][j] = '
    start_i = 0 if i-1 < 0 else i-1
    end_i = H if i + 2 > H else i+2
    start_j = 0 if j-1 < 0 else j-1
    end_j = W if j + 2 > W else j+2
    for k in range(start_i, end_i):
        for l in range(start_j, end_j):
            if count[k][l] != '
                count[k][l] = str(1 + int(count[k][l]))
    return count

for i in range(H):
    S = input()
    for j in range(W):
        if S[j] == '
            count = update_count(i, j, count)

for i in range(H):
    print(''.join(count[i]))

Solution (Python):
import copy
H,W=map(int,input().split())
List = []
for i in range (H):
  List.append(input())
resList = copy.deepcopy(List)

def bombCheck(x,y,ListX):  
  a = 0
  b = 0
  res = 0
  if ListX[x][y] == "
    return "
  for i in range(-1,2,1):
    a = x+i
    if 0<= a < H:
      for j in range(-1,2,1):
        b = y+j
        if 0<= b < W:
          if i == 0 and j ==0:
            pass
          else:
            if List[a][b] == "
              res +=1
  return str(res)

for i in range(H):
  resList[i] =""
  for j in range(W):
    resList[i] += bombCheck(i,j,List)
for i in resList:
  print(i)

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

if h == 1 and w == 1:
    if s[0][0] == "
        print("
    else:
        print(0)

elif h == 1:
    for j in range(w):
        if s[0][j] == ".":
            ans[0][j] = s[0][max(0, j - 1):min(w + 1, j + 2)].count("
        else:
            ans[0][j] = "

elif w == 1:
    for i in range(h):
        if i == 0:
            if s[i][0] == ".":
                ans[i][0] = s[i + 1][0].count("
            else:
                ans[i][0] = "
        elif i == h - 1:
            if s[i][0] == ".":
                ans[i][0] = s[i - 1][0].count("
            else:
                ans[i][0] = "
        else:
            if s[i][0] == ".":
                ans[i][0] = s[i - 1][0].count("
            else:
                ans[i][0] = "

else:
    for i in range(h):
        for j in range(w):
            if i == 0:
                if s[i][j] == ".":
                    ans[i][j] = s[i][max(0, j - 1):min(w + 1, j + 2)].count("
                                                                                   max(0, j - 1):min(w + 1, j + 2)].count(
                        "
                else:
                    ans[i][j] = "
            elif i == h-1:
                if s[i][j] == ".":
                    ans[i][j] = s[i - 1][max(0, j - 1):min(w + 1, j + 2)].count("
                                                                                                              j + 2)].count(
                        "
                else:
                    ans[i][j] = "
            else:
                if s[i][j] == ".":
                    ans[i][j] = s[i - 1][max(0, j - 1):min(w + 1, j + 2)].count("
                                                                                                              j + 2)].count(
                        "
                else:
                    ans[i][j] = "

[print(*k,sep="") for k in ans]

Solution (Python):
import sys
def MI(): return map(int,sys.stdin.readline().rstrip().split())
def LS2(): return list(sys.stdin.readline().rstrip())  

H,W = MI()
S = [['.']*(W+2)]
for i in range(H):
    S.append(['.'] + LS2() + ['.'])
S.append(['.']*(W+2))

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

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

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

wall = ["." * (w + 2)]
s = wall + ["." + input() + "." for _ in range(h)] + wall

for i in range(1, h + 1):
    for j in range(1, w + 1):
        if s[i][j] == ".":
            t = s[i-1][j-1:j+2] + s[i][j-1:j+2] + s[i+1][j-1:j+2]
            s[i] = s[i][:j] + str(t.count("
    else:
        print(s[i][1:-1])

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

t = [[0 for _ in range(w)] for _ in range(h)]
for i in range(h):
    for j in range(w):
        if s[i][j] == ".":
            bombs = 0
            for k in range(i - 1, i + 2):
                for l in range(j - 1, j + 2):
                    if 0 <= k < h and 0 <= l < w:
                        if s[k][l] == "
                            bombs += 1
            else:
                t[i][j] = str(bombs)
        else:
            t[i][j] = "
else:
    for i in range(h):
        print("".join(t[i]))

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

for _ in range(h):
    board.append(input())

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

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

        for d in range(8):
            ni = i + dy[d]
            nj = j + dx[d]

            if (ni < 0 or ni >= h) or (nj < 0 or nj >= w):
                continue
            if board[ni][nj] == "
                cnt += 1

        temp_board = list(board[i])
        temp_board[j] = str(cnt)
        board[i] = "".join(temp_board)

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

Solution (Python):
import copy
H, W = list(map(int, input().split()))
tb = []
for h in range(H+2):
    line = []
    if h == 0 or h == H+2-1:
        for w in range(W+2):
            line.append(0)
    else:
        line = list("." + input() + ".")
        for w in range(W+2):
            if line[w] == ".":
                line[w] = 0
    tb.append(line)




for i in range(1,H+1):
    for j in range(1,W+1):
        if tb[i][j] != "
            if tb[i-1][j] == "
            if tb[i+1][j] == "
            if tb[i][j-1] == "
            if tb[i][j+1] == "
            if tb[i-1][j-1] == "
            if tb[i-1][j+1] == "
            if tb[i+1][j-1] == "
            if tb[i+1][j+1] == "


[print("".join(list(map(str,line)))[1:-1]) for i,line in enumerate(tb) if 0 < i < H+2-1];print()

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


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

for i in range(h):
    for j in range(w):
        if s[i][j] == "
        cnt = 0
        for k in range(8):
            x = j + dx[k]
            y = i + dy[k]
            if x < 0 or y < 0 or w <= x or h <= y: continue
            if s[y][x] == "
                cnt += 1
        s[i][j] = str(cnt)
print(*map("".join, s), sep = "\n")

Solution (Python):
import sys
read = sys.stdin.read
readlines = sys.stdin.readlines
from itertools import chain
from copy import deepcopy
def main():
    h, w = map(int, input().split())
    
    
    hw = []
    hw.append([0] * (w + 2))
    for _ in range(h):
        row = list(input())
        row2 = [-1 if i == '
        row3 = list(chain([0], row2, [0]))
        hw.append(row3)
    hw.append([0] * (w + 2))
    hw2 = deepcopy(hw)
    for i1 in range(1, h + 1):
        for i2 in range(1, w + 1):
            if hw[i1][i2] == 0:
                bombs = sum(hw[i1 - 1][i2-1:i2+2])
                bombs += sum(hw[i1][i2 - 1:i2 + 2])
                bombs += sum(hw[i1 + 1][i2 - 1:i2 + 2])
                hw2[i1][i2] = - bombs
    for j1 in range(1, h + 1):
        row4 = hw2[j1][1:w+1]
        row5 = ['
        print("".join(row5))


if __name__ == '__main__':
    main()

Solution (Python):
def main():
    import sys

    def input(): return sys.stdin.readline().rstrip()
    
    h, w = map(int, input().split())
    g = [[] for _ in range(h)]
    di = [-1, 0, 1, 0, 1, 1, -1, -1]
    dj = [0, 1, 0, -1, 1, -1, -1, 1]

    for i in range(h):
        g[i] = list(input())
        
    for i in range(h):
        for j in range(w):
            if g[i][j] == '
                continue
            
            cnt = 0
            for k in range(8):
                ni = i + di[k]
                nj = j + dj[k]
                if ni < 0 or ni >= h or nj < 0 or nj >= w:
                    continue
                if g[ni][nj] == '
                    cnt += 1
            g[i][j] = str(cnt)
    
    for i in g:
        print(''.join(i))

            



if __name__ == '__main__':
    main()

Solution (Python):

h, w = map(int, input().split())
s = [[0 if i == "." else -9 for i in input()] for _ in range(h)]
d = ((1, 0), (1, 1), (1, -1), (-1, 0), (-1, 1), (-1, -1), (0, 1), (0, -1))
for i in range(h):
    for j in range(w):
        if s[i][j] < 0:
            for x, y in d:
                if 0 <= i + x < h and 0 <= j + y < w:
                    s[i + x][j + y] += 1
for i in s:
    print(*["

Solution (Python):
h, w = map(int, input().split(" "))
s = []
for i in range(h):
    s.append(str(input()))
d = [(1, 0), (0, 1), (-1, 0), (0, -1), (1, 1), (-1, 1), (1, -1), (-1, -1)]
ss = ["" for i in range(h)]
for i in range(w):
    for j in range(h):
        if s[j][i] == "
            ss[j] += "
            continue
        cnt = 0
        for dx, dy in d:
            if 0 <= i + dx < w and 0 <= j + dy < h:
                if s[j + dy][i + dx] == "
                    cnt += 1
        ss[j] = ss[j] + str(cnt)

for i in ss:
    print(i)

Solution (Python):
if __name__ == "__main__":
    h,w = map(int,input().split())
    s = []
    s.append([0]*(w+2))

    for i in range(h):
        q = [0]
        q.extend(list(input()))
        q.append(0)
        s.append(q)
    s.append([0]*(w+2))

    for p in range(h):
        for q in range(w):
            if s[p+1][q+1] == "
                print("
            else:
                aro = [s[p][q],s[p+1][q],s[p+2][q],s[p][q+1],s[p+2][q+1],s[p][q+2],s[p+1][q+2],s[p+2][q+2]]
                print(aro.count("
        print("")

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