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

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

Solution (Python):
h, w = (int(i) for i in input().split())
s = [input() for i in range(h)]
ans = [[0]*w for i in range(h)]
for i in range(h):
    for j in range(w):
        cnt = 0
        if s[i][j] == "
            ans[i][j] = "
            continue
        for k in range(-1,2):
            for l in range(-1,2):
                if 0 <= i+k <= h-1 and 0 <= j+l <= w-1 and s[i+k][j+l] == "
                    cnt += 1
        ans[i][j] = cnt

for i in ans:
    print(*i,sep="")

Solution (Python):
h, w = (int(i) for i in input().split())
s = [input() for i in range(h)]
ans = [[0]*w for i in range(h)]
for i in range(h):
    for j in range(w):
        cnt = 0
        if s[i][j] == "
            ans[i][j] = "
            continue
        for k in range(-1,2):
            for l in range(-1,2):
                if 0 <= i+k <= h-1 and 0 <= j+l <= w-1 and s[i+k][j+l] == "
                    cnt += 1
        ans[i][j] = cnt

for i in ans:
    print(*i,sep="")

Solution (Python):
h, w = map(int, input().split())
ss = [[0] * w for _ in range(h)]
s = [list(input()) for _ in range(h)]
for i in range(h):
    for j in range(w):
        if s[i][j] == "
            for k in range(max(0, i - 1), min(h, i + 2)):
                for l in range(max(0, j - 1), min(w, j + 2)):
                    ss[k][l] += 1
for i in range(h):
    ans = ""
    for j in range(w):
        if s[i][j] == "
            ans += s[i][j]
        else:
            ans += str(ss[i][j])
    print(ans)

Solution (Python):
h,w = map(int,input().split())	
s=[0 for i in range(h)]
for i in range(h):
    s[i]=list(input())
n=[["." for _ in range(w+2)]for _ in range(h+2)]
for i in range(h):
    for j in range(w):
        n[i+1][j+1]=s[i][j]
    

for i in range(h+2):
    for j in range(w+2):
        if(n[i][j]=="."):
            n[i][j]=0
        else:
            n[i][j]="1"


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

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

for i in range(h):
    for j in range(w):
        s[i][j]=n[i+1][j+1]
        
for i in range(h):
    print(''.join(map(str,s[i])))

Solution (Python):
H, W = map(int, input().split())
L = [[0 for w in range(W)] for h in range(H)]
_L = [['' for w in range(W)] for h in range(H)]
for h in range(H):
    tmp = list(input())
    for w in range(W):
        if tmp[w] == "
            _L[h][w] = "
            if h+1 < H:
                L[h + 1][w] += 1
            if h > 0:
                L[h - 1][w] += 1
            if h+1 < H and w+1 < W:
                L[h + 1][w + 1] += 1
            if h > 0 and w+1 < W:
                L[h - 1][w + 1] += 1
            if w+1 < W:
                L[h][w + 1] += 1
            if h+1 < H and w > 0:
                L[h + 1][w - 1] += 1
            if h > 0 and w > 0:
                L[h - 1][w - 1] += 1
            if w > 0:
                L[h][w - 1] += 1
for h in range(H):
    for w in range(W):
        if _L[h][w] == "":
            _L[h][w] = str(L[h][w])

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

Solution (Python):
def solve():
  h,w = (int(i) for i in input().split())
  String = [input() for _ in range(h)]
  query = []
  for i in range(h):
    for j in range(w):
      if String[i][j] == "
        query.append((i,j))

  dx = [(-1,-1),(-1,0),(0,-1),(-1,1),(0,1),(1,-1),(1,0),(1,1)]
  newString = [[0]*w for _ in range(h)]
  while query:
    i,j = query.pop()
    for x,y in dx:
      nx = i+x
      ny = j+y
      if nx < 0 or ny < 0 or nx >= h or ny >= w:
        continue
      else:
        newString[nx][ny] += 1
  for i in range(h):
    for j in range(w):
      if not String[i][j] =="
        newString[i][j] = str(newString[i][j])
      else:
        newString[i][j] = "
  for i in range(h):
    print("".join(newString[i]))
solve()

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

S = []
for _ in range(H):
	S.append([0 if x == '.' else x for x in list(input())])



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

for i in range(H):
	for j in range(W):
		if S[i][j] == '
			for k in range(8):
				x = i + dx[k]
				y = j + dy[k]
				if x >= 0 and y >= 0 and x < H and y < W:
					if S[x][y] != '
						S[x][y] += 1

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

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

S = []
for _ in range(H):
	S.append([0 if x == '.' else x for x in list(input())])

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

for i in range(H):
	for j in range(W):
		if S[i][j] == '
			for k in range(8):
				x = i + dx[k]
				y = j + dy[k]
				if x >= 0 and y >= 0 and x < H and y < W:
					if S[x][y] != '
						S[x][y] += 1

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

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

for i in range(h):
    for j in range(m):
        if s[i][j] == "
            ans[i].append("
        else:
            count = 0
            s1=[[i-1,j],[i+1,j],[i,j-1],[i,j+1]]
            s2=[[i-1,j+1],[i+1,j+1],[i+1,j-1],[i-1,j-1]]
            for temp in s1 :
                i2=temp[0];j2=temp[1]
                if i2<0 or h-1<i2 or j2<0 or m-1<j2:
                    continue

                if s[i2][j2]=="
                    count+=1

            for temp in s2 :
                i2=temp[0];j2=temp[1]
                if i2<0 or h-1<i2 or j2 <0 or m-1<j2 :
                    continue

                if s[i2][j2]=="
                    count+=1
            ans[i].append(count)

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

Solution (Python):
H,W = map(int,input().split())
src = [input() for i in range(H)]
 
ans = []
for row in src:
    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 src[y][x] == '
        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 src[y+dy][x+dx] == '
        ans[y][x] = c
 
for row in ans:
    print(''.join(list(map(str,row))))

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

arr=[list(input()) for _ in range(H)]

for i in range(H):
  for j in range(W):
    if arr[i][j]!='.': continue
    
    bom=0
    for row_offs in [-1,0,1]:
      for col_offs in [-1,0,1]:
        if row_offs==0 and col_offs==0:   continue
        if i+row_offs<0 or H<=i+row_offs: continue
        if j+col_offs<0 or W<=j+col_offs: continue  
        if arr[i+row_offs][j+col_offs]=='
          bom+=1
    arr[i][j]=bom
    
for arr_ in arr:
  print(*arr_,sep="")

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


def q(i, j):
    if not 0 <= i < H or not 0 <= j < W:
        return 0
    return 1 if S[i][j] == "


for i in range(H):
    for j in range(W):
        if S[i][j] != ".":
            continue
        c = 0
        c += q(i-1, j-1)
        c += q(i-1, j)
        c += q(i-1, j+1)
        c += q(i, j-1)
        c += q(i, j+1)
        c += q(i+1, j-1)
        c += q(i+1, j)
        c += q(i+1, j+1)
        S[i][j] = str(c)

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

Solution (Python):
h,w=map(int,input().split())
s=[input() for _ 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=[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=[input() for _ 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=[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):
import numpy as np
h,w=input().split()
h,w=int(h),int(w)
t1=[]
for i in range(h):
    s1=list(input())
    t1.append(s1)

t2=[[0 for i in range(w)] for i in range(h)]
for i in range(h):
    for j in range(w):
        if t1[i][j]=="
            for k in range(3):
                for l in range(3):
                    if k==1 and l==1:
                        continue
                    if i+k-1>=0 and j+l-1>=0 and i+k-1<h and j+l-1<w and t1[i+k-1][j+l-1]!="
                        t2[i+k-1][j+l-1]+=1
            t2[i][j]="
for i in range(h):
    for j in range(w):
        print(t2[i][j],end="")
    print("")

Solution (Python):
import itertools


H, W = map(int, input().split())
R = [[0 for i in range(W+2)] for j in range(H+2)]
memo = []


for h in range(1, H+1):
    for w, s in zip(range(1, W+1), input()):
        if s == '
            memo.append([h, w])
            for i, j in itertools.product([-1, 0, 1], [-1, 0, 1]):
                R[h+i][w+j] += 1

for h, w in memo:
    R[h][w] = '


for S in R[1:-1]:
    print(''.join([str(e) for e in S[1:-1]]))

Solution (Python):

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


R = []
R.append('.' + '.' * W + '.')
for c in S:
    R.append(['.'] + c + ['.'])

R.append('.' + '.' * W + '.')




ret = []
for h in range(H):
    for w in range(W):
        count = 0
        
        if S[h][w] != '
            
            if R[h][w] == '
            if R[h][w+1] == '
            if R[h][w+2] == '

            
            if R[h+1][w] == '
            if R[h+1][w+2] == '

            
            if R[h+2][w] == '
            if R[h+2][w+1] == '
            if R[h+2][w+2] == '

            S[h][w] = str(count)

for line in S:
    print(*line, sep="")

Solution (Python):
import sys
h, w = map(int, input().split())
s = [input() for _ in range(h)]
if h == 1 and w == 1:
    if s[0] == '
        print('
    else:
        print(0)
    sys.exit()
for i, m in enumerate(s):
    for j in range(w):
        if m[j] != '
            if w == 1:
                if i == 0:
                    count = s[1][0].count('
                elif i == h-1:
                    count = s[-2][0].count('
                else:
                    count = s[i-1][0].count('
            elif h == 1:
                if j == 0:
                    count = m[1].count('
                elif j == w-1:
                    count = m[-2].count('
                else:
                    count = m[j-1].count('
            else:
                if i == 0:
                    if j == 0:
                        count = s[i+1][:2].count('
                    elif j == w-1:
                        count = s[i+1][-2:].count('
                    else:
                        count = s[i+1][j-1:j+2].count('
                elif i == h-1:
                    if j == 0:
                        count = s[-2][:2].count('
                    elif j == w-1:
                        count = s[-2][-2:].count('
                    else:
                        count = s[i-1][j-1:j+2].count('
                else:
                    if j == 0:
                        count = s[i-1][:2].count('
                    elif j == w-1:
                        count = s[i-1][-2:].count('
                    else:
                        count = s[i-1][j-1:j+2].count('
            m = m.replace(m[j], str(count), 1)
    print(m)

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


ans=[[] for i in range(h)]

for i in range(h) :
  for j in range(w):
    
    if sl[i][j]=="
      ans[i].append("
    else :
      count=0
      search1=[[i-1,j],[i+1,j],[i,j-1],[i,j+1]]
      search2=[[i-1,j+1],[i+1,j+1],[i+1,j-1],[i-1,j-1]]
      for temp in search1 :
        i2=temp[0];j2=temp[1]
        if i2<0 or h-1<i2 or j2 <0 or w-1<j2 :
          continue
        if sl[i2][j2]=="
          count+=1
      for temp in search2 :
        i2=temp[0];j2=temp[1]
        if i2<0 or h-1<i2 or j2 <0 or w-1<j2 :
          continue
        if sl[i2][j2]=="
          count+=1
      ans[i].append(count)


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

Solution (Python):
import math
import sys
from itertools import accumulate
from functools import reduce
import queue
from fractions import gcd

def lcm(a, b):
    return a * b // gcd(a, b)

def combination_count(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

def permutations_count(n, r):
    return math.factorial(n) // math.factorial(n - r)

sys.setrecursionlimit(10000)

mod = 1000000007

"""

a = 100
b = 0.987654321
print("{0:06d}-{1:6f}".format(a,b))
000100-0.987654


char_list = list("abcd") 



    = sys.stdin.readline().rstrip()
    = list(sys.stdin.readline().rstrip())
    = [list(list(sys.stdin.readline().rstrip())) for i in range(h)] 


    = int(sys.stdin.readline())
    = map(int, sys.stdin.readline().split())
    = list(map(int, sys.stdin.readline().split()))
    = [list(map(int,list(sys.stdin.readline().split()))) for i in range(h)]  
"""

if __name__ == "__main__":
    h,w = map(int, sys.stdin.readline().split())
    s = [list(list(sys.stdin.readline().rstrip())) for i in range(h)]
    for i in range(h):
        for j in range(w):
            c=0
            if s[i][j] == "
                pass
            else:
                if 0<i and 0<j and s[i-1][j-1] == "
                    c +=1
                if 0<j and s[i][j-1] == "
                    c +=1
                if 0<i and s[i-1][j] == "
                    c +=1
                if i<h-1 and j<w-1 and s[i+1][j+1] == "
                    c +=1
                if j<w-1 and s[i][j+1] == "
                    c +=1
                if i<h-1 and s[i+1][j] == "
                    c +=1
                if 0<i and j<w-1 and s[i-1][j+1] == "
                    c +=1
                if 0<j and i<h-1 and s[i+1][j-1] == "
                    c +=1
                s[i][j] = c
    for i in range(h):
        print(*s[i], sep='')

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

for i in range(1,h+1):
    for j in range(1,w+1):
        cnt=0
        if sij[i][j]=="
            continue
        for ii in range(-1,2):
            for jj in range(-1,2):
                if ii==jj==0:
                    continue
                if sij[i+ii][j+jj]=="
                    cnt+=1
        sij[i][j]=str(cnt)
for i in range(1,h+1):
    for j in range(1,w+1):
        print(sij[i][j],end="")
    print()

Solution (Python):
H, W = map(int, input().split())
S = [[0 if a == '.' else '

for h in range(H):
    S[h].extend([0])
S.append([0 for _ in range(W+1)])

for h in range(H):
    for w in range(W):
        if S[h][w] == 0:
            near = [(h-1, w-1), (h, w-1), (h+1, w-1), (h-1, w), (h+1, w), (h-1, w+1), (h, w+1), (h+1, w+1)]
            for nh, nw in near:
                if S[nh][nw] == '
                    S[h][w] += 1
for h in range(H):
    print(''.join([str(a) for a in S[h][:-1]]))

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

h_marume=lambda x: min(h-1, max(x, 0))
w_marume=lambda x: min(w-1, max(x, 0))

for i in range(h):
  for j in range(w):
    if slist[i][j]==".":
      t_set=[(h_marume(i-1),w_marume(j-1)),
             (h_marume(i-1),w_marume(j)),
             (h_marume(i-1),w_marume(j+1)),
             (h_marume(i),w_marume(j-1)),
             (h_marume(i),w_marume(j+1)),
             (h_marume(i+1),w_marume(j-1)),
             (h_marume(i+1),w_marume(j)),
             (h_marume(i+1),w_marume(j+1))
            ]
      t_set=list(set(t_set))
      print(sum([slist[t[0]][t[1]]=="
    else:
      print("
  print("\n",end="")

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

a = [[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] == "
            a[i][j] = "
            for p in [-1, 0, 1]:
                for q in [-1, 0, 1]:
                    if i + p >= 0 and j + q >= 0 and i + p < H and j + q < W and S[i + p][j + q] != "
                        a[i + p][j + q] += 1

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

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

INPUT = [list(input()) for i in range(H)]
OUTPUT = INPUT.copy()

for i in range(H):
    for j in range(W):
        if INPUT[i][j] == '
            OUTPUT[i][j] = '
            continue
        else:
            OUTPUT[i][j] = 0
            for I in (i-1, i, i+1):
                if (I < 0 or H <= I):
                    continue
                for J in (j-1, j, j+1):
                    if (J < 0 or W <= J):
                        continue
                    if INPUT[I][J] == '
                        OUTPUT[i][j] += 1


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

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

A = ["." * (W+2)] + [("." + input()) + "." for _ in range(H)] + ["." * (W+2)]

for i in range(1,H+1):
  for j in range(1,W+1):
    if A[i][j] == "
      L ="
    else:
      L =sum([1 for x in range(-1, 2) for y in range(-1, 2) if A[i+x][j+y] == "
    print (L, end = "")
  print()

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

dum='xx'
for i in range(W):
  dum=dum+'x'
  
string.append(dum)
for i in range(H):
  string.append('x'+input()+'x')

string.append(dum)


  
for i in range(1,H+1):
  for j in range(1,W+1):
    if string[i][j]=='
      answer.append('
    else:
      count=0
      if string[i-1][j-1]=='
        count=count+1
      if string[i-1][j]=='
        count=count+1
      if string[i-1][j+1]=='
        count=count+1
      if string[i][j-1]=='
        count=count+1
      if string[i][j+1]=='
        count=count+1
      if string[i+1][j-1]=='
        count=count+1
      if string[i+1][j]=='
        count=count+1
      if string[i+1][j+1]=='
        count=count+1
      answer.append(count)



for i in range(H):
  dum=''
  for j in range(W):
   dum=dum+str(answer[i*W+j])
  print(dum)

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

sheet = []

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

for i in range(H):
	for j in range(W):
		if sheet[i][j] == '.':
			sV = (i if i == 0 else i - 1)
			eV = (i + 1 if i == H - 1 else i + 2)
			sH = (j if j == 0 else j - 1)
			eH = (j + 1 if j == W - 1 else j + 2)
			count = 0
			
			for p in range(sV, eV):
				for q in range(sH, eH):
					if sheet[p][q] == '
						count += 1
			
			sheet[i][j] = count

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

Solution (Python):
gyou,retu=map(int,input().split())
l=[input() for _ in range(gyou)]
d=[(-1,-1),(-1,0),(-1,1),(0,-1),(0,1),(1,-1),(1,0),(1,1)]
for g in range(gyou):
  preans=str()
  for r in range(retu):
    s=0
    if l[g][r]=="
      preans += "
    else:
      for x in range(8):
        a,b=d[x]
        try:
          if l[g+a][r+b]=="
            s+=1
        except IndexError:
          s+=0
      preans += str(s)
  print(preans)

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

INPUT = [list(input()) for i in range(H)]
OUTPUT = INPUT.copy()

for i in range(H):
    for j in range(W):
        if INPUT[i][j] == '
            OUTPUT[i][j] = '
            continue
        else:
            OUTPUT[i][j] = 0
            for I in (i-1, i, i+1):
                if (I < 0 or H <= I):
                    continue
                for J in (j-1, j, j+1):
                    if (J < 0 or W <= J):
                        continue
                    if INPUT[I][J] == '
                        OUTPUT[i][j] += 1


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

Solution (Python):
h, w = map(int, input().split())
a = [[j for j in input()] for i in range(h)]
for ki, k in enumerate(a):
    for ji, j in enumerate(k):
        if j == '
            print('
        else:
            c = 0
            for m in range(3):
                for n in range(3):
                    if 0 <= ki - n + 1 < h and 0 <= ji - m + 1 < w:
                        if a[ki - n + 1][ji - m + 1] == '
                            c += 1
            print(c, end='')
    print()

Solution (Python):
h, w = [int(x) for x in input().split()]
grid = [[b for b in input()] for i in range(h)]
result = []
for i in range(h):
    row = ''
    for j in range(w):
        if grid[i][j] == '
            row += '
        else:
            row += str(sum([area[max(0, j - 1):min(w, j + 2)].count('
                            for area in grid[max(0, i - 1):min(h, i + 2)]]))
    result.append(row)
print('\n'.join(result))

Solution (Python):
h, w = map(int, input().split())
S = []
S.append('.' * (w + 2))
for _ in range(h):
    S.append('.' + input() + '.')
S.append('.' * (w + 2))
for i in range(1, h+1):
    l = []
    for j in range(1, w+1):
        if S[i][j] == '
            l.append('
            continue
        _s = S[i-1][j-1:j+2] + S[i][j-1] + S[i][j+1] + S[i+1][j-1:j+2]
        l.append(_s.count('
    print(''.join(map(str, l)))

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

result = []
result.append(['0'] * (W))

for h in range(H):
    result.append(['0'] * W)
    s = input()

    for w in range(W):
        if s[w] == '

            result[h][w] = '

            if 0 < h and 0 < w:
                if result[h-1][w-1] != '
                    result[h-1][w-1] = str(int(result[h-1][w-1]) + 1)
            
            if 0 < h and w < W-1:
                if result[h-1][w+1] != '
                    result[h-1][w+1] = str(int(result[h-1][w+1]) + 1)

            if h < H-1 and w < W-1:
                if result[h+1][w+1] != '
                    result[h+1][w+1] = str(int(result[h+1][w+1]) + 1)
            
            if h < H-1 and 0 < w:
                if result[h+1][w-1] != '
                    result[h+1][w-1] = str(int(result[h+1][w-1]) + 1)

            if 0 < h:
                if result[h-1][w] != '
                    result[h-1][w] = str(int(result[h-1][w]) + 1)
            
            if h < H-1:
                if result[h+1][w] != '
                    result[h+1][w] = str(int(result[h+1][w]) + 1)
            
            if 0 < w:
                if result[h][w-1] != '
                    result[h][w-1] = str(int(result[h][w-1]) + 1)

            if w < W-1:
                if result[h][w+1] != '
                    result[h][w+1] = str(int(result[h][w+1]) + 1)

    if h > 0:
        print(''.join(result[h-1]))

print(''.join(result[-2]))

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

    
def akimasu(s,h,w):
    F=[]
    F.append("."*(w+2))
    for i in s:
        F.append("."+i+".") 
    F.append("."*(w+2))
    return F

F=akimasu(S,h,w)

def hantei(f,y,x): 
    
    C=0
    for i in range(3):
        if f[y-1][x+i-1]=="
            C+=1
    for j in range(3):
        if f[y+1][x+j-1]=="
            C+=1
    if f[y][x-1]=="
        C+=1
    if f[y][x+1]=="
        C+=1
    if f[y][x]=="
        return "
    return str(C)

L=[] 
for H in range(1,h+1):
    X=[]
    for W in range(1,w+1):
        X.append(hantei(F,H,W))
    L.append(X)
for l in L:
    print("".join(l))

Solution (Python):
import numpy as np

def check(mass,x,y):
  cnt=0
  for in_i in [-1,0,1]:
    for in_j in [-1,0,1]:
      if mass[x+in_i][y+in_j]=="
        cnt+=1
  return cnt

a,b = map(int,input().split())
temp=["." for _ in range(b+2)]
mass=[temp]

for i in range(a):
  mass.append(["."]+list(input())+["."])
mass.append(temp)

for i in range(1,a+1):
  for j in range(1,b+1):
    if mass[i][j]==".":
      mass[i][j] = check(mass,i,j)
    
for i in range(1,a+1):
  temp = [str(temp_i) for temp_i in list(mass[i][1:b+1])]
  sentence = "".join(temp)
  print(sentence)

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

def check(a):
    d=[-1, 0, 1]
    for i in range(h):
        for j in range(w):

            count=0
            if not a[i][j]=="
                for x in d:
                    posx=j+x
                    for y in d:
                        posy=i+y
                        if posx>=0 and posx<w and posy>=0 and posy<h and a[posy][posx]=="
                            count+=1
                a[i][j]=count
    return a

mine=check(mine)
for i in mine:
    print(*i, sep="")

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

f = ['.' * (w + 2)]
for _ in range(h):
  f.append('.' + input() + '.')
f.append('.' * (w + 2))

r = []
for y in range(1, h + 1):
  s = ''
  for x in range(1, w + 1):
    c = 0
    if f[y][x] == '
      s += '
    else:
      c += f[y - 1][x - 1: x + 2].count('
      c += f[y][x - 1: x + 2].count('
      c += f[y + 1][x - 1: x + 2].count('
      s += str(c)
  r.append(s)
print('\n'.join(r))

Solution (Python):
if __name__ == '__main__':
    H, W = map(int, input().split())
    S = [None]*H
    for i in range(H):
        S[i] = list(str(input()))
    S_num = [[0]*W for _ in range(H)]

    for h in range(H):
        for w in range(W):
            if S[h][w] == '
                if h-1 >= 0:
                    S_num[h-1][w] += 1
                    if w-1 >= 0:
                        S_num[h-1][w-1] += 1
                    if w+1 <= W-1:
                        S_num[h-1][w+1] += 1
                if h+1 <= H-1:
                    S_num[h+1][w] += 1
                    if w-1 >= 0:
                        S_num[h+1][w-1] += 1
                    if w+1 <= W-1:
                        S_num[h+1][w+1] += 1
                if w-1 >= 0:
                    S_num[h][w-1] += 1
                if w+1 <= W-1:
                    S_num[h][w+1] += 1
    for h in range(H):
        for w in range(W):
            if S[h][w] == '.':
                S[h][w] = str(S_num[h][w])
    for s in S:
        print(''.join(s))

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

arr = ['.' + s + '.' for s in arr]
arr = ['.'*(w+2)] + arr + ['.'*(w+2)]
arr = [list(s) for s in arr]
arr2 = []
for lst in arr:
    _lst = []
    for s in lst:
        if s=='.': 
            s = 0 
        else: 
            s = 1
        _lst.append(s)
    arr2.append(_lst)

arr = []
for i in range(1, h+1):
    lst = []
    for j in range(1, w+1):
        if arr2[i][j]==1:
            lst.append('
        else:
            cnt = sum(arr2[i][j-1:j+2]) + sum(arr2[i-1][j-1:j+2]) + sum(arr2[i+1][j-1:j+2]) - arr2[i][j]
            
            lst.append(str(cnt))
    arr.append(lst)

arr = [''.join(lst) for lst in arr]
for s in arr:
    print(s)

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

ls = [['.' for _ in range(w+2)] for _ in range(h+2)]
for i in range(1,h+1):
  ls[i][1:w+1]=input()

for h in range(1,h+1):
    for w in range(1,w+1):
        if (ls[h][w]=='
            continue
        c = 0
        for x in range(-1,2):
            for y in range(-1,2):
                if (x==0 and y==0):
                    continue
                elif (ls[h+x][w+y]=='
                    c+=1
        ls[h][w]=str(c)
    else:
        print(''.join(ls[h][1:w+1]))

Solution (Python):
import sys
h, w = [int(i) for i in sys.stdin.readline().split()]
s = [sys.stdin.readline().strip() for _ in range(h)]
result = [[0] * w for _ in range(h)]

for i in range(h):
    for j in range(w):
        if s[i][j] == '
            result[i][j] = '
            continue
        count = 0
        top = (i - 1)
        bottom = (i + 1)
        left = (j - 1)
        right = (j + 1)

        if top >= 0:
            if left >= 0:
                if s[top][left] == '
                    count += 1
            if s[top][j] == '
                count += 1
            if right < w:
                if s[top][right] == '
                    count += 1
        if bottom < h:
            if left >= 0:
                if s[bottom][left] == '
                    count += 1
            if s[bottom][j] == '
                count += 1
            if right < w:
                if s[bottom][right] == '
                    count += 1
        if left >= 0:
            if s[i][left] == '
                count += 1
        if right < w:
            if s[i][right] == '
                count += 1

        result[i][j] = str(count)

for r in result:
    print(''.join(r))

Solution (Python):
H,W=[int(x) for x in input().split()]
M=[['.' for _ in range(W+2)] for _ in range(H+2)]
for h in range(1,H+1):
  M[h][1:W+1]=list(input())

for h in range(1,H+1):
  for w in range(1,W+1):
    if M[h][w]=='
    c=0
    for x in range(-1,2):
      for y in range(-1,2):
        if x==0 and y==0:
          continue
        elif M[h+x][w+y]=='
          c+=1
    M[h][w]=str(c)
  else:
    print(''.join(M[h][1:W+1]))