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

def inc(i, j, H, W, field):
    if not(0 <= i <= H-1 and 0 <= j <= W-1):
        pass
    elif field[i][j] == "
        pass
    else:
        field[i][j] += 1


def main():
    H, W = [int(i) for i in input().split()]
    ans = [[0] * W for _ in range(H)]
    for i in range(H):
        line = input()
        for j in range(W):
            if line[j] == "
                ans[i][j] = "
                for d in product([-1, 0, 1], repeat=2):
                    d_i, d_j = d
                    inc(i+d_i, j+d_j, H, W, ans)
    for a in ans:
        print(*a, sep="", end="\n")


if __name__ == "__main__":
    main()

Solution (Python):
import sys

h, w = map(int, sys.stdin.readline().split())
canvas = ['.' * (w + 2)] + ['.' + sys.stdin.readline().rstrip() + '.' for _ in range(h)] + ['.' * (w + 2)]


def main():
    res = [[0] * (w + 1) for _ in range(h + 1)]
    for i in range(1, h + 1):
        for j in range(1, w + 1):
            if canvas[i][j] == '
                res[i][j] = '
                continue
            for di in range(-1, 2):
                for dj in range(-1, 2):
                    y = i + di
                    x = j + dj
                    if canvas[y][x] == '
                        res[i][j] += 1
    
    for i in range(1, h+1):
        yield ''.join(list(map(str, res[i][1:])))

if __name__ == '__main__':
    ans = main()
    print(*ans, sep='\n')

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

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

            if grid[i][j] != '
                grid[i][j] += 1

            if grid[i][j + 1] != '
                grid[i][j + 1] += 1

            if grid[i][j+2] != '
                grid[i][j+2] += 1

            if grid[i+1][j] != '
                grid[i+1][j] += 1

            if grid[i+1][j+2] != '
                grid[i+1][j+2] += 1

            if grid[i+2][j] != '
                grid[i+2][j] += 1

            if grid[i+2][j+1] != '
                grid[i+2][j+1] += 1

            if grid[i+2][j+2] != '
                grid[i+2][j+2] += 1




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

Solution (Python):
H, W = map(int, input().split())
S = [list(input()) for i in range(H)]
grid = []
for i in range(H+2):
    grid.append([0]*(W+2))
    
idxes = []
tmpIdxes = []
for i in range(H):
    for j in range(W):
        if S[i][j] == '
            idxes.append([i,j])
            tmpIdxes.append([i+1, j+1])
            
for tmpIdx in tmpIdxes:
    grid[tmpIdx[0]-1][tmpIdx[1]-1] += 1
    grid[tmpIdx[0]-1][tmpIdx[1]] += 1
    grid[tmpIdx[0]-1][tmpIdx[1]+1] += 1
    grid[tmpIdx[0]][tmpIdx[1]-1] += 1
    grid[tmpIdx[0]][tmpIdx[1]+1] += 1
    grid[tmpIdx[0]+1][tmpIdx[1]-1] += 1
    grid[tmpIdx[0]+1][tmpIdx[1]] += 1
    grid[tmpIdx[0]+1][tmpIdx[1]+1] += 1

for tmpIdx in tmpIdxes:
    grid[tmpIdx[0]][tmpIdx[1]] = "

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

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

eight = [[-1,-1],[0,-1],[1,-1], 
         [-1,0],[1,0],
         [-1,1],[0,1],[1,1]]

for i in range(H):
  for j in range(W):
    if S[i][j] == '
      for e in eight:
        if (0 <= i+e[0] < H) and (0 <= j+e[1] < W) :
          target = S[i+e[0]][j+e[1]]
          if target != '
            if target == '.' :
              S[i+e[0]][j+e[1]] = 1
            else :
              S[i+e[0]][j+e[1]] += 1

for i in range(H):
  for j in range(W):
    print('0' if S[i][j] == '.' else str(S[i][j]), end='')
  if i < H: print('')

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

for i in range(H):
    ls.append([i if i == '

for i in range(H):
    for j in range(W):
        if ls[i][j] == '
            ii_min = i if (i - 1) < 0 else i - 1
            ii_max = i if (i + 1) >= H else i + 1
            jj_min = j if (j - 1) < 0 else j - 1
            jj_max = j if (j + 1) >= W else j + 1

            for ii in range(ii_min, ii_max + 1):
                for jj in range(jj_min, jj_max + 1):
                    if ls[ii][jj] != '
                        ls[ii][jj] += 1

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

Solution (Python):
import sys
h,w = map(int, sys.stdin.readline().split())
s = list(input() for _ in range(h))

dx_list = [-1, 0, 1]
dy_list = [-1, 0, 1]

for i in range(h):
    for j in range(w):
        count = 0
        if s[i][j] == "
            print("
            continue
        for dx in dx_list:
            for dy in dy_list:
                x = i + dx
                y = j + dy
                if 0 <= x < h and 0 <= y < w and s[x][y] == "
                    count += 1
        print(count, end="")
    print()

Solution (Python):
H, W = map(int, input().split())
S = (
    [["."] * (W + 2)]
    + [["."] + [i for i in input()] + ["."] for j in range(H)]
    + [["."] * (W + 2)]
)
ans = []
for i in range(1, H + 1):
    i_ans = []
    for j in range(1, W + 1):
        if S[i][j] == "
            i_ans.append("
            continue
        num = 0
        arrows = [[1, 0], [1, 1], [0, 1], [-1, 1], [-1, 0], [-1, -1], [0, -1], [1, -1]]
        for arrow in arrows:
            if S[i + arrow[0]][j + arrow[1]] == "
                num += 1
        i_ans.append(num)
    ans.append(i_ans)
for i in ans:
    print(*i, sep="")

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

Solution (Python):
import sys
input = sys.stdin.readline
H,W=map(int,input().rstrip().split())
S=[input().rstrip() for _ in range(H)]
c=[[0 for _ in range(W)] for __ in range(H)]
dx=[0,1,1,1,0,-1,-1,-1]
dy=[1,1,0,-1,-1,-1,0,1]

for i in range(H):
    for j in range(W):
        if(S[i][j]=="
            c[i][j]="
            continue
        for direction in range(8):
            if(i==0):
                if(i+dy[direction]<0):
                    continue
            if(i==H-1):
                if(i+dy[direction]>=H):
                    continue
            if(j==0):
                if(j+dx[direction]<0):
                    continue
            if(j==W-1):
                if(j+dx[direction]>=W):
                    continue

            if(S[i+dy[direction]][j+dx[direction]]=="
                c[i][j]+=1

for i in range(H):
    s=""
    for j in range(W):
        s+=str(c[i][j])
    print(s)

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

for i in range(h):
    ans = ''
    for j in range(w):
        if s[i][j] == '.':
            n = 0
            for x in [-1, 0, 1]:
                for y in [-1, 0, 1]:
                    xx = x + i
                    yy = y + j
                    if (x != 0 or y != 0) and xx >= 0 and xx <= h - 1 and yy >= 0 and yy <= w - 1:
                        if s[xx][yy] == '
                            n += 1
            ans += str(n)
        else:
            ans += '
    print(ans)

Solution (Python):
H,W=map(int, input().split(' '))
Map = [list("A" + input() + "A") for i in range(H)]
Map.insert(0,["A"]*(W+2))
Map.append(["A"]*(W+2))
ans = [[] for i in range(H)]
for i in range(1,H+1):
    for j in range(1,W+1):
        if Map[i][j] == "
            ans[i-1].append("
        else:
            minenum = 0
            for (x,y) in [(i-1,j-1),(i-1,j),(i-1,j+1),(i,j-1),(i,j+1),(i+1,j-1),(i+1,j),(i+1,j+1)]:
                if Map[x][y] == "
                    minenum += 1
            ans[i-1].append(minenum)
for x in ans:
    print(*x,sep="")

Solution (Python):
h, w = map(int, input().split())
s0 = list(["." for _ in range(w+2)])
s1 = [list("." + input() + ".") for _ in range(h)]
s = []
s.append(s0)
s = s + s1
s.append(s0)
a = [-1,0,1]
b = [-1,0,1]
for i in range(1,h+1) :
    for j in range(1,w+1):
        if not s[i][j] == "
            cnt = 0
            for ii in range(3):
                for jj in range(3):
                    if s[i+a[ii]][j+a[jj]] == "
                        cnt +=1
            s[i][j] = str(cnt)
for i in range(1,h+1) :
    for j in range(1,w+1):
        print(s[i][j],end="")
    print()

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

dx=[0,1,1,1,0,-1,-1,-1]
dy=[1,1,0,-1,-1,-1,0,1]
for i in range(h):
    for j in range(w):
        if L[i][j]=='
            for k in range(8):
                xr=j+dx[k]
                yr=i+dy[k]
                if (yr>=0 and yr<h and xr>=0 and xr<w):
                    if L[yr][xr]=='.':
                        L[yr][xr]=1
                    elif L[yr][xr]=='
                        continue
                    else:
                        L[yr][xr]+=1
        elif L[i][j]=='.':
            L[i][j]=0
for i in range(h):
    print(*L[i],sep='')

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

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

ans = []
for i in range(1, H+1):
    tmp = []
    for j in range(1, W+1):
        count = 0
        if S[i-1][j-1] == "
            count += 1
        if S[i][j-1] == "
            count += 1
        if S[i-1][j] == "
            count += 1
        if S[i+1][j-1] == "
            count += 1
        if S[i-1][j+1] == "
            count += 1
        if S[i][j+1] == "
            count += 1
        if S[i+1][j] == "
            count += 1
        if S[i+1][j+1] == "
            count += 1
        if S[i][j] == "
            tmp.append("
        else:
            str_count = str(count)
            tmp.append(str_count)
    ans.append(tmp)

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

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

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

for i in range(1, h + 1):
    print(*s[i][1:w + 1], sep='')

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] == '.':
            cnt = 0
            for k in range(-1, 2):
                for l in range(-1, 2):
                    if 0 <= i + k and 0 <= j + l and i + k < h and j + l < w and s[i + k][j + l] == '
                        cnt += 1
            s[i][j] = cnt
for i in range(h):
    print(*s[i], sep='')

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

d = [[-1,-1],[0,-1],[1,-1],
     [-1, 0],       [1, 0],
     [-1, 1],[0, 1],[1, 1]]

for hi in range(h):
  for wi in range(w):
    if ff[hi][wi]=='
      pass
    else:
      count = 0
      for dx,dy in d:
        x = dx+wi
        y = dy+hi        
        if (0<= x < w) and (0<= y <h) :
          if ff[y][x] == '
            count += 1
      ff[hi][wi] = str(count)
      
for fi in ff:
  print(''.join(fi))

Solution (Python):
h,w=map(int,input().split())
l=[input() for i in range(h)]
ans=[[0]*(w+2) for i in range(h+2)]

for i in range(h):
  for j in range(w):
    if l[i][j]=='
      ans[i+1][j+1]=10
      
      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
      
ans=[i[1:-1] for i in ans][1:-1]
ans=[['
for i in ans:
  print(''.join(i))

Solution (Python):
import numpy as np
h,w = map(int, input().split())
mat = np.array([list(map(int,input().replace(".","0").replace("
mat = np.concatenate([np.zeros((1,w),int),mat,np.zeros((1,w),int)])
mat = np.concatenate([np.zeros((h+2,1),int),mat,np.zeros((h+2,1),int)], axis = 1)
import copy
nmat = copy.deepcopy(mat)
for i in range(1,h+1):
  for j in range(1, w+1):
    if mat[i,j] == 0:
      nmat[i,j] = mat[i+1,j]+mat[i-1,j]+mat[i,j+1]+mat[i,j-1]+mat[i-1,j-1]+mat[i-1,j+1]+mat[i+1,j-1]+mat[i+1,j+1]
nmat = np.array(nmat, dtype = "str")
nmat[np.where(mat == 1)] = "
for i in range(h):
  print("".join(nmat[1:h+1,1:w+1].tolist()[i]))

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

for i in range(1, h+1):
    for j in range(1, w+1):
        if S[i][j] == ".":
            S[i][j] = (str([S[i-1][j], S[i+1][j], S[i][j-1], S[i][j+1], S[i-1][j-1], S[i-1][j+1], S[i+1][j-1], S[i+1][j+1]].count("
for k in range(len(S[1:h+1])):
    print(*S[1:h+1][k][1:w+1], sep="")

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

N = [(0, 1), (1, 0), (1, 1), (0, -1), (-1, 0), (1, -1), (-1, 1), (-1, -1)]
ans = [[] for _ in range(H)]
for h in range(1, H + 1):
    for w in range(1, W + 1):
        if S[h][w] == '.':
            ans[h - 1].append(sum([S[h + n[0]][w + n[1]] == '
        else:
            ans[h - 1].append('

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

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

def search(h,w):
    count = 0
    if mat[h][w] == '
        return '
    else:
        if h+1 <= H-1:
            if mat[h+1][w] == '
                count += 1
            if w+1 <= W-1:
                if mat[h+1][w+1] == '
                    count += 1
            if w-1 >= 0:
                if mat[h+1][w-1] == '
                    count += 1
        if h-1 >= 0:
            if mat[h-1][w] == '
                count += 1
            if w+1 <= W-1:
                if mat[h-1][w+1] == '
                    count += 1
            if w-1 >= 0:
                if mat[h-1][w-1] == '
                    count += 1
        if w+1 <= W-1:
            if mat[h][w+1] == '
                count += 1
        if w-1 >= 0:
            if mat[h][w-1] == '
                count += 1   
        return count
    
for i in range(H):
    for j in range(W):
        ans[i][j] = search(i,j)
        
for i in range(H):
    ans[i] = map(str,ans[i])
    print(''.join(ans[i]))

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

for i in range(h):
    for j in range(w):
        count=0
        if s[i][j]=='.':
            for k in range(8):
                x=j+dx[k]
                y=i+dy[k]
                if 0<=x<=w-1 and 0<=y<=h-1:
                    if s[y][x]=='
                        count+=1
                        
            s[i][j]=str(count)

for i in s:
    print(*i,sep='')

Solution (Python):
Y,X=map(int,input().split())
info=[]
for i in range(Y):
    s=list(input())
    info.append(s)


def search(y,x):
    a=0
    if 0<=y<=Y-1 and 0<=x<=X-1:
        if info[y][x]=="
            a+=1
    return a
            
def count(y,x):
    num=0
    num+=search(y-1,x-1)
    num+=search(y-1,x)
    num+=search(y-1,x+1)
    num+=search(y,x-1)
    num+=search(y,x+1)
    num+=search(y+1,x-1)
    num+=search(y+1,x)
    num+=search(y+1,x+1)
    return num

for i in range(Y):
    for j in range(X):
        if info[i][j]!="
            info[i][j]=str(count(i,j))
for i in range(Y):
    for j in range(X):
        print(info[i][j],end="")
    print()

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


al_l = [["

for i in range(h):
    for j in range(w):
        if sl[i][j] == "
            continue
        ans = 0
        for v in range(i-1, i+2):
            for p in range(j-1, j+2):
                if 0 > v or v > h-1:
                    continue
                if 0 > p or p > w-1:
                    continue
                if sl[v][p] == "
                    ans += 1
        al_l[i][j] = str(ans)

for al in al_l:
    print("".join(al))

Solution (Python):
import numpy as np
h,w = map(int, input().split())
a = [list(input()) for i in range(h)]
a = np.array(a)
for hi in range(h):
    for wi in range(w):
        c = 0
        if a[hi][wi] == "
            print('
        else:
            print(np.count_nonzero(a[max(0,hi-1):hi+2,max(0,wi-1):wi+2] == "
    print()

Solution (Python):
h,w=map(int,input().split())
l=list(map(list,[input() for i in range(h)]))
for i in range(h):
    for j in range(w):
        if l[i][j]=="
            continue
        num=0

        if i!=0 and j!=0 and l[i-1][j-1]=="
            num+=1
        if i!=0 and l[i-1][j]=="
            num+=1
        if i!=0 and j!=w-1 and l[i-1][j+1]=="
            num+=1
        if j!=0 and l[i][j-1]=="
            num+=1
        if j!=w-1 and l[i][j+1]=="
            num+=1
        if i!=h-1 and j!=0 and l[i+1][j-1]=="
            num+=1
        if i!=h-1 and l[i+1][j]=="
            num+=1
        if i!=h-1 and j!=w-1 and l[i+1][j+1]=="
            num+=1

        l[i][j]=str(num)
    print("".join(l[i]))

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

bomb=[]

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

for i in range(len(bomb)):
    bomb[i].insert(0,0)
    bomb[i].append(0)

bomb.insert(0,[0 for j in range(W+2)])
bomb.append([0 for j in range(W+2)])

result=[[0 for i in range(W+2)] for j in range(H+2)]

count=0

for i in range(1,H+1):
    for j in range(1,W+1):
        if bomb[i][j]=='
            result[i][j]=-1
            for k in range(i-1,i+2):
                for l in range(j-1,j+2):
                    if(bomb[k][l] != '
                        result[k][l]+=1

for i in range(1,H+1):
    for j in range(1,W+1):
        if result[i][j]==(-1):
            print("
        else:
            print(result[i][j],end="")
    print("")

Solution (Python):
H , W = map(int,input().split())
stage = list(input() for _ in range(H))
dy_list = [1,1,0,-1,-1,-1,0,1]
dx_list = [0,1,1,1,0,-1,-1,-1]

for y in range(H):
    for x in range(W):
        cnt = 0
        Flag = False
        for dx , dy in zip(dy_list , dx_list):
            if stage[y][x] == "
                break
            Flag = True
            if 0 <= y + dy < H and 0 <= x + dx < W:
                if stage[y + dy][x + dx] == "
                    cnt += 1
        if Flag:
            print(cnt , end = "")
        else:
            print("
    print()

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

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


for i in range(h):
    for j in range(w):
        if ls_s[i][j] == "
            ls_ans[i][j] += 1
            ls_ans[i][j+1] += 1
            ls_ans[i][j+2] += 1
            ls_ans[i+1][j] += 1
            ls_ans[i+1][j+2] += 1
            ls_ans[i+2][j] += 1
            ls_ans[i+2][j+1] += 1
            ls_ans[i+2][j+2] += 1

for i in range(h):
    for j in range(w):
        if ls_s[i][j] == "
            ls_ans[i+1][j+1] = "
            
del(ls_ans[0])
del(ls_ans[h])

for k in range(h):
    ls_ans[k] = ls_ans[k][1:w+1]
    
for a in ls_ans:
    print(*a, sep="")

Solution (Python):
H, W = map(int, input().split())
matrix = [input() for i in range(H)] 
ans = [[0 for j in range(W)] for i in range(H)] 
dy = [-1, -1, -1, 0, 0, 1, 1, 1]
dx = [-1, 0, 1, -1, 1, -1, 0, 1]
bomb = []
for h in range(H):
        for w in range(W):
                if matrix[h][w] == "
                        bomb.append([h, w]) 
                        for i in range(8):
                                if H -1 < h + dy[i] or h + dy[i] < 0:
                                        continue
                                if W - 1 < w + dx[i] or w + dx[i] < 0:
                                        continue
                                ans[h + dy[i]][w + dx[i]] += 1
for i in bomb:
        ans[i[0]][i[1]] = "
for i in ans:
        l = map(str, i)
        print("".join(l))

Solution (Python):
H, W = map(int, input().split())
matrix = [input() for i in range(H)] 
ans = ""  
for h in range(H):
        for w in range(W):
                bomb = 0 
                for i in range(-1, 2): 
                        if 0 <= h - 1 and 0 <= w + i < W:
                                if matrix[h - 1][w + i] == "
                                        bomb += 1
                        if h + 1 < H and 0 <= w + i < W:
                                if matrix[h + 1][w + i] == "
                                        bomb += 1
                for i in range(-1, 2, 2): 
                        if 0 <= w + i < W:
                                if matrix[h][w + i] == "
                                        bomb += 1
                if matrix[h][w] == "
                        ans += "
                else:
                        ans += str(bomb)
                if W - 1 > 0:
                        if w % (W - 1) == 0 and w != 0:
                                ans += "\n"
                else:
                                ans += "\n"
print(ans)

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

output = [''] * h
for i in range(h):
    for j in range(w):
        if lst[i][j] == '.':
            output[i] += str(nlst[i + 1][j + 1])
        else:
            output[i] += '
print(*output, sep='\n')

Solution (Python):
HW = list(map(int,input().split()))
H=HW[0]
W=HW[1]
S = [list(input()) for i in range(H)]
cnt=0
for i in range(H):
    for j in range(W):
            if S[i][j]=='
                continue
            try:
                if S[i-1][j-1]=='
                    cnt=cnt+1            
            except:
                None
                          
            try:
                if S[i-1][j]=='
                    cnt=cnt+1
            except:
                None
            try:
                if S[i-1][j+1]=='
                    cnt=cnt+1
            except:
                None
            try:
                if S[i][j-1]=='
                    cnt=cnt+1
            except:
                None
                
            try:
                if S[i][j+1]=='
                    cnt=cnt+1
            except:
                None
            try:
                if S[i+1][j-1]=='
                    cnt=cnt+1
            except:
                None
            try:
                if S[i+1][j]=='
                    cnt=cnt+1
            except:
                None
            try:
                if S[i+1][j+1]=='
                    cnt=cnt+1
            except:
                None
            S[i][j]=cnt
            cnt=0

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

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

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

def up_left(s, x, y):
  if x - 1 < 0 or y - 1 < 0:
    return 0
  return 1 if s[y - 1][x - 1] == '

def up(s, x, y):
  if y - 1 < 0:
    return 0
  return 1 if s[y - 1][x] == '

def up_right(s, x, y, w):
  if y - 1 < 0 or x + 1 >= w:
    return 0
  return 1 if s[y - 1][x + 1] == '

def left(s, x, y):
  if x - 1 < 0:
    return 0
  if s[y][x-1] == '
    return 1
  return 0

def right(s, x, y, w):
  if x + 1 >= w:
    return 0
  return 1 if s[y][x + 1] == '

def down_left(s, x, y, h):
  if y + 1 >= h or x - 1 < 0:
    return 0
  return 1 if s[y + 1][x - 1] == '

def down(s, x, y, h):
  if y + 1 >= h:
    return 0
  return 1 if s[y + 1][x] == '

def down_right(s, x, y, h, w):
  if y + 1 >= h or x + 1 >= w:
    return 0
  return 1 if s[y + 1][x + 1] == '
  
rr = []
for y_i, sy in enumerate(s):
  r = []
  for x_i, x in enumerate(sy):
    bomb_count = 0
    if x == '
      r.append(x)
    else:
      
      bomb_count += up_left(s, x_i, y_i)
      bomb_count += up(s, x_i, y_i)
      bomb_count += up_right(s, x_i, y_i, w)
      bomb_count += left(s, x_i, y_i)
      bomb_count += right(s, x_i, y_i, w)
      bomb_count += down_left(s, x_i, y_i, h)
      bomb_count += down(s, x_i, y_i, h)
      bomb_count += down_right(s, x_i, y_i, h, w)
      r.append(str(bomb_count))
  rr.append(r)
    
for ri in rr:
  print(''.join(ri))

Solution (Python):
from itertools import product

H, W = list(map(int, input().split()))
S = [[0]*(W+2)] + [[0] + list(input()) + [0] for _ in range(H)] + [[0]*(W+2)]
D = list(product([-1,0,1], repeat=2))

for h, w in product(range(1,H+1), range(1,W+1)):
    if S[h][w]!='.' : continue
    n = 0
    for dx, dy in D:
        if S[h+dy][w+dx]=='
    S[h][w] = str(n)

for s in S[1:-1]:
    print(''.join(s[1:-1]))

Solution (Python):

import sys
if sys.platform =='ios':
    sys.stdin=open('input_file.txt')

def mine(s,i,j):
	
	ans = 0
	t = s[i-1][j-1]
	if t == '*' or t == '.':
		ans = ans + 0
	else:
		ans = ans + 1

	t = s[i-1][j]
	if t == '*' or t == '.':
		ans = ans + 0
	else:
		ans = ans + 1

	t = s[i-1][j+1]
	if t == '*' or t == '.':
		ans = ans + 0
	else:
		ans = ans + 1

	t = s[i][j-1]
	if t == '*' or t == '.':
		ans = ans + 0
	else:
		ans = ans + 1

	t = s[i][j+1]
	if t == '*' or t == '.':
		ans = ans + 0
	else:
		ans = ans + 1

	t = s[i+1][j-1]
	if t == '*' or t == '.':
		ans = ans + 0
	else:
		ans = ans + 1

	t = s[i+1][j]
	if t == '*' or t == '.':
		ans = ans + 0
	else:
		ans = ans + 1

	t = s[i+1][j+1]
	if t == '*' or t == '.':
		ans = ans + 0
	else:
		ans = ans + 1	

	return ans


h,w = map(int,input().split())
s = [str('*'+input())+'*' for i in range(h)]

test = ''
for i in range(w+2):
	test = test + '*' 


s.insert( 0,test)


s.insert(len(s),test)


s2 = [[0] * w for i in range(h)]
						
for i in range(1,h+1):
	for j in range(1,w+1):
		if s[i][j]=='*':
			pass
		elif s[i][j]=='
			s2[i-1][j-1]='
		else:
			s2[i-1][j-1] = str(mine(s,i,j))

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

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

Solution (Python):
h,w=map(int,input().split())
slis=[]
a=["."]*(w+2)
slis.append(a)
for i in range(h):
	s=list("."+input()+".")
	slis.append(s)
slis.append(a)
for i in range(1,h+1):
	for j in range(1,w+1):
		a=slis[i-1][j-1:j+2]+slis[i][j-1:j+2]+slis[i+1][j-1:j+2]
		if slis[i][j] != "
			slis[i][j]=a.count("
for i in range(1,h+1):
	print(*slis[i][1:w+1],sep="")

Solution (Python):
import numpy as np

H,W = map(int,input().split())
S = []
for i in range(H):
    s = list(map(lambda x:x=='
    S.append(s)

S = np.array(S)

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

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


for i in range(H):
    for j in range(W):
        if S[i][j]:
            print('
        else:
            t = 0
            for a in range(-1,2):
                for b in range(-1,2):
                    if 0<=i+a<H and 0<=j+b<W and S[i+a][j+b]:
                        t+=1
            print(t,end='')
    print()

Solution (Python):
h,w = map(int,input().split())
board = [list(input()) for _ in range(h)]
x = (-1,0,1,0,-1,1,-1,1)
y = (0,-1,0,1,-1,-1,1,1)
for i in range(h):
  for j in range(w):
    if board[i][j] == "
      continue
    else:
      cnt = 0
      for k in range(8):
        if 0 > i + y[k] or i + y[k] >= h or 0 > j + x[k] or j + x[k] >= w:
          continue
        else:
          if board[i + y[k]][j + x[k]] == "
            cnt += 1
      board[i][j] = str(cnt)
for i in range(h):
  print("".join(board[i]))

Solution (Python):











import sys
sys.setrecursionlimit(10**6)
input=sys.stdin.readline
from math import floor,ceil,sqrt,factorial,log 
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict,deque
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
from copy import deepcopy
inf=float('inf')
mod = 10**9+7
def pprint(*A): 
    for a in A:     print(*a,sep='\n')
def INT_(n): return int(n)-1
def MI(): return map(int,input().split())
def MF(): return map(float, input().split())
def MI_(): return map(INT_,input().split())
def LI(): return list(MI())
def LI_(): return [int(x) - 1 for x in input().split()]
def LF(): return list(MF())
def LIN(n:int): return [I() for _ in range(n)]
def LLIN(n: int): return [LI() for _ in range(n)]
def LLIN_(n: int): return [LI_() for _ in range(n)]
def LLI(): return [list(map(int, l.split() )) for l in input()]
def I(): return int(input())
def F(): return float(input())
def ST(): return input().replace('\n', '')
def main():
    H,W = MI()
    grid = [":"*(W+2)] + [":"+ST()+":" for _ in range(H)] + [":"*(W+2)]
    
    for i in range(1,H+1):
        for j in range(1,W+1):
            
            if grid[i][j] == ".":
                cnt=0
                for dx in range(-1,2):
                    for dy in range(-1,2):
                        cnt += grid[i+dy][j+dx]=="
                print(cnt,end="")
            elif grid[i][j] == "
                print("

        print()
if __name__ == '__main__':
    main()