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):
import numpy as np


def main():
    H, W = map(int, input().split())
    S = [input() for _ in range(H)]
    A = np.zeros([H + 2, W + 2], bool)

    for h in range(H):
        for w in range(W):
            if S[h][w] == "
                A[h + 1, w + 1] = True

    B = np.zeros([H, W], np.int32)
    for h in range(H):
        for w in range(W):
            B[h, w] = np.count_nonzero(A[h:h + 3, w:w + 3])

    B = B.tolist()
    for h in range(H):
        for w in range(W):
            if S[h][w] == "
                B[h][w] = "

    for h in range(H):
        b = list(map(str, B[h]))
        print("".join(b))


main()

Solution (Python):
h,w = map(int,input().split())
Array = [list(map(str,input())) for _ in range(h)]
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 Array[i][j]=='
      continue
    num=0
    for d in range(8):
      ni = i+dy[d]
      nj = j+dx[d]
      if ni<0 or h<=ni:
        continue
      if nj<0 or w<=nj:
        continue
      if Array[ni][nj]=='
        num=num+1
    Array[i][j]=str(num)
    
for i in Array:
  print(*i, sep='')

Solution (Python):
h,w = map(int,input().split())
S = [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 S[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 S[x][y] == "
                cnt += 1
        ans.append(str(cnt))
    print("".join(ans))

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

zero = []
for i in range(W+2):
    zero += '-'
S.append(zero)
for i in range(H):
    tmp = ['-']
    tmp += (list(str(input())))
    tmp += '-'
    S.append(tmp)
S.append(zero)
ans = []
for h in (range(H)):
    tmp = []
    for w in (range(W)):
        if S[h+1][w+1] == '
            tmp.append('
        else:
            count = 0
            for i in range(3):
                for j in range(3):
                    if S[h+i][w+j] == '
                        count += 1
            tmp.append(str(count))
    ans.append(tmp)

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

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

for i in range(H):
    S[i] = list(input())


def count_bomb_num(S, h, w):
    bomb_num = 0

    for i in range(-1, 2):
        ni = h + i
        if ni < 0 or H <= ni:
            continue
        for j in range(-1, 2):
            nj = w + j
            if nj < 0 or W <= nj:
                continue
            if S[ni][nj] == "
                bomb_num += 1

    return bomb_num


for h in range(H):
    for w in range(W):
        if S[h][w] == "
            continue
        S[h][w] = str(count_bomb_num(S, h, w))

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

Solution (Python):
import itertools
H,W = map(int,input().split())
grid = []
for i in range(H):
    a = input()
    l = []
    for i in a:
        if(i == "."):
            i = 0
        l.append(i)
    grid.append(l)
for i in range(H):
    for j in range(W):
        if(grid[i][j] != "
            continue
        for dx,dy in itertools.product([-1,0,1],repeat=2):
            x = i + dx
            y = j + dy
            if(x<0 or H<=x):
                continue
            if(y<0 or W <= y):
                continue
            if grid[x][y] == "
                continue
            grid[x][y] += 1
for Row in grid:
    print(*Row,sep="")

Solution (Python):
import numpy as np


def mnum(i,j):
    Acand=np.asarray([[r,c] for r in range(i-1,i+2) for c in range(j-1,j+2)])
    condh=np.logical_and(Acand[:,0]>=0,Acand[:,0]<H)
    condw=np.logical_and(Acand[:,1]>=0,Acand[:,1]<W)
    cond=np.logical_and(condh,condw)
    A=Acand[cond,:] 
    mcount=0 

    for a in A: 
        mcount+=int(S[a[0],a[1]]=="

    return mcount

H,W=list(map(int,input().split(" ")))
S=np.full((H,W),".",np.str)
MC=np.zeros((H,W),np.str) 


for s in range(H):
    S[s,:]=list(input())


for i in range(H):
    for j in range(W):
        if S[i,j]==".": 
            MC[i,j]=str(mnum(i,j)) 
        elif S[i,j]=="
            MC[i,j]="

    print("".join(MC[i,:]))

Solution (Python):
import sys

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]

def main():
    h, w = MI()
    t = [["."] * (w + 2)] + [["."] + list(input()) + ["."] for _ in range(h)] + [["."] * (w + 2)]
    for i in range(1, h + 1):
        for j in range(1, w + 1):
            if t[i][j] == "
            cnt=0
            for ii in range(i-1,i+2):
                for jj in range(j-1,j+2):
                    if t[ii][jj]=="
            t[i][j]=cnt

    for tk in t[1:1+h]:
        print(*tk[1:1+w],sep="")

main()

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 = [[x for x in str(input())] for _ in range(h)]
S.append(['/']*w)
S.insert(0, ['/']*w)

for i in range(h+2):
    S[i].append('/')
    S[i].insert(0, '/')

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

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

for k in range(h):
    print(''.join(ans[k]))

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

s = [input() for _ in range(h)]

padded = []

for i in range(h + 2):
    if i == 0 or i == h + 1:
        padded.append("." * (w + 2))
    else:
        padded.append("." + s[i - 1] + ".")


out = [[0] * w for _ in range(h)]

for i in range(1, h + 1):
    for j in range(1, w + 1):
        c = 0
        if padded[i][j] == "
            out[i - 1][j - 1] = "
        else:
            for k in range(8):
                dx = [-1, 0, 1, -1, 1, -1, 0, 1]
                dy = [-1, -1, -1, 0, 0, 1, 1, 1]
                c += padded[i + dx[k]][j + dy[k]] == "
            out[i - 1][j - 1] = c
        

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

Solution (Python):
import numpy as np

def check_surrounding(array, h, w):
    count = 0
    if array[h, w] == "
        count+=1
    if array[h, w+1] == "
        count+=1
    if array[h, w+2] == "
        count+=1
    if array[h+1, w] == "
        count+=1
    if array[h+1, w+2] == "
        count+=1
    if array[h+2, w] == "
        count+=1
    if array[h+2, w+1] == "
        count+=1
    if array[h+2, w+2] == "
        count+=1
        
    return count


H, W = map(int, input().split())
grid = np.array([["0"]*(W+2)]*(H+2))
for h in range(H+2):
    if (h == 0) or (h==(H+1)):
        for w in range(W+2):
            grid[h, w] = "-"
        continue
    inputs =  input()
    for w in range(W+2):
        if (w == 0) or (w==(W+1)):
            grid[h, w] = "-"
        else:
            grid[h, w] = inputs[w-1]
            


for h in range(H):
    for w in range(W):
        if grid[h+1, w+1] == "
            print("
        else:
            print(check_surrounding(grid, h, w), end="")
    print()

Solution (Python):
H,W=map(int,input().split())
masu=[[None for i in range(W+2)] for j in range(H+2)]
score=[[None for i in range(W)] for j in range(H)]
for i in range(1,H+1):
    S=list(input())
    for j in range(len(S)):
        masu[i][j+1]=S[j]

for i in range(1,H+1):
    for j in range(1,W+1):
        if masu[i][j]=="
            score[i-1][j-1]="
            continue
        count_score=0
        if masu[i-1][j-1]=="
        if masu[i-1][j]=="
        if masu[i-1][j+1]=="
        if masu[i][j-1]=="
        if masu[i][j+1]=="
        if masu[i+1][j-1]=="
        if masu[i+1][j]=="
        if masu[i+1][j+1]=="

        score[i-1][j-1]=count_score
for i in score:
    print("".join([str(_) for _ in i]))

Solution (Python):
from sys import stdin
import math
import bisect
import heapq
import numpy as np
from math import factorial
inf = 10**10

h,w = [int(x) for x in stdin.readline().rstrip().split()]

li = [["." for i in range(w+2)]for j in range(h+2)]
for i in range(1,h+1):
    s = stdin.readline().rstrip()
    for j in range(1,w+1):
        li[i][j] = s[j-1]

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

for i in range(1,h+1):
    for j in range(1,j+1):
        if li[i][j] == "
            lin[i-1][j-1] = "
        else:
            point = 0
            if li[i-1][j-1] == "
                point += 1
            if li[i-1][j] == "
                 point += 1
            if li[i-1][j+1] == "
                 point += 1
            if li[i][j-1] == "
                 point += 1
            if li[i][j+1] == "
                 point += 1
            if li[i+1][j-1] == "
                 point += 1
            if li[i+1][j] == "
                 point += 1
            if li[i+1][j+1] == "
                 point += 1
            lin[i-1][j-1] = str(point)

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

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

for i in range(h):
    for j in range(w):
        ans = 0
        if ls[i][j] == "
          ans = "
        else:
          if j+1 <= w-1:
            if ls[i][j+1] == "
                ans += 1
            if i+1 <= h-1:
              if ls[i+1][j+1] == "
                ans += 1
            if i-1 >= 0:
              if ls[i-1][j+1] == "
                ans += 1
          if i+1 <= h-1:
            if ls[i+1][j] == "
                ans += 1
          if j-1 >= 0:
            if ls[i][j-1] == "
                ans += 1
            if i+1 <= h-1:
              if ls[i+1][j-1] == "
                ans += 1
            if i-1 >= 0:
              if ls[i-1][j-1] == "
                ans += 1
          if i-1 >= 0:
            if ls[i-1][j] == "
                ans += 1
         
        ls2[i][j] = ans
        
for i in range(h):
  for j in range(w):
  	print(ls2[i][j],end="")
  print("")

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

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

for i in range(len(S)):
	row = list()
	for j in range(len(S[i])):
		if S[i][j] == '
			row.append('
			continue 
		count = 0
		check_list =[[i-1,j-1],[i-1,j],[i-1,j+1],[i,j-1],[i,j+1],[i+1,j-1],[i+1,j],[i+1,j+1]]
		for c in range(len(check_list)):
			if check_list[c][0] >= 0 and check_list[c][0] < len(S) and check_list[c][1] >= 0 and check_list[c][1] < len(S[i]):
				if S[check_list[c][0]][check_list[c][1]] == '
					count += 1
		row.append(str(count))
	print(''.join(row))

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

Solution (Python):
import numpy as np
line = input().split()
h = int(line[0])
w = int(line[1])
grid = []
for i in range(h):
  grid.append(list(input()))
              
grid = np.array(grid)


def get8(grid,y,x):
  v = [None]*8
  try :
    if y-1 < 0 :
      pass
    else:
      v[0] = grid[y-1][x]
  except :
    pass
  try :
    if y-1 < 0 :
      pass
    else :
      v[1] = grid[y-1][x+1]
  except :
    pass  
  try :
    v[2] = grid[y][x+1]
  except :
    pass
  try :
    v[3] = grid[y+1][x+1]
  except :
    pass
  try :
    v[4] = grid[y+1][x]
  except :
    pass
  try :
    if x-1 < 0 :
      pass
    else:
      v[5] = grid[y+1][x-1]
  except :
    pass
  try :
    if x-1 < 0 :
      pass
    else:
      v[6] = grid[y][x-1]
  except :
    pass
  try :
    if y-1 < 0 or x-1 < 0 :
      pass
    else :
    	v[7] = grid[y-1][x-1]
        
  except :
    pass
  
  return v

for i in range(h) :
  for j in range(w) :
    v = get8(grid,i,j)
    if grid[i][j] == "." :
    	grid[i][j] = v.count("
  
  
for i in range(h) :
  print("".join(grid[i]))

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

Solution (Python):
def examB():
    H, W = LI()
    St = [S() for _ in range(H)]
    ans = [[] for _ in range(H)]
    for i1,l in enumerate(St):
        for i2,j in enumerate(l):
            cur = int(0)
            if j==".":
                for k in range(3):
                    for p in range(3):
                        if 0<=i1-1+k<H and 0<=i2-1+p<W:
                            if St[i1 - 1 + k][i2 - 1 + p] == "
                                cur += 1
            else:
                cur = "
            ans[i1].append(str(cur))
    for v in ans:
        print("".join(map(str,v)))



import sys,copy,bisect,itertools
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def S(): return sys.stdin.readline().strip()
mod = 10**9 + 7
inf = float('inf')

examB()

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

def calc(y,x):
  if y<0 or y>=H or x<0 or x>=W:
    return 0
  if grid[y][x]=="
    return 1
  else:
    return 0

for i in range(H):
  line=""
  for j in range(W):
    ans=0
    if grid[i][j]=="
      line+=("
    else:
      ans+=calc(i-1,j-1)
      ans+=calc(i-1,j)
      ans+=calc(i-1,j+1)
      ans+=calc(i,j-1)
      ans+=calc(i,j+1)
      ans+=calc(i+1,j-1)
      ans+=calc(i+1,j)
      ans+=calc(i+1,j+1)
      line+=str(ans)
  print(line)

Solution (Python):
import numpy as np

H, W = map(int, input().split())
input_map = np.array([input() for i in range(H)])

mine_map = np.array([[0 if j == "." else "

for y in range(H):
	for x in range(W):
		if mine_map[y][x] == "
			pass
		else:	
			y_lower = 0 if y-1<0 else y-1
			y_upper = H if y+2>H else y+2
			x_lower = 0 if x-1<0 else x-1
			x_upper = W if x+2>W else x+2

			mine_map[y][x] = np.sum(mine_map[y_lower:y_upper, x_lower:x_upper] == "

		print(mine_map[y][x], end = "")
	print("")

Solution (Python):
h,w=map(int,input().split())
s=[]
cnt=0
for i in range(h):
    s.append(list(input()))
if h==1 and w==1:
    if s[0][0]=='
        print('
    else:
        print(0)
elif h==1:
    for i in range(w):
        if s[0][i]=='
            continue
        if i==0:
            if s[0][i+1]=='
                cnt+=1
            s[0][i]=str(cnt)
            cnt=0
        elif i==w-1:
            if s[0][i-1]=='
                cnt+=1
            s[0][i]=str(cnt)
            cnt=0
        else:
            if s[0][i+1]=='
                cnt+=1
            if s[0][i-1]=='
                cnt+=1
            s[0][i]=str(cnt)
            cnt=0
    ans=''
    for l in range(h):
        for b in range(w):
            ans+=s[l][b]
        print(ans)
        ans=''
elif w==1:
    for i in range(h):
        if s[i][0]=='
            continue
        if i==0:
            if s[i+1][0]=='
                cnt+=1
            s[i][0]=str(cnt)
            cnt=0
        elif i==h-1:
            if s[i-1][0]=='
                cnt+=1
            s[i][0]=str(cnt)
            cnt=0
        else:
            if s[i+1][0]=='
                cnt+=1
            if s[i-1][0]=='
                cnt+=1
            s[i][0]=str(cnt)
            cnt=0
    ans=''
    for l in range(h):
        for b in range(w):
            ans+=s[l][b]
        print(ans)
        ans=''
else:
    for j in range(h):
        for k in range(w):
            if j==0:
                if s[j][k]=='
                    continue
                else:
                    if k==0:
                        if s[j][k+1]=='
                            cnt+=1
                        if s[j+1][k]=='
                            cnt+=1
                        if s[j+1][k+1]=='
                            cnt+=1
                        s[j][k]=str(cnt)
                        cnt=0
                    elif k==w-1:
                        if s[j][k-1]=='
                            cnt+=1
                        if s[j+1][k]=='
                            cnt+=1
                        if s[j+1][k-1]=='
                            cnt+=1
                        s[j][k]=str(cnt)
                        cnt=0
                    else:
                        if s[j][k+1]=='
                            cnt+=1
                        if s[j+1][k]=='
                            cnt+=1
                        if s[j+1][k+1]=='
                            cnt+=1
                        if s[j][k-1]=='
                            cnt+=1
                        if s[j+1][k-1]=='
                            cnt+=1
                        s[j][k]=str(cnt)
                        cnt=0
            elif j==h-1:
                if s[j][k]=='
                    continue
                else:
                    if k==0:
                        if s[j][k+1]=='
                            cnt+=1
                        if s[j-1][k]=='
                            cnt+=1
                        if s[j-1][k+1]=='
                            cnt+=1
                        s[j][k]=str(cnt)
                        cnt=0
                    elif k==w-1:
                        if s[j][k-1]=='
                            cnt+=1
                        if s[j-1][k]=='
                            cnt+=1
                        if s[j-1][k-1]=='
                            cnt+=1
                        s[j][k]=str(cnt)
                        cnt=0
                    else:
                        if s[j][k+1]=='
                            cnt+=1
                        if s[j-1][k]=='
                            cnt+=1
                        if s[j-1][k+1]=='
                            cnt+=1
                        if s[j][k-1]=='
                            cnt+=1
                        if s[j-1][k-1]=='
                            cnt+=1
                        s[j][k]=str(cnt)
                        cnt=0 
            elif 0<j<h-1:
                if s[j][k]=='
                    continue
                else:
                    if k==0:
                        if s[j][k+1]=='
                            cnt+=1
                        if s[j-1][k]=='
                            cnt+=1
                        if s[j-1][k+1]=='
                            cnt+=1
                        if s[j+1][k]=='
                            cnt+=1
                        if s[j+1][k+1]=='
                            cnt+=1
                        s[j][k]=str(cnt)
                        cnt=0
                    elif k==w-1:
                        if s[j][k-1]=='
                            cnt+=1
                        if s[j-1][k]=='
                            cnt+=1
                        if s[j-1][k-1]=='
                            cnt+=1
                        if s[j+1][k]=='
                            cnt+=1
                        if s[j+1][k-1]=='
                            cnt+=1
                        s[j][k]=str(cnt)
                        cnt=0
                    else:
                        if s[j][k+1]=='
                            cnt+=1
                        if s[j-1][k]=='
                            cnt+=1
                        if s[j-1][k+1]=='
                            cnt+=1
                        if s[j-1][k-1]=='
                            cnt+=1
                        if s[j+1][k]=='
                            cnt+=1
                        if s[j+1][k+1]=='
                            cnt+=1
                        if s[j][k-1]=='
                            cnt+=1
                        if s[j+1][k-1]=='
                            cnt+=1
                        s[j][k]=str(cnt)
                        cnt=0
    ans=''
    for l in range(h):
        for b in range(w):
            ans+=s[l][b]
        print(ans)
        ans=''

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

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

                try:S[i-1][j-1]
                except IndexError:pass
                else:
                    if  i-1>=0 and j-1>=0 and S[i-1][j-1]!='
                        S[i-1][j-1]+=1

                try:S[i-1][j]
                except IndexError:pass
                else:
                    if i-1>=0 and S[i-1][j]!='
                        S[i-1][j]+=1

                try:S[i-1][j+1]
                except IndexError:pass
                else:
                    if i-1>=0 and S[i-1][j+1]!='
                        S[i-1][j+1]+=1

                try:S[i][j-1]
                except IndexError:pass
                else:
                    if  j-1>=0 and S[i][j-1]!='
                        S[i][j-1]+=1
                        
                try:S[i][j+1]
                except IndexError:pass
                else:
                    if  S[i][j+1]!='
                        S[i][j+1]+=1

                try:S[i+1][j-1]
                except IndexError:pass
                else:
                    if j-1>=0 and S[i+1][j-1]!='
                        S[i+1][j-1]+=1

                try:S[i+1][j]
                except IndexError:pass
                else:
                    if S[i+1][j]!='
                        S[i+1][j]+=1

                try:S[i+1][j+1]
                except IndexError:pass
                else:
                    if S[i+1][j+1]!='
                        S[i+1][j+1]+=1
for i in S:
    print(*i,sep="")

Solution (Python):
H,W = map(int,input().split())
S = [list(input()) for i in range(H)]
S.insert(0,[0]*W)
S.append([0]*W)
for i in range(len(S)):
    S[i].insert(0,0)
    S[i].append(0)
dx = [1,0,-1,0,1,-1,-1,1]
dy = [0,1,0,-1,1,1,-1,-1]
tmp = [[0] * W for i in range(H)]
for i in range(1,H+1):
    for j in range(1,W+1):
        if S[i][j] == "
            tmp[i-1][j-1] = "
            continue
        num = 0
        for k in range(len(dx)):
            if S[i+dx[k]][j+dy[k]] == "
                num += 1
                tmp[i-1][j-1] = num
for j in range(len(tmp)):
    print(*tmp[j],sep = "")

Solution (Python):

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

for i in range(h):
    for j in range(w):
        if s[i][j] == '.':
            bombs = 0

            if i - 1 >= 0 and s[i - 1][j] == '
                bombs += 1
            if i - 1 >= 0 and j - 1 >= 0 and s[i - 1][j - 1] == '
                bombs += 1
            if j - 1 >= 0 and s[i][j - 1] == '
                bombs += 1
            if i + 1 < h and j - 1 >= 0 and s[i + 1][j - 1] == '
                bombs += 1
            if i + 1 < h and s[i + 1][j] == '
                bombs += 1
            if i + 1 < h and j + 1 < w and s[i + 1][j + 1] == '
                bombs += 1
            if j + 1 < w and s[i][j + 1] == '
                bombs += 1
            if i - 1 >= 0 and j + 1 < w and s[i - 1][j + 1] == '
                bombs += 1
        
            s[i] = s[i][:j] + str(bombs) + s[i][j + 1:]

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

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

Solution (Python):
H,W = map(int,input().split(' '))
N = [[i for i in input()] for i in range(H)]
lst = [-1,0,1]
for i in range(H):
    for x in range(W):
        if N[i][x] == '.':
            ans = 0
            for i1 in lst:
                for x1 in lst:
                    if (i+i1 >= 0 and x+x1 >= 0) and (i+i1 < H and x+x1 < W):
                        if N[i+i1][x+x1] == "
                            ans = ans + 1
            N[i][x] = ans

[print(''.join([str(x) for x in i])) for i in N]

Solution (Python):

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

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

for i in range(1,H+1):
    S = list(input())
    S_list[i][1:W+1] = S
    
ans = [["
    
for i in range(1,H+1):
    for j in range(1,W+1):
        if S_list[i][j] != "
            ans[i][j] = S_list[i-1][j-1:j+2].count("
            
for i in range(1,H+1):
    print(''.join(map(str, ans[i][1:-1])))

Solution (Python):
import sys
input = sys.stdin.readline


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


def f(i, j):
    x = 0
    for h in range(max(0, i-1), min(i+2, H)):
        for w in range(max(0, j-1), min(j+2, W)):
            if S[h][w] == "
                x += 1
    return x


def main():
    for y in range(H):
        s = ""
        for x in range(W):
            if S[y][x] == ".":
                s += str(f(y, x))
            else:
                s += "
        print(s)


if __name__ == '__main__':
    main()

Solution (Python):
H,W=map(int,input().split())
ls=[]
ls.append("."*(W+2))
for i in range(H):
    ls.append("."+input()+".")
ls.append("."*(W+2))
lans=[]
for i in range(1,H+1):
    clans=""
    for j in range(1,W+1):
        if ls[i][j]==".":
            clans+=str((ls[i-1][j-1]=="
        else:
            clans+=ls[i][j]
    lans.append(clans)
for i in range(H):
    print(lans[i])

Solution (Python):
H, W = map(int, input().split())
grid = []
ans = []
grid.append(['.'] * (W + 2))
for i in range(H):
    si = input()
    s = '.' + si + '.'
    grid.append(list(s))
    ans.append([0] * W)
grid.append(['.'] * (W + 2))
for i in range(1, H + 1):
    for j in range(1, W + 1):
        if grid[i][j] == '
            ans[i - 1][j - 1] = '
        if grid[i + 1][j] == '
            if ans[i - 1][j - 1] != '
                ans[i - 1][j - 1] += 1
        if grid[i - 1][j] == '
            if ans[i - 1][j - 1] != '
                ans[i - 1][j - 1] += 1
        if grid[i][j + 1] == '
            if ans[i - 1][j - 1] != '
                ans[i - 1][j - 1] += 1
        if grid[i][j - 1] == '
            if ans[i - 1][j - 1] != '
                ans[i - 1][j - 1] += 1
        if grid[i + 1][j + 1] == '
            if ans[i - 1][j - 1] != '
                ans[i - 1][j - 1] += 1
        if grid[i + 1][j - 1] == '
            if ans[i - 1][j - 1] != '
                ans[i - 1][j - 1] += 1
        if grid[i - 1][j + 1] == '
            if ans[i - 1][j - 1] != '
                ans[i - 1][j - 1] += 1
        if grid[i - 1][j - 1] == '
            if 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())
ss = []
ls = []


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

for i in range(1, h+1):
    for j in range(1, w+1):
        if ss[i][j] == '.':
            n = (
                ss[i-1][j-1],
                ss[i-1][j],
                ss[i-1][j+1],
                ss[i][j-1],
                ss[i][j+1],
                ss[i+1][j-1],
                ss[i+1][j],
                ss[i+1][j+1],
            ).count('
            print(n, end='')
        else:
            print('
    print()

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

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

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 y in range(H):
    for x in range(W):
        if S[y][x] == ".":
            cnt = 0
            for c in range(8):
                nx = x + dx[c]
                ny = y + dy[c]
                if 0 <= nx <= W-1 and 0<= ny <= H-1:
                    if S[ny][nx] == "
                        cnt += 1
            S[y][x] = str(cnt)
for i in range(H):
    print(("".join(S[i])))