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())
array = [[0]*w for i in range(h)]

for i in range(h):
  s = input()
  for j in range(w):
    if s[j] == "
      if i-1>=0 and j-1>=0:
        array[i-1][j-1] += 1
        
      if i-1>=0:
        array[i-1][j] += 1
        
      if i-1>=0 and j+1 <= w-1:
        array[i-1][j+1] += 1
        
      if j-1>=0:
        array[i][j-1] += 1
        
      array[i][j] = 9
      
      if j+1 <= w-1:
        array[i][j+1] += 1
        
      if i+1 <= h-1 and j-1>=0:
        array[i+1][j-1] += 1
        
      if i+1 <= h-1:
        array[i+1][j] += 1
      if i+1 <= h-1 and j+1 <=w-1:
        array[i+1][j+1] += 1

for i in range(h):
  for j in range(w):
    if array[i][j] >= 9:
      array[i][j] = "
    else:
      array[i][j] = str(array[i][j])
for i in range(h):
  print("".join(array[i]))

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

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

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):
def nyu():
    H,W = map(int,input().split())
    S = [list(input()) for i in range(H)] 
    return S,H,W

def check(S,H,W):

    for h in range(H):
        for w in range(W):
            if S[h][w] != "
                if h !=0 :
                    if S[h-1][w] == "
                        S[h][w] +=1
                if h !=H-1 :
                    if S[h+1][w] == "
                        S[h][w] +=1
                if w !=0 :
                    if S[h][w-1] == "
                        S[h][w] +=1
                if w !=W-1 :
                    if S[h][w+1] == "
                        S[h][w] +=1

                if h !=H-1  and w !=W-1:                    
                    if S[h+1][w+1] == "
                        S[h][w] +=1
                if h != 0  and w !=W-1:                    
                    if S[h-1][w+1] == "
                        S[h][w] +=1

                if h !=H-1  and w !=0:                    
                    if S[h+1][w-1] == "
                        S[h][w] +=1

                if h != 0  and w !=0:                    
                    if S[h-1][w-1] == "
                        S[h][w] +=1

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

def convert(S,H,W):
    
    for h in range(H):
        for w in range(W):
            if S[h][w]==".":
                S[h][w] = 0
    return S

S,H,W = nyu()
S =convert(S,H,W)
check(S,H,W)

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

res = []

for i in range(H):
    r = []
    for j in range(W):
        if S[i][j] == '
            r.append('
        else:
            count = 0
            for d in [(-1, 1), (0, 1), (1, 1), (-1, 0), (0, 0), (1, 0), (-1, -1), (0, -1), (1, -1)]:
                x = i+d[0]
                y = j+d[1]

                if (0 <= x < H) and (0 <= y < W) and S[x][y] == '
                    count += 1
            r.append(count)
    res.append(r)

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

Solution (Python):
h,w = map(int,input().split())
s = [input() for i in range(h)]
grid = [[0 for i in range(w+2)] for j in range(h+2)]
grid2 = [[0 for i in range(w)] for j in range(h)]
for i in range(h):
    for j in range(w):
        if s[i][j] == '
            grid[i+1][j+1] = 1
for i in range(h):
    for j in range(w):
        if grid[i+1][j+1] == 0:
            grid2[i][j] += sum(grid[i][j:j+3]) +sum(grid[i+1][j:j+3]) + sum(grid[i+2][j:j+3])
            grid2[i][j] = str(grid2[i][j])
        else:
            grid2[i][j] = '
    print(''.join(grid2[i]))

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):
        if S[i][j] == '
            continue
        cnt = 0
        for dx in [-1, 0, 1]:
            for dy in [-1, 0, 1]:
                if not 0 <= i+dy <h  or not 0 <= j+dx < w:
                    continue
                if S[i+dy][j+dx] == '
                    cnt += 1
        S[i][j] = str(cnt)
for s in S:
    print("".join(s))

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):
        if S[i][j] == '
            continue
        cnt = 0
        for dx in [-1, 0, 1]:
            for dy in [-1, 0, 1]:
                if not 0 <= i+dy <h  or not 0 <= j+dx < w:
                    continue
                if S[i+dy][j+dx] == '
                    cnt += 1
        S[i][j] = str(cnt)
for s in S:
    print("".join(s))

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


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
            print(count, end="")
        else:
            print("
    print()

Solution (Python):
h, w = map(int, input().split())
map = a = [ list(input()) for _ in range(h) ]
d = [[1, 0], [0, 1], [1, 1], [-1, 0], [0, -1], [-1, -1], [1, -1], [-1, 1]]

for i in range(h):
    for j in range(w):
        cnt = 0
        if map[i][j] == ".":
            for dx, dy in d:
                nx = j + dx
                ny = i + dy
                if 0 <= nx < w and 0 <= ny < h and map[ny][nx] == "
                    cnt += 1
        if map[i][j] != "
            map[i][j] = cnt

for i in range(h):
    print(*map[i],sep="")

Solution (Python):
h, w = map(int, input().split())
s = []
d = [[-1,-1], [-1,0], [-1,1], [0,-1], [0,1], [1,-1], [1,0], [1,1]]

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

for y in range(h):
    for x in range(w):
        c = 0
        if s[y][x] == "

        for u in d:
            if x + u[0] < 0 or x + u[0] > w-1 or y + u[1] < 0 or y + u[1] > h -1: continue
            if s[y + u[1]][x + u[0]] == "

        s[y][x] = c

for i in s:
    print("".join(list(map(str,i))))

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

for i in range(H):
    for j in range(W):
        if S[i][j] == "
            RES[i][j] = "
            for n in range(-1, 2):
                for m in range(-1, 2):
                    if not(n == 0 and m == 0) and 0 <= i + n and i + n < H and 0 <= j + m and j + m < W:
                        if RES[i + n][j + m] != "
                            RES[i + n][j + m] = str(int(RES[i + n][j + m]) + 1)

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

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

for i in range(H):
    for j in range(W):
        if S[i][j] == "
            RES[i][j] = "
            for n in range(-1, 2):
                for m in range(-1, 2):
                    if not(n == 0 and m == 0) and 0 <= i + n and i + n < H and 0 <= j + m and j + m < W:
                        if RES[i + n][j + m] != "
                            RES[i + n][j + m] = str(int(RES[i + n][j + m]) + 1)

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

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

for h in range(H):
    for w in range(W):
        cnt = 0
        if s[h][w] != "
            for dh, dw in ((1, 0), (-1, 0), (0, 1), (0, -1), (1, 1), (-1, -1), (1, -1), (-1, 1)):
                next_h, next_w = h + dh, w + dw
                if next_h < 0 or next_h >= H or next_w < 0 or next_w >= W:
                    continue
                elif s[next_h][next_w] == "
                    cnt += 1
            s[h][w] = str(cnt
                          )
for i in s:
    print("".join(i))

Solution (Python):
from collections import deque
h,w=map(int,input().split())
s=deque([deque(input()) for _ in range(h)])
s.appendleft(deque([0]*w))
s.append(deque([0]*w))
s=list(s)
for i in range(h+2):
  s[i].appendleft(0)
  s[i].append(0)
  s[i]=[0 if j==0 or j=='.' else '
for i in range(1,h+1):
  for j in range(1,w+1):
    if s[i][j]=='
      for a in range(3):
        for b in range(3):
          x=s[i-1+a][j-1+b]
          x=x+1 if type(x)==int else '
          s[i-1+a][j-1+b]=x
for i in range(1,h+1):
  print(''.join([str(j) for j in s[i]][1:-1]))

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

dh = [1,1,0,-1,-1,-1,0,1]
dw = [0,1,1,1,0,-1,-1,-1]

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


for H in range(h):
    for W in range(w):
        cnt = 0
        for i in range(8):
            if 0<=H+dh[i]<=h-1 and 0<=W+dw[i]<=w-1:
                if s[H+dh[i]][W+dw[i]] =='
                    cnt += 1
        if s[H][W] == '.':
            s[H] = s[H][:W] + str(cnt) + s[H][W+1:]

for c in s:
    print(c)

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

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

def top(i,j):
    ans=0
    if i==0:
        pass
    else:
        check=masu[i-1][max(0,j-1):min(w,j+2)]
        ans+=check.count("
    return ans

def bottom(i,j):
    ans=0
    if i==h-1:
        pass
    else:
        check=masu[i+1][max(0,j-1):min(w,j+2)]
        ans+=check.count("
    return ans

def side(i,j):
    ans=0
    check=masu[i][max(0,j-1):min(w,j+2)]
    ans+=check.count("
    return ans
    
def mawari(i,j):
    taishou=masu[i][j]
    if taishou==".":
        return str(top(i,j)+bottom(i,j)+side(i,j))
    else:
        return "

answer=[]
for i in range(h):
    gyou=[]
    for j in range(w):
        gyou.append(mawari(i,j))
    answer.append("".join(gyou))
    
for i in answer:
    print(i)

Solution (Python):
dh=[1,1,0,-1,-1,-1,0,1]
dw=[0,1,1,1,0,-1,-1,-1]
h,w=map(int, input().split())
s=[]
for _ in range(h):
    s.append(str(input()))

for H in range(h):
    for W in range(w):
        cnt=0
        for i in range(8):
            if 0<=H+dh[i]<=h-1 and 0<=W+dw[i]<=w-1:
                if s[H+dh[i]][W+dw[i]]=='
                    cnt+=1
        if s[H][W]!='
            s[H]=s[H][:W]+str(cnt)+s[H][W+1:]
for c in s:
    print(c)

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

l = []
l.append(["."]*(W+2))
for h in range(H):
  l.append(["."] + list(input()) + ["."])
l.append(["."]*(W+2))

ans = [[0 for w in range(W+2)] for h in range(H+2)]

for h in range(1,H+1):
  for w in range(1,W+1):
    if l[h][w] == "
      ans[h+1][w] += 1
      ans[h-1][w] += 1
      ans[h][w+1] += 1
      ans[h][w-1] += 1
      ans[h+1][w+1] += 1
      ans[h-1][w-1] += 1
      ans[h+1][w-1] += 1
      ans[h-1][w+1] += 1
      
for h in range(1,H+1):
  a = []
  for w in range(1,W+1):
    if l[h][w] == "
      a.append("
    else:
      a.append(str(ans[h][w]))
  print("".join(a))

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

for _ in range(H):
    row = list(input())
    for i in range(W):
        if row[i] == ".":
            row[i] = 0
    matrix.append(row)

for i in range(H):
    for j in range(W):
        if matrix[i][j] == "
            for k in [-1, 0, 1]:
                for l in [-1, 0, 1]:
                    if i+k < 0 or i+k == H or j+l < 0 or j+l == W:
                        continue
                    try:
                        matrix[i+k][j+l] += 1
                    except:
                        pass

for row in matrix:
    row = [str(x) for x in row]
    print("".join(row))

Solution (Python):


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

S_list = []

for h in range(H):
    S = list(input())
    S_list.append(S)


check_list = [[-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 S_list[i][j] == '.':
            count = 0
            for k in check_list:
                k_i = i - k[1]
                k_j = j - k[0]
                if k_i >= 0 and k_j >= 0 and k_i < H and k_j < W:
                    if S_list[k_i][k_j] == '
                        count += 1
            
            S_list[i][j] = str(count)

    print(''.join(S_list[i]))

Solution (Python):
def solve():
    H, W = map(int, input().split())
    S = [list(input()) for i in range(H)]
    c = 0
    lis = [[0]*W for i in range(H)]
    for i in range(H):
        for j in range(W):
            c = 0

            if S[i][j] == '
                lis[i][j] = '
                continue
            else:
                for k in range(-1, 2):
                    for l in range(-1, 2):
                        if k == 0 and l == 0:
                            pass
                        elif judge(i + k, j + l, W, H, S) == True:
                            c += 1
                if c == 0:
                    lis[i][j]= '0'
                else:
                    lis[i][j] = str(c)


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


def judge(x, y, W, H, S):
    if (x < 0 or x > H - 1 or y < 0 or y >W - 1) == True:
        return False
    else:
        if S[x][y] == '
            return True
        else:
            return False

def main():
    solve()


if __name__ == '__main__':
    main()

Solution (Python):
H,W = map(int, input().split())
S = []
result=[]
for n in range(H):
    a = list(input())
    a.insert(0,".")
    a.append(".")
    S.append(a)
a = ["."]*(W+2)
S.insert(0,a)
S.append(a)
result=[]


for h in range(H):
    result.append("")
    for w in range(W):
        if S[h+1][w+1]=="
            result[h] = result[h] + "
        else:
            sum=0
            for i in range(3):
                for j in range(3):
                    if S[h+i][w+j]=="
                        sum=sum+1
            sum = sum
            result[h] = result[h] + str(sum)

for h in range(H):
    print(result[h])

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

dx=[1,1,0,-1,-1,-1,0,1]
dy=[0,1,1,1,0,-1,-1,-1]
minesweeper=[['
for y in range(H):
    for x in range(W):
        if S[y][x]=='.':
            cnt=0
            for i in range(8):
                if 0<=y+dy[i]<H and 0<=x+dx[i]<W:
                    if S[y+dy[i]][x+dx[i]] =='
                        cnt+=1
            minesweeper[y][x]=str(cnt)

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

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

    dx = (-1,0,1)
    dy = (-1,0,1)
    d_pattern = []
    for x in dx:
        for y in dy:
            if x == 0 and y == 0:
                continue
            d_pattern.append([y,x])

    for h in range(H):
        line = []
        for w in range(W):
            if S[h][w] == '
                line.append('
                continue

            cnt = 0
            for y, x in d_pattern:
                if 0 <= y+h < H and 0 <= x+w < W:
                    if S[y+h][x+w] == '
                        cnt += 1
            line.append(str(cnt))
        ans.append("".join(line))
    
    print(*ans, sep='\n')

if __name__ == '__main__':
    solve()

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

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

for i in range(m):
    for j in range(n):
        if l[i][j]=="
            
            continue
        elif l[i][j]==".":
            cnt=0
            for k in range(8):
                x=dx[k]+i
                y=dy[k]+j
                if (x<0 or x>=m or y<0 or y>=n):continue
                elif l[x][y]=="
                    cnt+=1
            l[i][j]=str(cnt)
for i in range(m):
    print("".join(l[i]))

Solution (Python):












mod = int(1e9+7)
INF = 1<<29
lINF = 1<<35

def main():
    h,w = map(int,input().split())
    s = []
    for i in range(h):
        s.append(list(input()))
    ans = [[0 for i in range(w+2)] for j in range(h+2)]
    for i in range(h):
        for j in range(w):
            if s[i][j]=="
                ans[i][j] += 1
                ans[i+1][j] += 1
                ans[i+2][j] += 1
                ans[i][j+1] += 1
                ans[i+2][j+1] += 1
                ans[i][j+2] += 1
                ans[i+1][j+2] += 1
                ans[i+2][j+2] += 1
    for i in range(h):
        for j in range(w):
            if s[i][j]=="
                ans[i+1][j+1] = "
            else:
                ans[i+1][j+1] = str(ans[i+1][j+1])
    for i in range(1,h+1):
        print(''.join(ans[i][1:w+1]))
    return

if __name__=='__main__':
    main()

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

A = [[0 for _ in range(W)] for _ in range(H)]
for i in range(H):
  for j in range(W):
    if S[i][j] == '
      A[i][j] = '
    else:
      c =0
      for k in [-1,0,1]:
        for l in [-1,0,1]:
          if i+k < 0 or i+k >= H or j+l < 0 or j+l >= W or (k == 0 and l == 0):
            continue
          if S[i+k][j+l] == '
            c += 1
      A[i][j] = c
for a in A:
  print(*a,sep='')

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

ans = [[None for j in range(w+2)] for i in range(h+2)]

for i in range(1, h+1):
  for j in range(1, w+1):
    if grid[i][j] == '
      ans[i][j] = '
      continue
    else:
      tmp = 0
      for k in range(i-1, i+2):
        tmp += grid[k][j-1:j+2].count('
      ans[i][j] = tmp

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

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

for i in range(H):
  for j in range(W):
    if list[i][j] == '
      try:
        ans[i][j] = ''
        ans[i][j] = '
      except:
        pass
      try:
        if i-1 == -1 or j-1 == -1:
          pass
        else:
          ans[i-1][j-1] += 1
      except:
        pass
      try:
        if i-1 == -1:
          pass
        else:
          ans[i-1][j] += 1
      except:
        pass
      try:
        if i-1 == -1:
          pass
        else:
          ans[i-1][j+1] += 1
      except:
        pass
      try:
        if j-1 == -1:
          pass
        else:
          ans[i][j-1] += 1
      except:
        pass
      try:
        ans[i][j+1] += 1
      except:
        pass
      try:
        if j-1 == -1:
          pass
        else:
          ans[i+1][j-1] += 1
      except:
        pass
      try:
        ans[i+1][j] += 1
      except:
        pass
      try:
        ans[i+1][j+1] += 1
      except:
        pass  
      
for i in range(H):
  line = ''
  for j in range(W):
    line += str(ans[i][j])
  print(line)

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

def check(i, j):
    ans = 0
    for k in [-1, 0, 1]:
        for l in [-1, 0, 1]:
            if k == 0 and l == 0:
                continue
            elif 0 <= i+k < H and 0 <= j+l < W:
                if tizu[i+k][j+l] == "
                    ans += 1
    return ans

for a in range(H):
    for b in range(W):
        if tizu[a][b] == "
            continue
        else:
            tizu[a][b] = check(a, b)

for i in range(H):
    for j in range(W):
        print(tizu[i][j], end="")
    print()

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

grid = [list(input()) for _ in range(H)]
num = [[0] * W for _ in range(H)]

for i in range(H):
    for j in range(W):
        if grid[i][j] == '
            for di in [-1, 0, 1]:
                for dj in [-1, 0, 1]:
                    ni = i + di
                    nj = j + dj
                    if 0 <= ni < H and 0 <= nj < W:
                        if grid[ni][nj] == '
                            num[ni][nj] = '
                        else:
                            num[ni][nj] += 1

for i in range(H):
    print(*num[i], sep='')

Solution (Python):
import sys

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

s = [list(map(str, input())) for i in range(h)]

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

for i in range(h):
    for j in range(w):
        if s[i][j] == '
            continue
        num = 0
        for k in range(8):
            ni = i + dy[k]
            nj = j + dx[k]

            if ni < 0 or ni >= h:
                continue
            if nj < 0 or nj >= w:
                continue
            if s[ni][nj] == '
                num += 1
        
        s[i][j] = str(num)

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

Solution (Python):
H,W = map(int,input().split())
SS = []
for i in range(H):
    SS.append(list(input()))
    
def seekindices(i,j,H,W):
    seeks = []
    for x in [-1,0,1]:
        for y in [-1,0,1]:
            if i + x >= 0 and i + x < H:
                if j + y >= 0 and j + y < W:
                    seeks.append((i+x,j+y))
    return(seeks)

AS = []

for h in range(H):
    s = SS[h]
    
    a = []
    
    for w in range(W):
        if s[w] == '.':
            seeks = seekindices(h,w,H,W)
            count = 0
            for zahyo in seeks:

                if SS[zahyo[0]][zahyo[1]] == '
                    count += 1
            a.append(str(count))
        else:
            a.append(s[w])
    AS.append(a)
                    
for a in AS:
    print("".join(a))

Solution (Python):
h,w = map(int,input().split())
S = [input() for i in range(h)]
for i in range(h):
    for j in range(w):
        if S[i][j] == '
            print('
            continue
        cnt = 0
        for dx in [-1,0,1]:
            for dy in [-1,0,1]:
                if not 0 <= i+dx < h or not 0 <= j+dy < w: continue
                if S[i+dx][j+dy] == '
        print(cnt,end='')
    print()

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

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

for i in range(1,h+1):
    for j in range(1,w+1):
        if data[i][j] == '
            print('
            continue
        cnt = 0
        for k in range(8):
            if data[i+dx[k]][j+dy[k]] == '
        print(cnt,end='')
    print()

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

for i in range(H):
  S[i] = ["."] + S[i] + ["."]
S = [["."]*(W+2)] + S + [["."]*(W+2)]
ans = [[0]*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] = "
      continue
    cnt = 0
    for k in range(i-1, i+2):
      for l in range(j-1, j+2):
        if S[k][l] == "
          cnt += 1
    ans[i-1][j-1] = str(cnt)
for a in ans:
  print("".join(a))

Solution (Python):
h, w = map(int, input().split())
s = [list(input()) for i in range(h)]
S2 = [[0] * (w+2) for i in range(h+2)]
for i in range(h):
    for j in range(w):
        if s[i][j] == "
            S2[i+1][j+1] = 1
for i in range(h):
    for j in range(w):
        if s[i][j] == ".":
            s[i][j] = sum(S2[i][j:j+3]) + sum(S2[i+1][j:j+3]) + sum(S2[i+2][j:j+3])
    print(''.join(map(str, s[i])))

Solution (Python):
H, W = map(int, input().split())
mine = []
dx = [-1, 0, 1, 1, 1, 0, -1, -1]
dy = [-1, -1, -1, 0, 1, 1 ,1, 0]
for i in range(H):
    s_list = list(input())
    mine.append(s_list)
for i in range(H):
    for j in range(W):
        if mine[i][j] == "
            continue
        num = 0
        for k in range(8):
            nx = i + dx[k]
            ny = j + dy[k]
            if nx<0 or nx>=H or ny < 0 or ny >= W:
                continue
            else:
                if mine[nx][ny]=="
                    num += 1
        mine[i][j]=str(num)
for i in mine:
    print("".join(i))

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

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

for y in range(h):
    for x in range(w):
        if (map_data[y][x] == "."):
            bs = 0
            for yy in range(max(0, y - 1), min(h, y + 2)):
                for xx in range(max(0, x - 1),min(w, x + 2)):
                    bs += map_data[yy][xx] == "
            map_data[y][x] = str(bs)

for y in range(h):
    print("".join(map_data[y]))

Solution (Python):
n,m= map(int, input().split())
a=[list(input()) for i in range(n)]
b=0
for i in range(n):
    for j in range(m):
        if a[i][j]==".":
            if n-1>=i+1 and j-1>=0 and a[i+1][j-1]=='
                b+=1
            if j-1>=0 and a[i][j-1]=='
                b+=1
            if i-1>=0 and j-1>=0 and a[i-1][j-1]=='
                b+=1
            if i-1>=0 and a[i-1][j]=='
                b+=1
            if i-1>=0 and m-1>=j+1 and a[i-1][j+1]=='
                b+=1
            if m-1>=j+1 and a[i][j+1]=='
                b+=1
            if n-1>=i+1 and m-1>=j+1 and a[i+1][j+1]=='
                b+=1
            if n-1>=i+1 and a[i+1][j]=='
                b+=1
            a[i][j]=b
            b=0
        print(a[i][j], end="")
    print()

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

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

for h in range(1, H+1):
    for w in range(1, W+1):
        if S[h][w] == '.':
            cnt = 0
            for dy, dx in table:
                if S[h+dy][w+dx] == '
                    cnt += 1
            S[h] = S[h][:w] + str(cnt) + S[h][w+1:]

for h in range(1, H+1):
    print(S[h][1:-1])

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

ans = []

for row in mass:
  ans.append(list(row))
  
dxy = [(-1,-1),(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1)]

for y in range(h):
  for x in range(w):
    if mass[y][x] == '
      continue
    c = 0
    for dx,dy in dxy:
      if x + dx < 0 or x + dx > w - 1 or y + dy < 0 or y + dy > h - 1:
        continue
      if mass[y + dy][x + dx] == "
        c += 1
    ans[y][x] = c
    
for row in ans:
  print(''.join( list(map(str,row))))

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

mass = [input() for i in range(h)]

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

bomb_pos = []


for i in range(h):
  if mass[i].find("
    continue
  index_num = [n for n, v in enumerate(mass[i]) if v == "
  for index in index_num:
    bomb_pos.append([i,index])

for xy in bomb_pos:
  for i in range(max(0,xy[0]-1),min(xy[0]+2,h)):
      for j in range(max(0,xy[1]-1),min(xy[1]+2,w)):
        ans[i][j] += 1

for xy in bomb_pos:
  ans[xy[0]][xy[1]] = '
  
for arr in ans:
  print(''.join([str(n) for n in arr]))

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

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

        else:
            tmp.append('

    ans.append(tmp)
for row in ans:
    print(''.join(map(str, row)))

Solution (Python):
h,w = map(int,input().split())
S = [['' for i in range(w+2)] for j in range(h+2)]
for i in range(h):
    S[i+1][1:w+1] = input()
for i in range(h):
    for j in range(w):
        if S[i+1][j+1]=='.':
            S[i+1][j+1]=str(sum(S[k+i][j:j+3].count('
    print(*S[i+1][1:w+1],sep='')

Solution (Python):
def f(s):
    if s == "
        return 1
    else:
        return 0

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

for i in range(h):
    for j in range(w):
        if s[i][j] == ".":
            cnt = 0
            for x in [-1, 0, 1]:
                for y in [-1, 0, 1]:
                    if 0 <= i+x < h and 0 <= j+y < w:
                        cnt += f(s[i+x][j+y])

            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            s[i][j] = cnt

for i in range(h):
    for j in range(w):
        print(s[i][j], end="")
    if i != h-1:
        print("\n")