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 i in range(h)]

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

for i in range(h):
    for j in range(w):
        cnt = 0

        if 1 <= i and 1 <= j:
            if s[i-1][j-1] == '
                cnt += 1
        if 1 <= i:
            if s[i-1][j] == '
                cnt += 1
        if 1 <= i and j <= w-2:
            if s[i-1][j+1] == '
                cnt += 1
        if 1 <= j:
            if s[i][j-1] == '
                cnt += 1
        if j <= w-2:
            if s[i][j+1] == '
                cnt += 1
        if i <= h-2 and 1 <= j:
            if s[i+1][j-1] == '
                cnt += 1
        if i <= h-2:
            if s[i+1][j] == '
                cnt += 1
        if i <= h-2 and j <= w-2:
            if s[i+1][j+1] == '
                cnt += 1

        if s[i][j] == '
            s2[i][j] = '
        else:
            s2[i][j] = str(cnt)


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

Solution (Python):
h, w = map(int, input().split())
S = [input() for i in range(h)]
 
for i in range(h):
    l = ''
    for j in range(w):
        if S[i][j] == '
            l += '
        else:
            l += str(sum([T[max(0, j-1) : min(w, j+2)].count('
    print(l)

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

S.insert(0,["."]*(W+2))
S.insert(H+1,["."]*(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-1:j+2]+S[i][j-1:j+2]+S[i+1][j-1:j+2]).count('
    print(''.join(S[i][1:-1]))

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

Solution (Python):
h, w = list(map(int, input().split()))
a = [input() for i in range(h)]
x_ = [-1, -1, -1, 0, 0, 1, 1, 1]
y_ = [-1, 0, 1, -1, 1, -1, 0, 1]
result = [['0' for i in range(w)] for j in range(h)]
for i in range(h):
  for j in range(w):
    count = 0
    if a[i][j] == '
      result[i][j] = '
      continue
    for k in range(8):
      x = i+x_[k]
      y = j+y_[k]
      if x<0 or y<0 or x>=h or y>=w:
        continue
      elif a[x][y] == '
        count += 1
    result[i][j] = str(count)
for i in result:
  print(''.join(i))

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

for i in range(h):
  for j in range(w):
    if l[i][j]=="
      if i>0:
        if l[i-1][j]!="
          l[i-1][j]+=1
        if j>0 and l[i-1][j-1]!="
          l[i-1][j-1]+=1
        if j<w-1 and l[i-1][j+1]!="
          l[i-1][j+1]+=1
      if i<h-1:
        if l[i+1][j]!="
          l[i+1][j]+=1
        if j>0 and l[i+1][j-1]!="
          l[i+1][j-1]+=1
        if j<w-1 and l[i+1][j+1]!="
          l[i+1][j+1]+=1
      if j>0 and l[i][j-1]!="
        l[i][j-1]+=1
      if j<w-1 and l[i][j+1]!="
        l[i][j+1]+=1
for item in l:
  print("".join(map(str,item)))

Solution (Python):
h,w = map(int, input().split())
s = eval('list(input()),' *h)
for i in range(h):
    for j in range(w):
        c = 0
        for a in range(i-1,i+2):
            for b in range(j-1,j+2):
                c += all((min(a,b)>=0, a<h, b<w)) and s[a][b] == '
                
        if s[i][j] == '.':
            s[i][j] = c
for c in s:
    print(*c,sep='')

Solution (Python):
h,w = map(int, input().split())
S=[["." for _ in range(w+2)]]
S+=[["."]+[i for i in input()]+["."]for _ in range(h)]
S+=[["." for _ in range(w+2)]]
for i in range(1, h+1):
    l=""
    for j in range(1, w+1):
        if S[i][j]=="
            l+="
        else:
            l+=str(sum(S[i+n][j+m]=="
    print(l)

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

S = []

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

for _ in range(H):
    S.append(list(input()))

for i in range(H):
    for j in range(W):
        if(S[i][j] == '
        
        num = 0
        for dxy in dxy_list:
            ni = i + dxy[0]
            nj = j + dxy[1]

            if (ni < 0 or H <= ni): continue
            if (nj < 0 or W <= nj): continue
            
            if(S[ni][nj] == '

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

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

Solution (Python):
h,w=map(int,input().split())
s=eval('list(input()),'*h)
r=range
for i in r(h):
  for j in r(w):
    c=0
    for a in r(i-1,i+2):
      for b in r(j-1,j+2):
        c+=all((min(a,b)>=0,a<h,b<w))and s[a][b]=='
    if s[i][j]=='.':s[i][j]=c
for a in s:print(*a,sep='')

Solution (Python):
h,w=map(int,input().split())
s=eval('list(input()),'*h)
r=range
for i in r(h):
  for j in r(w):
    c=0
    for a in r(i-1,i+2):
      for b in r(j-1,j+2):
        c+=all((min(a,b)>=0,a<h,b<w))and s[a][b]=='
    if s[i][j]=='.':s[i][j]=c
for a in s:print(*a,sep='')

Solution (Python):
h, w = [ int(v) for v in input().split() ]
field = [ list(input()) for i in range(h) ]
ansfield = [ [ "
for i in range(h):
	for j in range(w):
		if field[i][j] == ".":
			ansfield[i][j] =  str(sum([ [ field[i][j] for j in range(max(j-1,0),min(w,j+2)) ].count("

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

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

for i in range(1, h+1):
    l=""
    for j in range(1, w+1):
        if S[i][j]=="
            l+="
        else:
            
            l+=str(sum([S[i+n][j+m]=="
    print(l)

Solution (Python):
H,W = map(int, input().split())
S = [list(input()) for _ in range(H)]
answer = []
for i in range(H):
    row = []
    for j in range(W):
        if S[i][j] == "
            row.append("
            continue
        count = 0
        if i-1 != -1:
            for k in range(3):
                if j+k-1 != -1 and j+k-1 != W:
                    count += 1 if S[i-1][j+k-1] == "
        for k in range(0,3,2):
            if j+k-1 != -1 and j+k-1 != W:
                count += 1 if S[i][j+k-1] == "
        if i+1 != H:
            for k in range(3):
                if j+k-1 != -1 and j+k-1 != W:
                    count += 1 if S[i+1][j+k-1] == "
        row.append(count)
    answer.append(row)
for i in answer:
    print(*i, sep="")

Solution (Python):
global h, w, dat
h, w = map(int, input().split())
dat = []
for i in range(h):
    dat.append(input())
for y in range(h):
    for x in range(w):
        c = 0
        if dat[y][x] == "
            print("
            continue

        if 0 < y:
            if dat[y - 1][x] == "
                c += 1
            if 0 < x:
                if dat[y - 1][x - 1] == "
                    c += 1
            if w - 1 > x:
                if dat[y - 1][x + 1] == "
                    c += 1
        if 0 < x:
            if dat[y][x - 1] == "
                c += 1
            if y < h - 1:
                if dat[y + 1][x - 1] == "
                    c += 1

        if x < w - 1:
            if dat[y][x + 1] == "
                c += 1
            if y < h - 1:
                if dat[y + 1][x + 1] == "
                    c += 1

        if y < h - 1:
            if dat[y + 1][x] == "
                c += 1
        print(c, end="")
    print("")

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

def pixel_input(H,W,border="."):
    pixel = [[border]*(W+2)] + \
    [[border] + list(input()) + [border] for _ in range(H)] + \
    [[border]*(W+2)]
    return pixel

def pixel_print(pixel):
    for i in range(len(pixel)):
        print("".join(pixel[i]))
S=pixel_input(H,W)
T=[["?" for _ in range(W)] for _ in range(H)]

for i in range(1,H+1):
    for j in range(1,W+1):
        if S[i][j]=="
            T[i-1][j-1]="
        else:
            T[i-1][j-1]=str((S[i-1][j-1]+S[i-1][j]+S[i-1][j+1]+S[i][j-1]+S[i][j+1]+S[i+1][j-1]+S[i+1][j]+S[i+1][j+1]).count("
pixel_print(T)

Solution (Python):
import copy
H, W = map(int, input().split())
s = []
for _ in range(H):
    s.append(list(input()))
ans = copy.deepcopy(s)
for i in range(H):
    for j in range(W):
        if s[i][j] == '
            continue
        c = 0
        for dx in [-1, 0, 1]:
            for dy in [-1, 0, 1]:
                if 0 <= dy + i < H and 0 <= dx + j < W:
                    if s[i+dy][j+dx] == '
                        c += 1
                else:
                    continue
        ans[i][j] = str(c)
for a in ans:
    print("".join(a))

Solution (Python):
h,w = map(int,input().split())
S = [list(input()) for i in range(h)]
dy = [1,1,1,0,-1,-1,-1,0]
dx = [1,0,-1,-1,-1,0,1,1]
for y in range(h):
    A = []
    for x in range(w):
        if S[y][x] == '
            A.append('
        else:
            cnt = 0
            for i in range(8):
                ny = y+dy[i]
                nx = x+dx[i]
                if 0<=ny<h and 0<=nx<w and S[ny][nx]=='
                    cnt += 1
            A.append(str(cnt))
    print("".join(A))

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

Solution (Python):


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

H += 2
W += 2

S = ['0'*W] * H

for i in range(1, H-1):
    S[i] = str(input())

for n in range(1, H-1):
    S[n] = '0' + S[n] + '0'

for n in range(1, H-1):
    for m in range(1, W-1):
        if(S[n][m] == '.'):
            counter = 0
            for s in range(n-1, n+2):
                for t in range(m-1, m+2):
                    if(S[s][t] == '
                        counter += 1
            print(counter,end='')
        if(S[n][m] == '
            print('
    print()

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

for y in range(1, H+1):
    for x in range(1, W+1):
        if S[y][x] == '.':
            S[y][x] = str(len([1 for i in range(y-1, y+2) for j in range(x-1, x+2) if S[i][j] == '

S = [s[1:-1] for s in S[1:-1]]

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

Solution (Python):
H,W = map(int,input().split())
S = [list(input()) for i in range(H)]
dx = [1,1,1,0,0,-1,-1,-1]
dy = [1,0,-1,1,-1,1,0,-1]
for j in range(H):
    for i in range(W):
        cnt = 0
        for k in range(8):
            if 0<=i+dx[k]<W and 0<=j+dy[k]<H:
                if S[j+dy[k]][i+dx[k]]=="
                    cnt += 1
        if S[j][i] == "
            continue
        else:
            S[j][i] = cnt


for i in range(H):
    for j in range(W):
        S[i][j] = str(S[i][j])

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

Solution (Python):
def putnum(arr, i, j):
    i_l = i-1 if i-1 >= 0 else 50
    j_l = j-1 if j-1 >= 0 else 50
    try: arr[i_l][j_l] += 1
    except: pass
    try: arr[i_l][j] += 1
    except: pass
    try: arr[i_l][j+1] += 1
    except: pass
    try: arr[i][j_l] += 1
    except: pass
    try: arr[i][j+1] += 1
    except: pass
    try: arr[i+1][j_l] += 1
    except: pass
    try: arr[i+1][j] += 1
    except: pass
    try: arr[i+1][j+1] += 1
    except: pass

h, w = map(int, input().split())
fie = [[0]*w for _ in range(h)]
for i in range(h):
    for j, c in enumerate(list(input())):
        if c=="

for i in range(h):
    for j in range(w):
        if fie[i][j] >= 9: putnum(fie, i, j)

for i in fie:
    i = list(map(str, map(lambda x:min(9,x), i)))
    print("".join(i).replace("9", "

Solution (Python):

H, W = map(int, input().split())
S = []
for i in range(H):
    S.append(list(input()))
ans = []
for i in range(H):
    ans_s = []
    for j in range(W):
        if S[i][j] == "
            ans_s.append("
        else:
            bom = 0
            for x in range(-1, 2):
                for y in range(-1, 2):
                    if 0 <= i - x and i - x <= H - 1 and 0 <= j - y and j - y <= W - 1:
                        if not (x == 0 and y == 0):
                            if S[i - x][j - y] == "
                                bom += 1
            ans_s.append(str(bom))
    ans.append(ans_s)

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

Solution (Python):
import itertools
import math
H,W = list(map(int,input().split(" ")))
mines = []
dirs = [0,1,-1]

for _ in range(H):
    l =input()
    mines.append(l)


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

        if mines[h][w] == "
            print("

        else:
            c = 0
            for d1 in dirs:
                for d2 in dirs:
                    x = w - d1
                    y = h - d2
                    if x >=0 and x < W and y >= 0 and y < H:
                        if mines[y][x] == "
                            c += 1
            print(c,end="")
    print()

Solution (Python):
H,W = map(int,input().split())
s = [input() for _ in range(H)]
def ch(ii,jj):
    if s[ii][jj] == '
        return 1
    else:
        return 0

news = ['' for _ in range(H)]
for i in range(H):
    for j in range(W):
        if s[i][j] == '.':
            b = 0
            if i-1>=0:
                if j-1>=0:
                    b += ch(i-1,j-1)
                b += ch(i-1,j)
                if j+1 <= W-1:
                    b += ch(i-1,j+1) 
            if j-1>=0:
                b += ch(i,j-1)
            if j+1 <= W-1:
                b += ch(i,j+1)
            if i+1<=H-1:
                if j-1>=0:
                     b += ch(i+1,j-1)
                b += ch(i+1,j)
                if j+1 <= W-1:
                    b += ch(i+1,j+1)
            news[i] = news[i] + str(b)
        else:
            news[i] = news[i] + '
for l in news:
    print(l)

Solution (Python):
import numpy as np

def in_map(x,y,map_str):
    if 0<=x and x <map_str.shape[1] and 0<=y and y<map_str.shape[0]:
        result=True
    else:
        result=False
    return result

def bom_count(bom,map_str,bom_map):
    
    for h in range(-1,2):
        for w in range(-1,2):
            
            y = bom[0]+h
            x = bom[1]+w
            if h==0 and w==0:
                bom_map[y,x]=9
            if not in_map(x,y,map_str):
                continue
            if not map_str[y,x]=="
                bom_map[y,x]+=1

    
    return bom_map

if __name__ =="__main__":
    H,W=input_list=[int(i) for i in input().split()]
    map_str=np.empty((H,W),dtype=np.str_)
    for h in range(H):
        map_str[h] = np.array([i for i in input()])
    boms=np.where(map_str=="
    
    bom_map=np.zeros((H,W),dtype=np.int16)
    for i in range(len(boms[0])):
        bom=[boms[0][i],boms[1][i]]
        
        bom_map=bom_count(bom,map_str,bom_map)
    for h in range(H):
        print_str=""
        for w in range(W):
            if bom_map[h,w]==9:
                print_str+="
            else:
                print_str+=str(bom_map[h,w])
        print(print_str)

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

s = [["." for _ in range(w+2)]] + [["."]+list(input())+["."] for _ in range(h)] + [["." for _ in range(w+2)]]
for r in range(1, h+1):
  for c in range(1, w+1):
    if s[r][c] == ".":
      s[r][c] = str([s[r+dr][c+dc] for dr in [-1, 0, 1] for dc in [-1, 0, 1]].count("
for r in range(1, h+1):
  print("".join(s[r][1:w+1]))

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):
        if S[i][j] == '.':
            cnt = 0
            if i != 0:
                cnt += S[i-1][max(0,j-1):min(j+1,W-1)+1].count('
            cnt += S[i][max(0,j-1):min(j+1,W-1)+1].count('
            if i != H-1:
                cnt += S[i+1][max(0,j-1):min(j+1,W-1)+1].count('
            S[i][j] = str(cnt)
for i in range(H):
    print(''.join(S[i]))

Solution (Python):
h,w=map(int,input().split())
S=[]
Key=[]
S.append(list('.'*(w+2)))
for i in range(1,h+1):
    S.append(list(input()))
    S[i].insert(0,'s')
    S[i].append('f')
S.append(list('.'*(w+2)))

for i in range(1,h+1):
    tmp=[]
    for j in range(1,w+1):
        cnt=0
        if S[i-1][j-1]=='
            cnt+=1
        if S[i-1][j]=='
            cnt+=1
        if S[i-1][j+1]=='
            cnt+=1
        if S[i][j-1]=='
            cnt+=1
        if S[i][j+1]=='
            cnt+=1
        if S[i+1][j-1]=='
            cnt+=1
        if S[i+1][j]=='
            cnt+=1
        if S[i+1][j+1]=='
            cnt+=1
        
        if S[i][j]=='
            tmp.append('
        else: tmp.append(str(cnt))
    Key.append(tmp)


for i in range(h):
    tmp=''.join(Key[i])
    print(tmp)

Solution (Python):
r, c = map(int, input().split())
d = [list(input()) for i in range(r)]
bom = [(x,y) for (x,i) in enumerate(d) for (y,j) in enumerate(i) if j == '
temp = []
for b in bom:
    temp += [(b[0]+x, b[1]+y) for y in [1,0,-1] for x in [1,0,-1]]
print('\n'.join([''.join([str(temp.count((i,j))) if d[i][j] != '

Solution (Python):
h,w = map(int,input().split())
S = []
for i in range(h):
    S.append(list(input()))
if h == 1 and w == 1:
    if S[0][0] == '.':
        print('0')
    else:
        print(S[0][0])
if h == 1 and w == 2:
    if S == [['.','
        print('1
    if S == [['
        print('
    if S == [['
        print('
    if S == [['.','.']]:
        print('00')
if h == 1 and w >= 3:
    for g in range(w):
        if S[0][0] == '.':
            if S[0][1] == '
                S[0][0] = '1'
            else:
                S[0][0] = '0'
        if 1 <= g <= w-2:
            if S[0][g] == '.':
                S[0][g] = str(S[0][g-1:g+2].count('
        if S[0][w-1] == '.':
            if S[0][w-2] == '
                S[0][w-1] = '1'
            else:
                S[0][w-1] = '0'
    print(''.join(S[0]))
if h >= 3 and w == 1:
    for y in range(h):
        if S[0][0] == '.':
            if S[1][0] == '
                S[0][0] = '1'
            else:
                S[0][0] = '0' 
        if 1 <= y <= h-2:
            if S[y][0] == '.':
                count = 0
                if S[y-1][0] == '
                    count += 1
                if S[y+1][0] == '
                    count += 1
                S[y][0] = int(count)
        if S[h-1][0] == '.':
            if S[h-2][0] == '
                S[h-1][0] = '1'
            else:
                S[h-1][0] = '0'
    for p in S:
        print(p[0])
if h == 2 and w == 1:
    if S == [['.'],['.']]:
        print('0')
        print('0')
    if S == [['
        print('
        print('1')
    if S == [['.'],['
        print('1')
        print('
    if S == [['
        print('
        print('
if h >= 2 and w >= 2:
    for k in range(h):
        for j in range(w):
            if S[k][j] == '.':
                if k == 0 and j == 0:
                    count = 0
                    if S[0][1] == '
                        count += 1 
                    count += S[1][0:2].count('
                    S[k][j] = str(count)
                elif k == 0 and 1 <= j <= w-2:
                    count = 0
                    count += S[0][j-1:j+2].count('
                    count += S[1][j-1:j+2].count('
                    S[k][j] = str(count)
                elif k == 0 and j == w-1:
                    count = 0
                    if S[0][j-1] == '
                        count += 1
                    count += S[1][j-1:].count('
                    S[k][j] = str(count)
                elif 1 <= k <= h-2 and j == 0:
                    count = 0
                    count += S[k-1][0:2].count('
                    if S[k][1] == '
                        count += 1 
                    count += S[k+1][0:2].count('
                    S[k][j] = str(count)
                elif 1 <= k <= h-2 and 1 <= j <= w-2:
                    count = 0
                    count += S[k-1][j-1:j+2].count('
                    count += S[k][j-1:j+2].count('
                    count += S[k+1][j-1:j+2].count('
                    S[k][j] = str(count)
                elif 1 <= k <= h-2 and j == w-1:
                    count = 0
                    count += S[k-1][j-1:].count('
                    count += S[k][j-1:].count('
                    count += S[k+1][j-1:].count('
                    S[k][j] = str(count)
                elif k == h-1 and j == 0:
                    count = 0
                    count += S[k-1][0:2].count('
                    count += S[k][1].count('
                    S[k][j] = str(count)
                elif k == h-1 and 1 <= j <= w-2:
                    count = 0
                    count += S[k-1][j-1:j+2].count('
                    count += S[k][j-1:j+2].count('
                    S[k][j] = str(count)
                elif k == h-1 and j == w-1:
                    count = 0
                    count += S[k-1][j-1:].count('
                    count += S[k][j-1:].count('
                    S[k][j] = str(count)
    for p in S:
        print(''.join(p))

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

for i in range(H):
  for j in range(W):
    if S[i][j] == ".":
      cnt = 0
      if i != 0:
        cnt += S[i-1][max(0, j-1) : min(j+1, W-1) + 1].count("
      cnt += S[i][max(0, j-1) : min(j+1, W-1) + 1].count("
      if i != H-1:
        cnt += S[i+1][max(0, j-1) : min(j+1, W-1) + 1].count("
      S[i][j] = str(cnt)
for i in range(H):
	print("".join(S[i]))

Solution (Python):


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

for i in range(h):
    for j in range(w):
        if s_list[i][j] == "
            ans_list[i][j] = "
        else:
            if (i!=0) and (j!=0):
                if (s_list[i-1][j-1] == "
                    ans_list[i][j] += 1
            if (i!=0):
                if (s_list[i-1][j] == "
                    ans_list[i][j] += 1
            if (i!=0) and (j!=w-1):
                if (s_list[i-1][j+1] == "
                    ans_list[i][j] += 1
            if (j!=0):
                if (s_list[i][j-1] == "
                    ans_list[i][j] += 1
            if (j!=w-1):
                if (s_list[i][j+1] == "
                    ans_list[i][j] += 1
            if (i!=h-1) and (j!=0):
                if (s_list[i+1][j-1] == "
                    ans_list[i][j] += 1
            if (i!=h-1):
                if (s_list[i+1][j] == "
                    ans_list[i][j] += 1
            if (i!=h-1) and (j!=w-1):
                if (s_list[i+1][j+1] == "
                    ans_list[i][j] += 1

for i in ans_list:
    print(''.join(map(str, i)))

Solution (Python):

H, W = map(int, input().split())
s = [list(input()) for _ in range(H)]
for i in range(H):
    for j in range(W):
        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 < W-1 and s[i][j+1] =="
                count +=1
            if i < H-1 and j<W-1 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>0 and s[i+1][j-1] =="
                count +=1
            if j > 0 and s[i][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())

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

for i in range(H):
  for j in range(W):
    
    c=0

    
    if l[i][j]=='
      pass

    
    else:
      
      if 0<i and 0<j and l[i-1][j-1]=='
        c+=1
      
      if 0<i and l[i-1][j]=='
        c+=1
      
      if 0<i and j<W-1 and l[i-1][j+1]=='
        c+=1
      
      if 0<j and l[i][j-1]=='
        c+=1
      
      if j<W-1 and l[i][j+1]=='
        c+=1
      
      if i<H-1 and 0<j and l[i+1][j-1]=='
        c+=1
      
      if i<H-1 and l[i+1][j]=='
        c+=1
      
      if i<H-1 and j<W-1 and l[i+1][j+1]=='
        c+=1

      l[i][j]=str(c)


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

Solution (Python):
import copy

H,W = map(int, input().split())
s = [list("."+input()+".") for _ in range(H)]
S = [["."]*(W+2)] + s + [["."]*(W+2)]
T = copy.deepcopy(S)

for i in range(1,H+1):
    for j in range(1,W+1):
        cnt = S[i-1][j-1:j+2].count('
        cnt += S[i][j+1].count('
        if T[i][j]!="
            T[i][j] = str(cnt)

ans = T[1:-1]
for i in range(H):
    ans[i] = ans[i][1:-1]
    print ("".join(ans[i]))

Solution (Python):
h,w=map(int,input().split())
M=[['.']*(w+2),['.']*(w+2)]
for i in range(h):
    m=list(input())
    m.append('.')
    m.insert(0,'.')
    M.insert(-1,m)
K=[]

for i in range(1,h+1):
    k=[]
    for j in range(1,w+1):
        if M[i][j]=='
            k.append('
        else:
            c=0
            for r in range(j-1,j+2):
                if M[i-1][r]=='
                    c+=1
            for r in range(j-1,j+2):
                if M[i+1][r]=='
                    c+=1
            if M[i][j-1]=='
                c+=1
            if M[i][j+1]=='
                c+=1
            k.append(str(c))
    K.append(k)
for i in range(len(K)):
    print(''.join(K[i]))

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

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

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 dx == 0 and dy == 0:
                    continue
                x, y = i + dx, j + dy
                if not inside(x, y):
                    continue
                if s[x][y] == '
                    cnt += 1
        res[i][j] = str(cnt)

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

Solution (Python):
import itertools

H, W = map(int, input().split())
S = [[0]*W for _ in range(H)]
for h in range(H):
    s = input()
    for w in range(W):
        if s[w] == '
            S[h][w] = '

for i, j in itertools.product(range(H), range(W)):
    if S[i][j] == '
        if j != 0 and S[i][j-1] != '
            S[i][j-1] += 1
        if j != W - 1 and S[i][j+1] != '
            S[i][j+1] += 1
        if i != 0 and S[i-1][j] != '
            S[i-1][j] += 1
        if i != H-1 and S[i+1][j] != '
            S[i+1][j] += 1

        if j != 0 and i != 0 and S[i-1][j-1] != '
            S[i-1][j-1] += 1
        if j != W - 1 and i != H - 1 and S[i+1][j+1] != '
            S[i+1][j+1] += 1
        if i != 0 and j != W - 1 and S[i-1][j+1] != '
            S[i-1][j+1] += 1
        if i != H-1 and j != 0 and S[i+1][j-1] != '
            S[i+1][j-1] += 1

for s in S:
    print(*s, 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
			if i != 0:
				cnt += S[i - 1][max(0, j - 1): min(j + 1, W - 1) + 1].count("
			cnt += S[i][max(0, j - 1): min(j + 1, W - 1) + 1].count("
			if i != H - 1:
				cnt += S[i + 1][max(0, j - 1): min(j + 1, W - 1) + 1].count("
			S[i][j] = str(cnt)
for x in range(H):
	print("".join(S[x]))

Solution (Python):
def main():
    h, w = map(int, input().split())
    grid = []
    for height in range(0, h):
        grid.append(list(input()))

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

    for height in range(0, h):
        for width in range(0, w):
            if grid[height][width] == '
                continue
            else:
                grid[height][width] = 0

            for i in range(0, 8):
                if (height + dy[i] < 0) or (height + dy[i] >= h) or (width + dx[i] < 0) or (width + dx[i] >= w):
                    continue
                if (grid[height + dy[i]][width + dx[i]]) == '
                    grid[height][width] += 1

    for height in range(0, h):
        for width in range(0, w):
            print(grid[height][width], end='')
        print()


if __name__ == "__main__":
  main()

Solution (Python):
def main():
    h, w = map(int, input().split())
    grid = []
    for height in range(0, h):
        grid.append(list(input()))

    replaced = []
    for height in range(0, h):
        replaced.append([0] * w)

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

    for height in range(0, h):
        for width in range(0, w):
            if grid[height][width] == '
                replaced[height][width] = '
                continue
            for i in range(0, 8):
                if (height + dy[i] < 0) or (height + dy[i] >= h) or (width + dx[i] < 0) or (width + dx[i] >= w):
                    continue
                if (grid[height + dy[i]][width + dx[i]]) == '
                    replaced[height][width] += 1

    for height in range(0, h):
        for width in range(0, w):
            print(replaced[height][width], end='')
        print()


if __name__ == "__main__":
  main()