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 = [list(input()) for _ in range(h)]

for i in range(h):
    for j in range(w):
        if s[i][j] == '.':
            s[i][j] = 0
            left = max(0, j - 1)
            right = min(w - 1, j + 1)
            up = max(0, i - 1)
            down = min(h - 1, i + 1)
            for k in range(up, down + 1):
                for l in range(left, right + 1):
                    if s[k][l] == '
                        s[i][j] += 1

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

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

move = [[0,1],[0,-1],[1,0],[-1,0],[1,1],[-1,1],[1,-1],[-1,-1]]
for i in range(h):
    ans = []
    for j in range(w):
        cnt = "
        if board[i][j] == "
            ans.append("
            continue
        cnt = 0
        for mx,my in move:
            x,y = i+mx,j+my
            if 0 > x or x >= h or 0 > y or y >= w:continue
            if board[x][y] == "
                cnt += 1
        ans.append(str(cnt))
    print("".join(ans))

Solution (Python):
from sys import stdin,setrecursionlimit
setrecursionlimit(10 ** 7)

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

li = [[0 for i in range(w)]for j in range(h)]

x = [-1,0,1]
y = [-1,0,1]

for i in range(h):
    for j in range(w):
        count = 0
        for xx in x:
            for yy in y:
                if xx == 0 and yy == 0:
                    continue
                if j+xx < 0 or j+xx > w-1:
                    continue
                if i+yy < 0 or i+yy > h-1:
                    continue
                if s[i+yy][j+xx] == "
                    count += 1
        if s[i][j] == "
            li[i][j] = "
        else:li[i][j] = str(count)

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

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

ans = [[0] * w for _ in range(h)]
for i in range(h):
    for j in range(w):
        if s[i][j] == "
            for x in (-1, 0, 1):
                for y in (-1, 0, 1):
                    if 0 <= i+x < h and 0 <= j+y < w:
                        if s[i+x][j+y] == "
                            ans[i+x][j+y] = "
                        else:
                            ans[i+x][j+y] += 1

for i in ans:
    for j in i:
        print(j, end = "")
    print("\n", end = "")

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

    for i in range(h):
        for j in range(w):
            if s[i][j] == ".":
                mine = 0
                for k in range(max(0, i-1), min(h, i+2)):
                    for l in range(max(0, j-1), min(w, j+2)):
                        if s[k][l] == "
                            mine += 1
                ans[i][j] = str(mine)
    for m in range(h):
        print("".join(ans[m]))

resolve()

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

for s in S:
    for i in range(W):
        if s[i] == ".": s[i] = 0

for i in range(H):
    for j in range(W):
        if S[i][j] == "
            if i > 0:
                if S[i-1][j] != "
                if j > 0 and S[i-1][j-1] != "
                if j < W-1 and S[i-1][j+1] != "
            if i < H-1:
                if S[i+1][j] != "
                if j > 0 and S[i+1][j-1] != "
                if j < W-1 and S[i+1][j+1] != "
            if j > 0 and S[i][j-1] != "
            if j < W-1 and S[i][j+1] != "

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

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

S=[]
for i in range(H):
  s=list(input())
  S.append(s)
D=[(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1),(-1,0),(-1,1)]

ans=[[] for _ in range(H)]
for h in range(H):
  for w in range(W):
    if S[h][w]=="
        ans[h].append("
    else:
      count=0
      for d in D:
        dh,dw=d
        nh=h+dh
        nw=w+dw
        if 0<=nh and nh<H and 0<=nw and nw<W and S[nh][nw]=="
          count+=1
      ans[h].append(str(count))
for A in ans:
  print("".join(A))

Solution (Python):
h,w = map(int,input().split())
vec = [(1,0),(0,1),(-1,0),(0,-1),(-1,-1),(1,1),(-1,1),(1,-1)]
ans = []
s = [input()for _ in range(h)]
def check(x,y):
    if x>=0 and y >=0 and x <=h-1 and y <= w-1:
        return s[x][y] == '
    else:return 0

for x in range(h):
    next = []
    for y in range(w):
        
        if s[x][y] == '
            next.append('
            continue
        res = 0
        for tx,ty in vec:
            nx = x+tx
            ny = y + ty
            res +=check(nx,ny)
        next.append(str(res))
    ans.append(next)
for i in range(h):
    print("".join(ans[i]))

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

for y in range(h):
    for x in range(w):
        if s[y][x]=='.':
            count = 0
            for dy,dx in ((1,0),(1,1),(0,1),(1,-1),(-1,1),(0,-1),(-1,-1),(-1,0)):
                ny,nx = dy+y,dx+x
                if (0<=ny<h and 0<=nx<w and s[ny][nx]=="
                    count += 1
            s[y][x] = str(count)
for i in s:
    print(''.join(i))

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

Solution (Python):
h,w = map(int, input().split())
f = [list(input()) for _ in range(h)]
for k in range(h):
    for l in range(w):
        if f[k][l] == ".":
            c = 0
            for i in range(-1,2):
                for j in range(-1,2):
                    xi,yi = k+i,l+j
                    
                    if 0 <= xi < h  and 0 <= yi < w and f[xi][yi] == "
                        c += 1
            f[k][l] = str(c)

for m in range(h):
    print(''.join(f[m]))

Solution (Python):
def main():
    h, w = map(int, input().split())
    grid = [["." for _ in range(w + 2)]]
    for _ in range(h):
        grid.append(["."] + list(input()) + ["."])
    grid.append(grid[0])
    answer = [["0" for _ in range(w)] for _ in range(h)]
    for i in range(1, h + 1):
        for j in range(1, w + 1):
            now_ans = 0
            if grid[i][j] == ".":
                for k in range(-1, 2):
                    now_ans += ("".join(grid[i + k][j - 1:j + 2])).count("
                answer[i - 1][j - 1] = str(now_ans)
            else:
                answer[i - 1][j - 1] = "
    for ans in answer:
        print("".join(ans))


if __name__ == '__main__':
    main()

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

for i in range(h):
    for j in range(w):
        if s[i][j] == "
            continue
        cnt = 0
        for nxt_i in (i - 1, i, i + 1):
            for nxt_j in (j - 1, j, j + 1):
                if not(0 <= nxt_i < h) or not(0 <= nxt_j < w):
                    continue
                if s[nxt_i][nxt_j] == "
                    cnt += 1
        s[i][j] = str(cnt)

for res in s:
    print("".join(res))

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

def inb(x, y):
    return 0 <= x < h and 0 <= y < w

def count(x, y):
    if board[x][y] == '
        return '
    ret = 0
    for i in [-1, 0, 1]:
        for j in [-1, 0, 1]:
            if i == 0 and j == 0:
                continue
            nx, ny = x + i, y + j
            if not inb(nx, ny):
                continue
            if board[nx][ny] == '
                ret += 1
    return str(ret)

ret = [['?' for _ in range(w)] for _ in range(h)]
for i in range(h):
    for j in range(w):
        ret[i][j] = count(i, j)
for i in ret:
    print(''.join(i))

Solution (Python):

import io
import math

nim, mike = map(int, input().split())
data = [['.'] * (mike+2)]
data += [['.'] + list(input()) + ['.'] for _ in range(nim)]
data += [['.'] * (mike+2)]

result = []
for i in range(1, nim + 1):
  strings = str()
  for j in range(1, mike + 1):
    counter = 0
    if data[i][j] == '.':
      for k in range(-1, 2, 1):
        for l in range(-1, 2, 1):
          if data[i+k][j+l] == '
            counter += 1
      strings += str(counter)
    else:
      strings += '
  result += [strings]
    
for i in result:
  print (i)

Solution (Python):
h, w = map(int, input().split())
sss = [list(input()) for _ in range(h)]
for i in range(h):
    for j in range(w):
        if sss[i][j] == '
            continue
        else:
            cnt = 0
            for x in [-1, 0, 1]:
                for y in [-1, 0, 1]:
                    if 0 <= i + y < h and 0 <= j + x < w:
                        if sss[i + y][j + x] == '
                            cnt += 1

                    sss[i][j] = str(cnt)
for s in sss:
    print(''.join(s))

Solution (Python):
def main():
  from sys import stdin
  input = stdin.readline
  h,w = map(int,input().split())
  s = []
  sa = s.append
  sa(list("."*(w+2)))
  for _ in range(h):
    sa(["."]+list(input().strip())+["."])
  sa(list("."*(w+2)))
  for i in range(1,h+1):
    for j in range(1,w+1):
      if s[i][j] == ".":
        n = 0
        n += s[i-1][j-1:j+2].count("
        n += s[i][j-1:j+2].count("
        n += s[i+1][j-1:j+2].count("
        s[i][j] = str(n)
  for i in range(1,h+1):
    print("".join(s[i][1:w+1]))
main()

Solution (Python):
from sys import stdin
import numpy as np
input = stdin.readline
h,w = map(int,input().split())
s = (np.array([list(input().strip()) for _ in range(h)])=="
ans = np.zeros((h+2,w+2),dtype=np.uint8)
for i,j in [(-1,-1),(-1,0),(-1,1),(0,-1),(0,1),(1,-1),(1,0),(1,1)]:
  ans[1+i:h+1+i,1+j:w+1+j] += s
ans = ans[1:h+1,1:w+1].astype("str")
ans[s] = "
for i in ans:
  print("".join(i))

Solution (Python):

r,c=map(int,input().split())
grid=[[__ for __ in input()] for _ in range(r)]
grid2=[["
def pos(x,y):
    dx = [+0, +0, +1, -1, -1, +1, -1, +1]
    dy = [-1, +1, +0, +0, +1, +1, -1, -1]
    tot=0
    for a,b in zip(dx,dy):
        if 0<=a+x<r and 0<=b+y<c and grid[a+x][b+y]=="
            tot+=1
    return str(tot)
for i in range(r):
    for j in range(c):
        if grid[i][j]=='.':
            grid2[i][j]=pos(i,j)
for __ in grid2:
    print(''.join(__))

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

mp.insert(0, '.'*(W+2))
mp.append('.'*(W+2))

for i in range(1, H+1):
    for j in range(1, W+1):
        if mp[i][j] == '
            mp_num[i-1][j-1] += 1
            mp_num[i-1][j] += 1
            mp_num[i-1][j+1] += 1
            mp_num[i][j-1] += 1
            mp_num[i][j+1] += 1
            mp_num[i+1][j-1] += 1
            mp_num[i+1][j] += 1
            mp_num[i+1][j+1] += 1

for i in range(1, H+1):
    for j in range(1, W+1):
        if mp[i][j] == '
            mp_num[i][j] = '

for i in range(H):
    tmp = [str(a) for a in mp_num[i+1]]
    print(''.join(tmp[1:W+1]))

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

for h in range(H):
  for w in range(W):
    if A[h][w] == "
      print("
    elif A[h][w] == ".":
      c = 0 
      for i in range(-1,2):
        for j in range(-1,2):
          if 0<=h+i<=H-1 and 0<=w+j<=W-1 and A[h+i][w+j]=="
            c+=1
      print(c,end="")
  print("")

Solution (Python):
import numpy as np
import scipy as sp
from scipy import signal

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

B = np.array(A)
B = np.where(B=="
B = np.where(B==".",0,B)
B = B.astype("int32")

s = np.ones(9).reshape(3,3)
s[1][1]=0

C = sp.signal.convolve2d(B,s,"same").astype("int32")

for h in range(H):
  for w in range(W):
    if A[h][w] == "
      print("
    elif A[h][w] == ".":
      print(C[h][w],end="")
  print("")

Solution (Python):
h , w = map(int,input().split())
p = []
for i in range(h+2):
    kar = []
    if i == 0 or i == h+1:
        p.append(["!" for j in range(w+2)])
    else:
        kar = list(input())
        kar.insert(0,"!")
        kar.append("!")
        p.append(kar)
for i in range(1,h+1):
    for j in range(1,w+1):
        cou = 0
        if p[i][j] == "
            print("
        elif p[i][j] ==".":
            for k in range(-1,2):
                for l in range(-1,2):
                    if p[i+k][j+l] == "
                        cou += 1
            print(cou,end="")
    print("")

Solution (Python):
def main():
    H, W = map(int, input().split())
    board = [list(input()) for _ in range(H)]
    for i in range(H):
        for j in range(W):
            if board[i][j] == ".":
                count = 0
                for k in range(-1, 2):
                    for l in range(-1, 2):
                        if 0 <= i + k < H and 0 <= j + l < W:
                            if board[i + k][j + l] == "
                                count += 1
                board[i][j] = str(count)
    for i in range(H):
        print("".join(map(str, board[i])))
main()

Solution (Python):
def main():
    H, W = map(int, input().split())
    board = [list(input()) for _ in range(H)]
    for i in range(H):
        for j in range(W):
            if board[i][j] == ".":
                count = 0
                for k in range(-1, 2):
                    for l in range(-1, 2):
                        if 0 <= i + k < H and 0 <= j + l < W:
                            if board[i + k][j + l] == "
                                count += 1
                board[i][j] = str(count)
    for i in range(H):
        print("".join(map(str, board[i])))
main()

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

dxy=[(-1,-1),(-1,0),(-1,1),(0,-1),(0,1),(1,-1),(1,0),(1,1)]
def bombcount(hi,wi):
  cnt=0
  for x,y in dxy:
    
      
    if 0<=hi+x<=h-1 and 0<=wi+y<=w-1:
      if s[hi+x][wi+y]=="
        
        cnt+=1
  return cnt
      
for i in range(h):
  for j in range(w):
    
    
    if s[i][j]==".":
      s[i][j]=str(bombcount(i,j))
      
for i in range(h):
  print("".join(s[i]))

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






ans = [['






for h in range(H):
    for w in range(W):
        if S[h][w] == '
            continue
        dH = [1, 1, 0, -1, -1, -1, 0, 1]
        dW = [0, 1, 1, 1, 0, -1, -1, -1]
        cnt = 0
        for dh, dw in zip(dH, dW):
            nh = h + dh
            nw = w + dw
            if (nh < 0 or H <= nh or nw < 0 or W <= nw):
                continue
            if S[nh][nw] == '
                cnt += 1
        ans[h][w] = cnt

for h in range(H):
    for w in range(W):
        print(ans[h][w], end='')
    print()

Solution (Python):

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

mines = 0

for row in range(h):
    for column in range(w):
        if s[row][column] == '.':
            if row > 0 and column > 0: 
                mines = mines + 1 if s[row - 1][column -1] == '
            if row > 0: 
                mines = mines + 1 if s[row - 1][column] == '
            if row > 0 and column < w - 1: 
                mines = mines + 1 if s[row - 1][column + 1] == '
            if column > 0: 
                mines = mines + 1 if s[row][column - 1] == '
            if column < w - 1: 
                mines = mines + 1 if s[row][column + 1] == '
            if row < h - 1 and column > 0: 
                mines = mines + 1 if s[row + 1][column - 1] == '
            if row < h -1 : 
                mines = mines + 1 if s[row + 1][column] == '
            if row < h - 1 and column < w - 1: 
                mines = mines + 1 if s[row + 1][column + 1] == '
        
            
            s[row] = s[row][:column] + str(mines) + s[row][column + 1:]
            mines = 0

for row in range(h):
    print(s[row])

Solution (Python):
h,w = map(int,input().split())
field = [list(input())for i in range(h)]
for i in range(h):
    for j in range(w):
        res = 0
        if field[i][j] == '
        if i-1 >=0 and field[i-1][j] == '
        if j-1 >=0 and field[i][j-1] == '
        if i+1 <=h-1 and field[i+1][j] == '
        if j+1 <=w-1 and field[i][j+1] == '
        if i-1 >=0 and j-1 >= 0 and field[i-1][j-1] == '
        if i-1 >=0 and j+1 <= w-1 and field[i-1][j+1] == '
        if i+1 <=h-1 and j-1 >= 0 and field[i+1][j-1] == '
        if i+1 <=h-1 and j+1 <= w-1 and field[i+1][j+1] == '
        field[i][j] = str(res)
for i in range(h):
    print("".join(field[i]))

Solution (Python):
h,w=map(int,input().split())
s=[]
for _ in range(h):
  s.append(input())
ans=[[0]*w for _ in range(h)]
for i in range(h):
  for j in range(w):
    if s[i][j]=='
      ans[i][j]='
      continue
    i0=max(0,i-1)
    i1=min(i+1,h-1)
    j0=max(0,j-1)
    j1=min(j+1,w-1)
    p=0
    for _ in range(i0,i1+1):
      p += s[_][j0:j1+1].count('
    ans[i][j]=p

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

Solution (Python):
from itertools import product
import numpy as np
H, W = map(int, input().split())
S = [list('-' * (W + 2))] + [['-'] + list(input()) + ['-']
                             for _ in range(H)] + [list('-' * (W + 2))]
S = np.array(S, dtype='object')


d = [-1, 0, 1]
dd = list(product(d, repeat=2))
for h in range(1, H + 1):
    for w in range(1, W + 1):
        if S[h, w] == '.':
            cnt = 0
            for x, y in dd:
                nh = h + x
                nw = w + y
                if S[nh, nw] == '
                    cnt += 1
            S[h, w] = cnt

for h in range(1, H + 1):
    for w in range(1, W + 1):
        if S[h, w] != '-':
            print(S[h, w], sep='', end='')
    print('')

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

for i in range(h):
    tmp = list(input())
    
    for j in range(w):
        
        if tmp[j] == "
            
            L[i][j] = "



for i in range(h):
    for j in range(w):
        if L[i][j] == "
            try:
                L[i+1][j] += 1
            except:
                pass
            try:
                if i-1>=0:
                    L[i-1][j] += 1
            except:
                pass
            try:
                L[i][j+1] += 1
            except:
                pass
            try:
                if j-1>=0:
                    L[i][j-1] += 1
            except:
                pass
            try:
                L[i+1][j+1] += 1
            except:
                pass
            try:
                if i-1>=0 and j-1>=0:
                    L[i-1][j-1] += 1
            except:
                pass
            try:
                if i-1>=0:
                    L[i-1][j+1] += 1
            except:
                pass
            try:
                if j-1>=0:
                    L[i+1][j-1] += 1
            except:
                pass

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

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

def bomb_count(hi, wi):
    cnt = 0
    for i in range(-1, 2):
        for j in range(-1, 2):
            if (0 <= (hi + i) <= (H-1)) and (0 <= (wi + j) <= (W-1)):
                if S[hi+i][wi+j] == "
                    cnt += 1
    return cnt


for hi in range(H):
    for wi in range(W):
        masu = S[hi][wi]
        if masu == ".":
            S[hi][wi] = str(bomb_count(hi, wi))

for hi in range(H):
    row = S[hi]
    print("".join(row))

Solution (Python):
h, w = map(int, input().split())
s = [list(input()) for _ in range(h)]
d = [[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):
        cnt = 0
        if s[i][j] == ".":
            for k in d:
                if 0 <= i + k[0] <= h-1 and 0 <= j + k[1] <= w-1:
                    if s[i+k[0]][j+k[1]] == "
                        cnt += 1
        if s[i][j] == "
            continue
        s[i][j] = str(cnt)

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

Solution (Python):
h,w=map(int,input().split())
a = []
a.append([u'はしっこ' for i in range(w+2)])
for i in range(h):
    a.append([u'はしっこ']+list(input())+[u'はしっこ'])
a.append([u'はしっこ' for i in range(w+2)])

for i in range(1,h+1):
    for j in range(1,w+1):
        if a[i][j]=='.':
            p=0
            if a[i-1][j-1]=='
                p+=1
            if a[i-1][j]=='
                p+=1
            if a[i-1][j+1]=='
                p+=1
            if a[i][j-1]=='
                p+=1
            if a[i][j+1]=='
                p+=1
            if a[i+1][j-1]=='
                p+=1
            if a[i+1][j]=='
                p+=1
            if a[i+1][j+1]=='
                p+=1
            a[i][j]=str(p)
for i in range(1,h+1):
    print(''.join(a[i][1:w+1]))

Solution (Python):
import copy
H,W = map(int,input().split())
S = [['.']*(W+2)]
for i in range(H):
    S.append(list('.'+input()+'.'))
S.append(['.']*(W+2))
T = copy.deepcopy(S)
for i in range(1,H+1):
    for j in range(1,W+1):
        if S[i][j] == '
            continue
        s = 0
        for k in [-1,0,1]:
            for l in [-1,0,1]:
                if S[i+k][j+l]=='
                    s += 1
        T[i][j] = s
for i in range(1,H+1):
    print(''.join([str(i) for i in T[i][1:W+1]]))

Solution (Python):

import numpy as np
H,W = list(map(int,input().split()))
data = [input().strip().split() for _ in range(H)]
data_ = np.array([list(str(data[i])) for i in range(H)])[:,2:-2]

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

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

		if data_[y][x] == '
			continue

		count = 0
		for dy,dx in d:
			if 0 <= y+dy < H and 0 <= x+dx < W and data_[y+dy][x+dx] == '
				count += 1

		data_[y][x] = count

[print(''.join(i) ) for i in data_]

Solution (Python):
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):

       count = 0
       if s[i][j] == "
          pass
    
       else :

           if i > 0 and j > 0 and s[i-1][j-1] == "
               count += 1
           
           if i > 0 and s[i-1][j] == "
               count += 1

           if i > 0 and j < W-1 and s[i-1][j+1] == "
               count += 1
           
           if j > 0 and s[i][j-1] == "
               count += 1
           if j < W-1 and s[i][j+1] == "
               count += 1
           if i < H-1 and j > 0 and s[i+1][j-1] == "
               count += 1
           if i < H-1 and s[i+1][j] == "
               count += 1
           if i < H-1 and j < W-1 and s[i+1][j+1] == "
               count += 1
           
           s[i][j] = str(count)    

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

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


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

for i in range(H):    
    for j in range(W):
        if S[i][j] == "
            ans[i][j] = "
            continue    
        counter = 0
        for d in range(8):    
            di = i + dx[d]
            dj = j + dy[d]
            if di<0 or (H-1)<di or dj<0 or W-1<dj:
                continue
            if S[di][dj] == "
                counter += 1
        ans[i][j] = str(counter)
        
for i in range(H):
    ans[i] = "".join(ans[i])
    print(ans[i])

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

def p(i, j):
    if S[i][j] == '
        return 1
    return 0

for i in range(H):
    for j in range(W):
        if S[i][j] == '
            continue
        
        cnt = 0
        if i != 0:
            if j != 0:
                cnt += p(i-1,j-1)
            cnt += p(i-1,j)
            if j != W-1:
                cnt += p(i-1,j+1)

        if j != 0:
            cnt += p(i,j-1)
        if j != W-1:
            cnt += p(i,j+1)
        
        if i != H-1:
            if j != 0:
                cnt += p(i+1,j-1)
            cnt += p(i+1,j)
            if j != W-1:
                cnt += p(i+1,j+1)
        
        S[i][j] = str(cnt)

print('\n'.join([''.join(S[i]) for i in range(H)]))

Solution (Python):
from heapq import heappush,heappop,heapify
from collections import deque,defaultdict,Counter
from itertools import permutations,combinations,groupby
import sys,bisect,string,math,time,functools,random
def Golf():*a,=map(int,open(0))
def I():return int(input())
def S_():return input()
def IS():return input().split()
def LS():return [i for i in input().split()]
def LI():return [int(i) for i in input().split()]
def LI_():return [int(i)-1 for i in input().split()]
def NI(n):return [int(input()) for i in range(n)]
def NI_(n):return [int(input())-1 for i in range(n)]
def StoLI():return [ord(i)-97 for i in input()]
def ItoS(n):return chr(n+97)
def LtoS(ls):return ''.join([chr(i+97) for i in ls])
def GI(V,E,Directed=False,index=0):
    org_inp=[];g=[[] for i in range(n)]
    for i in range(E):
        inp=LI();org_inp.append(inp)
        if index==0:inp[0]-=1;inp[1]-=1
        if len(inp)==2:
            a,b=inp;g[a].append(b)
            if not Directed:g[b].append(a)
        elif len(inp)==3:
            a,b,c=inp;aa=(inp[0],inp[2]);bb=(inp[1],inp[2]);g[a].append(bb)
            if not Directed:g[b].append(aa)
    return g,org_inp
def GGI(h,w,boundary=1,search=[],replacement_of_found='.',mp_def={'

    mp=[boundary]*(w+2);found={}
    for i in range(h):
        s=input()
        for char in search:
            if char in s:
                found[char]=((i+1)*(w+2)+s.index(char)+1)
                mp_def[char]=mp_def[replacement_of_found]
        mp+=[boundary]+[mp_def[j] for j in s]+[boundary]
    mp+=[boundary]*(w+2)
    return h+2,w+2,mp,found
def TI(n):return GI(n,n-1)
def bit_combination(k,n=2):return [[tb//(n**bt)%n for bt in range(k)] for tb in range(n**k)]
def show(*inp,end='\n'):
    if show_flg:print(*inp,end=end)
def show2d(g,h,w):
    for i in range(h):show(g[i*w:i*w+w])

YN=['YES','NO'];Yn=['Yes','No']
mo=10**9+7
inf=float('inf')
l_alp=string.ascii_lowercase

input=lambda: sys.stdin.readline().rstrip()

show_flg=False
show_flg=True

h,w=LI()
h,w,g,_=GGI(h,w,boundary=0)
ans=['']*(h*w)
for i in range(h*w):
    if 0==i//w or i//w==h-1 or i%w==0 or i%w==w-1:
        continue
    if g[i]==1:
        ans[i]='
    else:
        ans[i]=str(sum([g[i+d]for d in (-1,1,w,-w,w-1,-w-1,w+1,-w+1)]))

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

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):
        count = 0
        if s[i][j] == '
            continue
        for n, m in ([-1,1],[-1,0],[-1,-1],[0,1],[0,-1],[1,1],[1,0],[1,-1]):
            if i + n < 0 or i + n >= h or j + m < 0 or j + m >= w:
                continue
            if s[i+n][j+m] == '
                count += 1
        s[i][j] = str(count)

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

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

ans = [[0 for _ in range(W)] for _ in range(H)]
for i in range(H):
    for j in range(W):
        if S[i][j] == "
            ans[i][j] = "
            if j-1 >= 0 and ans[i][j-1] != "
                ans[i][j-1] += 1
            if j-1 >= 0 and i-1 >= 0 and ans[i-1][j-1] != "
                ans[i-1][j-1] += 1
            if j-1 >= 0 and i+1 <= H-1 and ans[i+1][j-1] != "
                ans[i+1][j-1] += 1

            if i-1 >= 0 and ans[i-1][j] != "
                ans[i-1][j] += 1
            if i+1 <= H-1 and ans[i+1][j] != "
                ans[i+1][j] += 1
            
            if j+1 <= W-1 and ans[i][j+1] != "
                ans[i][j+1] += 1
            if j+1 <= W-1 and i-1 >= 0 and ans[i-1][j+1] != "
                ans[i-1][j+1] += 1
            if j+1 <= W-1 and i+1 <= H-1 and ans[i+1][j+1] != "
                ans[i+1][j+1] += 1                        

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

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

for i in range(h):
    for j in range(w):
        if x[i][j] == '.':
            y[i+1][j+1] = 0
        else:
            y[i+1][j+1] = 1

for i in range(h):
    for j in range(w):
        if x[i][j] == '.':
            x[i][j] = str(sum(y[i][j:j+3])+sum(y[i+1][j:j+3])+sum(y[i+2][j:j+3]))
    print(''.join(x[i]))

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

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

ans = [''] * h

for y in range(h):
    for x in range(w):
        if S[y][x] == '
            ans[y] += '
        else:
            b = 0
            for dx,dy in dxy:
                nx, ny = x + dx , y + dy
                if not 0 <= nx < w: continue
                if not 0 <= ny < h: continue
                if S[ny][nx] == '
                    b += 1
            
            ans[y] += str(b)

print(*ans,sep = '\n')