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 = []
for i in range(H):
  S.append(input())
for j in range(H):
  answer = list(S[j])
  for k in range(W):
    cnt = 0
    if S[j][k] == '
      pass
    if S[j][k] == '.':
      if j-1 >= 0:
        if k-1 >= 0:
          if S[j-1][k-1] == '
            cnt += 1
        if S[j-1][k] == '
          cnt += 1
        if k+1 <= W-1:
          if S[j-1][k+1] == '
            cnt += 1
      if k-1 >= 0:
        if S[j][k-1] == '
          cnt += 1
      if k+1 <= W-1:
        if S[j][k+1] == '
          cnt += 1
      if j+1 <= H-1:
        if k-1 >= 0:
          if S[j+1][k-1] == '
            cnt += 1
        if S[j+1][k] == '
          cnt += 1
        if k+1 <= W-1:
          if S[j+1][k+1] == '
            cnt += 1
      answer[k] = str(cnt)
  print(''.join(answer))

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

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

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

        cnt = 0
        for k in range(0, 8):
            ni = i + dy[k]
            nj = j + dx[k]

            if not (ni < 0 or h <= ni) and not (nj < 0 or w <= nj) and s[ni][nj] == '
                cnt += 1

        s[i][j] = str(cnt)

[print(''.join(s[ii])) for ii in range(0, h)]

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

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

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

        cnt = 0
        for k in range(0, 8):
            ni = i + dy[k]
            nj = j + dx[k]

            if not (ni < 0 or h <= ni) and not (nj < 0 or w <= nj) and s[ni][nj] == '
                cnt += 1

        s[i][j] = str(cnt)

[print(''.join(s[ii])) for ii in range(0, h)]

Solution (Python):
H, W = map(int, input().split())
S = []
for _ in range(H):
    S.append(input())
ans = [[0] * W for _ in range(H)]
for y in range(H):
    for x in range(W):
        if S[y][x] == "
            ans[y][x] = "
            if not (y == H-1 or x == W-1):
                if S[y+1][x+1] != "
                    ans[y+1][x+1] += 1
            if not y == H - 1:
                if S[y+1][x] != "
                    ans[y+1][x] += 1
            if not x == W - 1:
                if S[y][x+1] != "
                    ans[y][x+1] += 1
            if not (y == H - 1 or x == 0):
                if S[y+1][x-1] != "
                    ans[y+1][x-1] += 1
            if not (y == 0 or x == W - 1):
                if S[y-1][x+1] != "
                    ans[y-1][x+1] += 1
            if not x == 0:
                if S[y][x-1] != "
                    ans[y][x-1] += 1
            if not y == 0:
                if S[y-1][x] != "
                    ans[y-1][x] += 1
            if not (y == 0 or x == 0):
                if S[y-1][x-1] != "
                    ans[y-1][x-1] += 1

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

Solution (Python):
h,w = map(int,input().split())
ls = []
side = [[-1,-1],[-1,0],[-1,1],[0,-1],[0,1],[1,-1],[1,0],[1,1]]
for i in range(h):
  ls.append(list(input()))
for i in range(h):
  ans2 = ""
  for j in range(w):
    ans = 0
    if ls[i][j] == "
      ans2 += "
    else:
      for k in range(8):
        if i+side[k][0] < 0 or j+side[k][1] < 0 or h <=i+side[k][0] or w <= j+side[k][1]:
          continue
        else:
          ans += (1 if ls[i+side[k][0]][j+side[k][1]] == "
      ans2 += str(ans)
  print(ans2)

Solution (Python):
h,w = map(int,input().split())
ls = []
side = [[-1,-1],[-1,0],[-1,1],[0,-1],[0,1],[1,-1],[1,0],[1,1]]
for i in range(h):
  ls.append(list(input()))
for i in range(h):
  ans2 = ""
  for j in range(w):
    ans = 0
    if ls[i][j] == "
      ans2 += "
    else:
      for k in range(8):
        if i+side[k][0] < 0 or j+side[k][1] < 0 or h <=i+side[k][0] or w <= j+side[k][1]:
          continue
        else:
          ans += (1 if ls[i+side[k][0]][j+side[k][1]] == "
      ans2 += str(ans)
  print(ans2)

Solution (Python):
h,w = map(int,input().split())
ls = []
side = [[-1,-1],[-1,0],[-1,1],[0,-1],[0,1],[1,-1],[1,0],[1,1]]
for i in range(h):
  ls.append(list(input()))
for i in range(h):
  ans2 = ""
  for j in range(w):
    ans = 0
    if ls[i][j] == "
      ans2 += "
    else:
      for k in range(8):
        if i+side[k][0] < 0 or j+side[k][1] < 0 or h <=i+side[k][0] or w <= j+side[k][1]:
          continue
        else:
          ans += (1 if ls[i+side[k][0]][j+side[k][1]] == "
      ans2 += str(ans)
  print(ans2)

Solution (Python):
h,w = map(int,input().split())
ls = []
side = [[-1,-1],[-1,0],[-1,1],[0,-1],[0,1],[1,-1],[1,0],[1,1]]
for i in range(h):
  ls.append(list(input()))
for i in range(h):
  ans2 = ""
  for j in range(w):
    ans = 0
    if ls[i][j] == "
      ans2 += "
    else:
      for k in range(8):
        if i+side[k][0] < 0 or j+side[k][1] < 0 or h <=i+side[k][0] or w <= j+side[k][1]:
          continue
        else:
          ans += (1 if ls[i+side[k][0]][j+side[k][1]] == "
      ans2 += str(ans)
  print(ans2)

Solution (Python):
h,w = map(int,input().split())
ls = []
yside = {-1,-1,-1,0,0,1,1,1}
xside = {-1,0,1,-1,1,-1,0,1}
side = [[-1,-1],[-1,0],[-1,1],[0,-1],[0,1],[1,-1],[1,0],[1,1]]
for i in range(h):
  ls.append(list(input()))
for i in range(h):
  ans2 = ""
  for j in range(w):
    ans = 0
    if ls[i][j] == "
      ans2 += "
    else:
      for k in range(8):
        if i+side[k][0] < 0 or j+side[k][1] < 0 or h <=i+side[k][0] or w <= j+side[k][1]:
          continue
        else:
          ans += (1 if ls[i+side[k][0]][j+side[k][1]] == "
      ans2 += str(ans)
  print(ans2)

Solution (Python):
H, W = map(int, input().split())
S = ['.'+input()+'.' if i > 0 and i < H + 1 else '.' * (W+2) for i in range(H+2)]
for i in range(1, H+1):
    s = ''
    for j in range(1, W+1):
        if S[i][j] == '
        else: s += 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('
    print(s)

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

Solution (Python):
h,w=map(int,input().split())
li=[input() for _ in range(h)]
for i in range(h):
    res=""
    for j in range(w):
        cnt=0
        if li[i][j]==".":
            for x in range(-1,2):
                for y in range(-1,2):
                    if 0<=i+x<h and 0<=j+y<w and li[i+x][j+y]=="
                        cnt+=1
            res+=str(cnt)
        else:
            res+="
    print(res)

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

tate=[-1,-1,-1,0,0,1,1,1]
yoko=[-1,0,1,-1,1,-1,0,1]

for i in range(1,h+1):
    l2=[]
    for j in range(1,w+1):
        if l[i][j]=="
            l2.append("
        else:
            cnt=0
            for k in range(8):
                if l[i+tate[k]][j+yoko[k]]=="
                    cnt+=1
            l2.append(str(cnt))
    print("".join(l2))

Solution (Python):
h,w=map(int,input().split())
maze=[]
lst=['.']*(w+2)
maze.append(lst)

for i in range(h):
  lst2=list(input())
  lst2=['.']+lst2+['.']
  maze.append(lst2)

maze.append(lst)


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

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

for i in range(1,h+1):
  for j in range(1,w+1):
    cnt=0
    if maze[i][j]=='
      ans[i-1][j-1]='
      continue

    for k in range(8):
      s=i+dx[k]
      t=j+dy[k]
      if maze[s][t]=='
        cnt+=1
    ans[i-1][j-1]=str(cnt)
    
for i in range(h):
  answer=''
  for j in range(w):
    answer+=ans[i][j]
  
  print(answer)

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


for k in range(H):
  for l in range(W):
    if S[k][l]=="
      try:

        S[k-1][l-1]+=1
        if k<=0 or l<=0:
          S[k-1][l-1]-=1
      except:

        pass
      try:
        S[k-1][l]+=1
        if k<=0:
          S[k-1][l]-=1
      except:

        pass
      try:
        S[k-1][l+1]+=1
        if k<=0:
          S[k-1][l+1]-=1
      except:

        pass
      try:
        S[k][l-1]+=1
        if l<=0:
          S[k][l-1]-=1
      except:

        pass
      try:
        S[k][l+1]+=1
      except:

        pass
      try:
        S[k+1][l-1]+=1
        if l<=0:
          S[k+1][l-1]-=1
      except:

        pass
      try:
        S[k+1][l]+=1
      except:

        pass
      try:
        S[k+1][l+1]+=1
      except:

        pass
for m in S:
  ans=""
  for n in m:
    ans+=str(n)
  print(ans)

Solution (Python):
import numpy as np
H,W = (int(T) for T in input().split())
Map = np.zeros((H+2,W+2),dtype=bool)
for TH in range(0,H):
    Map[TH+1,1:W+1] = [True if T=='
Disp = np.full((H,W),'
for TH in range(0,H):
    for TW in range(0,W):
        if Map[TH+1,TW+1]==False:
            Disp[TH,TW] = np.sum(Map[TH:TH+3,TW:TW+3])
    print(''.join(Disp[TH,:]))

Solution (Python):
h, w = map(int, input().split())
s = [list(input()) for i in range(h)]
l = [["
for i in range(h):
    for j in range(w):
        if s[i][j] == ".":
            l[i].append(0)
        else:
            l[i].append("
    l[i].append("
l = [["
pos = []
for i in range(1, h+1):
    for j in range(1, w+1):
        if l[i][j] == "
            pos.append([i, j])
for i in pos:
    x, y = i
    for j in [[x-1, y-1], [x, y-1], [x+1, y-1], [x-1, y], [x+1, y], [x-1, y+1], [x, y+1], [x+1, y+1]]:
        if l[j[0]][j[1]] != "
            l[j[0]][j[1]] += 1
for i in range(1, h+1):
    print("".join(list(map(str, l[i][1:-1]))))

Solution (Python):
def chk(s,x,y):
    cnt = 0
    if s[y][x]!='
        for i in [-1,0,1]:
            for j in [-1,0,1]:
                if 0<=x+i<w and 0<=y+j<h:
                    if s[y+j][x+i]=='
                        cnt += 1
        return str(cnt)
    else:
        return '

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




for i in range(h):
    for j in range(w):
        print(chk(gazo,j,i),end='')
    print('')

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

sn = [input() for _ in range(h)]
csn = [[0] * w for i in range(h)]

for i in range(h):
  for j in range(w):
    if sn[i][j] == "
      csn[i][j] = -1
      if i-1 >= 0 and sn[i-1][j] == "." :
        csn[i-1][j] += 1
        
      if i+1 <= h-1 and sn[i+1][j] == "." :
        csn[i+1][j] += 1   
        
      if j-1 >= 0 and sn[i][j-1] == "." :
        csn[i][j-1] += 1
        
      if j+1 <= w-1 and sn[i][j+1] == "." :
        csn[i][j+1] += 1        

      if i-1 >= 0 and j-1 >= 0 and sn[i-1][j-1] == "." :
        csn[i-1][j-1] += 1
 
      if i-1 >= 0 and j+1 <= w-1  and sn[i-1][j+1] == "." :
        csn[i-1][j+1] += 1 

      if i+1 <= h-1 and j-1 >= 0 and sn[i+1][j-1] == "." :
        csn[i+1][j-1] += 1        
        
      if i+1 <= h-1 and j+1 <= w-1 and sn[i+1][j+1] == "." :
        csn[i+1][j+1] += 1    
        
for i in range(h):
  for j in range(w):  
    if csn[i][j] == -1 :
      print("
    else :
      print(csn[i][j], end='')
  print()

Solution (Python):
h,w = map(int, input().split())
s = ["." + str(input()) + "." for i in range(h)]
s.append("."*(w+2))
s.insert(0,"."*(w+2))
l = []

for h in range(1,h+1):
    s1 = ""
    for j in range(1,w+1):
        if s[h][j] == "
            s1 += "
        else:
            num = 0
            for x in range(-1,2):
                for y in range(-1,2):
                    if s[h+x][j+y] == "
                        num += 1
            s1 += str((num))
    l.append(s1)
for z in l:
    print(z)

Solution (Python):

H, W = map(int, input().split())
S = [list(input()) for _ in range(H)]
moves = ((1, 0), (0, 1), (0, -1), (-1, 0), (-1, -1), (-1, 1), (1, -1), (1,1))
c = [[0]*W for _ in range(H)]
for i in range(H):
  for j in range(W):
    cnt = 0
    if S[i][j] == "
      c[i][j] = "
      continue
    for dy, dx in moves:
      ny = i + dy
      nx = j + dx
      if 0 <= ny < H and 0 <= nx < W and S[ny][nx] == '
        cnt += 1
    c[i][j] = cnt

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

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



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

    if S[h][w] == ".":
      cnt = 0

      for h1 in range(max(0,h-1),min(H,h+2)):
        for w1 in range(max(0,w-1),min(W,w+2)):
          if S[h1][w1] =="
            cnt += 1
      S[h][w] = cnt
    
for h in range(H):
  print("".join(map(str,S[h])))

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

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

            ist = -1
            ien = 2
            jst = -1
            jen = 2

            if h == 0:
                ist = 0
            if h == H - 1:
                ien = 1
            if w == 0:
                jst = 0
            if w == W - 1:
                jen = 1

            for i in range(ist, ien):
                for j in range(jst, jen):
                    if S[h + i][w + j] == "
                        s += 1

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

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

Solution (Python):
H, W = map(int, input().split())
data = []
for i in range(H):
  data.append(input())
x = [1,1,1,0,-1,-1,-1,0]
y = [1,0,-1,-1,-1,0,1,1]
ans = []
for i in range(H):
  s = ""
  for j in range(W):
    if data[i][j] == "
      s += "
      continue
    else:
      count = 0
      for k in range(8):
        if 0 <= i + x[k] and i + x[k] < H and 0 <= j + y[k] and j + y[k] < W:
          if data[i + x[k]][j + y[k]] == "
            count += 1
      count = str(count)
      s += count
  ans.append(s)
for i in range(H):
  print(ans[i])

Solution (Python):
def solve():
  H, W = map(int, input().split())
  S = [0]*(H+2)
  S[0] = '*'*(W+2)
  S[H+1] = '*'*(W+2)
  for h in range(1,H+1):
    S[h] = '*' + input() + '*'
  ans = [[0]*W for _ in range(H)]
  dh = [1,1,1,0,-1,-1,-1,0]
  dw = [1,0,-1,-1,-1,0,1,1]
  for h in range(1,H+1):
    for w in range(1,W+1):
      if S[h][w]=='
        ans[h-1][w-1]='
        continue
      for i in range(8):
        if S[h+dh[i]][w+dw[i]]=='
          ans[h-1][w-1] += 1
    ans[h-1] = ''.join(map(str,ans[h-1]))
  return ans
print(*solve(),sep='\n')

Solution (Python):
import sys, re
from math import ceil, floor, sqrt, pi, factorial, gcd
from copy import deepcopy
from collections import Counter, deque
from heapq import heapify, heappop, heappush
from itertools import accumulate, product, combinations, combinations_with_replacement
from bisect import bisect, bisect_left, bisect_right
from functools import reduce
from decimal import Decimal, getcontext

def i_input(): return int(input())
def i_map(): return map(int, input().split())
def i_list(): return list(i_map())
def i_row(N): return [i_input() for _ in range(N)]
def i_row_list(N): return [i_list() for _ in range(N)]
def s_input(): return input()
def s_map(): return input().split()
def s_list(): return list(s_map())
def s_row(N): return [s_input for _ in range(N)]
def s_row_str(N): return [s_list() for _ in range(N)]
def s_row_list(N): return [list(s_input()) for _ in range(N)]
def lcm(a, b): return a * b // gcd(a, b)
sys.setrecursionlimit(10 ** 6)
INF = float('inf')
MOD = 10 ** 9 + 7
num_list = []
str_list = []

def main():
    h, w = i_map()
    str_list = s_row_list(h)

    for i in range(h):
        for j in range(w):
            if str_list[i][j] == '
                continue
            count = 0
            for dx, dy in ([1, 0], [-1, 0], [0, 1], [0, -1], [1, 1], [1, -1], [-1, 1], [-1, -1]):
                ni = i + dx
                nj = j + dy
                if ni < 0 or ni >= h or nj < 0 or nj >= w:
                    continue
                if str_list[ni][nj] == '
                    count += 1
            str_list[i][j] = count

    for i in range(h):
        for j in range(w):
            print(str_list[i][j], end='')
        print('')
    
if __name__ == '__main__':
    main()

Solution (Python):
H,W = map(int,input().split())
S = []
S.append('.'*(2+W))
for i in range(H):
    S.append('.'+input()+'.')
S.append('.'*(2+W))
Sans = []
for i in range(1,H+1):
    S1 = ''
    for j in range(1,W+1):
        ans = 0
        if S[i][j] == '.':
            if S[i-1][j-1] == '
                ans +=1
            if S[i-1][j] == '
                ans +=1
            if S[i-1][j+1] == '
                ans +=1
            if S[i][j-1] == '
                ans +=1
            if S[i][j+1] == '
                ans +=1
            if S[i+1][j-1] == '
                ans +=1
            if S[i+1][j] == '
                ans +=1
            if S[i+1][j+1] == '
                ans +=1
        if S[i][j] == '
            ans = '
        S1 += str(ans)
    Sans.append(S1)

for i in range(H):
    print(Sans[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):
        if s[i][j] == ".":
            bomb = 0
            dx = [0]
            dy = [0]
            if i != 0:
                dx.append(-1)
            if i != h - 1:
                dx.append(1)
            if j != 0:
                dy.append(-1)
            if j != w - 1:
                dy.append(1)

            for di in dx:
                for dj in dy:
                    if s[i + di][j + dj] == "
                        bomb += 1

            s[i][j] = str(bomb)

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

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

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

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

for i in range(1, H+1):
    print(S[i][1:W+1])

Solution (Python):










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

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

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 ( x >= 0 and x < h and y >=0 and y < w):
                    if (s[x][y] == "
                        cnt+=1
            s[i][j] = str(cnt)


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 = []
s.append(["."] * (w + 2))
for i in range(h):
  s.append(list("."+input()+"."))
s.append(["."] * (w + 2))

for i in range(1, h + 1):
  for j in range(1, w + 1):
    if s[i][j] != "
      tmp = ""
      tmp += s[i-1][j-1]+s[i-1][j]+s[i-1][j+1]
      tmp += s[i][j-1]+s[i][j+1]
      tmp += s[i+1][j-1]+s[i+1][j]+s[i+1][j+1]
      s[i][j] = str(tmp.count("

for i in range(1, h + 1):
  for j in range(1, w + 1):
    print(s[i][j], end="")
  print()

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

ans = []
for i in range(1, h + 1):
  tmp = []
  for j in range(1, w + 1):
    if s[i][j] == "1":
      tmp.append("
    else:
      tmp2 = ""
      tmp2 += s[i-1][j-1]
      tmp2 += s[i-1][j]
      tmp2 += s[i-1][j+1]
      tmp2 += s[i][j-1]
      tmp2 += s[i][j+1]
      tmp2 += s[i+1][j-1]
      tmp2 += s[i+1][j]
      tmp2 += s[i+1][j+1]
      tmp.append(tmp2.count("1"))
  ans.append(tmp)

for i in range(h):
  for j in range(w):
    print(ans[i][j], end="")
  print()

Solution (Python):
H,W=map(int,input().split())
s0='.'*(W+2)
s1='.'+input()+'.'

ans=''
for i in range(H):
    if i==H-1:
        s2='.'*(W+2)
    else:
        s2='.'+input()+'.'
    
    for j in range(1,W+1):
        if s1[j]=='.':
            x=s0[j-1:j+2]+s1[j-1]+s1[j+1]+s2[j-1:j+2]
            ans+=str(x.count('
        else:
            ans+='
    s0=s1
    s1=s2

for i in range(H):
    print(ans[i*W:i*W+W])

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]+'.')
    
def calc(row,col):
  out=0
  for i in range(3):
    for j in range(3):
      if padded[row-1+i][col-1+j]=='
  return out

ans=[[] for _ in range(H)]
for i in range(1,H+1):
  for j in range(1,W+1):
    if padded[i][j]=='
      ans[i-1].append('
      continue
    tmp = calc(i,j)
    ans[i-1].append(str(tmp))
  ans[i-1] = ''.join(ans[i-1])

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

Solution (Python):
def f(L, i, j):
  h = len(L)
  w = len(L[0])
  if L[i][j] != ".":
    return L[i][j]
  else:
    around = [L[i-1][j-1], L[i-1][j], L[i-1][j+1], L[i][j-1], L[i][j+1], L[i+1][j-1], L[i+1][j], L[i+1][j+1]]
    return str(around.count("

H, W = list(map(lambda x: int(x), input().split(" ")))
A = []
A.append(["X"] * (W + 2))
for i in range(H):
  A.append(["X"])
  A[i + 1].extend(list(input()))
  A[i + 1].extend(["X"])
A.append(["X"] * (W + 2))

for i in range(len(A)):
  for j in range(len(A[0])):
    A[i][j] = f(A, i, j)

print("\n".join(["".join(a[1:-1]) for a in A[1:-1]]))

Solution (Python):
H,W = map(int, input().split())
T = []
T.append(["."]*(W+2))
for _ in range(H):
  s = "."+input()+"."
  T.append(list(s))
T.append(["."]*(W+2))
for h in range(1, H+1):
  for w in range(1, W+1):
    if T[h][w]==".":
      S = [T[a][b] for b in range(w-1, w+2) \
           for a in range(h-1, h+2)]
      T[h][w]=S.count("
for h in range(1, H+1):
  print(*T[h][1:W+1], sep="")

Solution (Python):
import sys
sys.setrecursionlimit(10**9)

def mi(): return map(int,input().split())
def ii(): return int(input())
def isp(): return input().split()
def deb(text): print("-------\n{}\n-------".format(text))

INF=10**20
def main():
    H,W=mi()
    G = [[] for _ in range(H)]
    for i in range(H):
        S = input()
        G[i] = list(S)
    
    
    for h in range(H):
        for w in range(W):
            if G[h][w] == "
            res = 0
            for dh in [-1,0,1]:
                for dw in [-1,0,1]:
                    if dh == dw == 0: continue
                    if 0 <= h+dh < H and 0<= w+dw < W:
                        if G[h+dh][w+dw] == "
                            res += 1
            
            G[h][w] = str(res)

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



if __name__ == "__main__":
    main()

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

def count_mine(i, j, s, h, w):
  mine = 0
  
  if i-1 >= 0 and j-1 >= 0 and s[i-1][j-1] == "
    mine += 1
  if i-1 >= 0 and s[i-1][j] == "
    mine += 1
  if i-1 >= 0 and j+1 < w and s[i-1][j+1] == "
    mine += 1
  
  
  if j-1 >= 0 and s[i][j-1] == "
    mine += 1
  if j+1 < w and s[i][j+1] == "
    mine += 1
    
  
  if i+1 < h and j-1 >= 0 and s[i+1][j-1] == "
    mine += 1
  if i+1 < h and s[i+1][j] == "
    mine += 1
  if i+1 < h and j+1 < w and s[i+1][j+1] == "
    mine += 1
    
  return mine

for i in range(h):
  for j in range(w):
    if s[i][j] == ".":
      s[i][j] = count_mine(i, j, s, h, w)
  print(''.join(map(str, s[i])))

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

maps = [[0 for _ in range(W+2)]]

for _ in range(H):
    line = [dot for dot in ("."+input()+".")]
    line_ = [1 if point == "
    maps.append(line_)
maps.append([0 for _ in range(W+2)])

    
def calc(x, y):
    return maps[y-1][x] + maps[y+1][x] + maps[y][x+1] + maps[y][x-1] \
            + maps[y+1][x+1] + maps[y-1][x-1] + maps[y+1][x-1] + maps[y-1][x+1]

for y in range(1,H+1):
    for x in range(1,W+1):
        if maps[y][x] != 1:
            print(calc(x,y), end="")
        else:
            print("
    print()

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

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

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

for i in range(1, H+1):
    print(S[i][1:W+1])

Solution (Python):
def resolve():
    H, W = map(int, input().split())
    S = []
    for _ in range(H):
        s = input()
        S.append(s)
    for i in range(H):
        ans = ""
        for j in range(W):
            count = 0
            if S[i][j] == "
                ans += "
            else:
                if i >= 1 and j >= 1:
                    if S[i-1][j-1] == "
                        count += 1
                if i >= 1:
                    if S[i-1][j] == "
                        count += 1
                if i >= 1 and j <= W-2:
                    if S[i-1][j+1] == "
                        count += 1
                if j >= 1:
                    if S[i][j-1] == "
                        count += 1
                if j <= W-2:
                    if S[i][j+1] == "
                        count += 1
                if j >= 1 and i <= H-2:
                    if S[i+1][j-1] == "
                        count += 1
                if i <= H-2:
                    if S[i+1][j] == "
                        count += 1
                if i <= H-2 and j <= W-2:
                    if S[i+1][j+1] == "
                        count += 1
                ans += str(count)
        print(ans)
resolve()

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

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

for i in range(h):
    for j in range(w):
        if s[i][j] == '
            continue
        cnt = 0
        for t in range(8):
            ni=i+dx[t]
            nj=j+dy[t]
            if ni<0 or h<=ni: continue
            if nj<0 or w<=nj: continue
            if s[ni][nj] == '
                cnt += 1
        
        s[i][j] = str(cnt)

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

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

maze = [['.']*(W+2)]
for i in range(H):
    maze.append(list('.'+input()+'.'))
maze.append(['.']*(W+2))

ans = [[0]*W for _ in range(H)]
for i in range(1,H+1):
    for j in range(1,W+1):
        if maze[i][j] == '
            ans[i-1][j-1] = '
        else:
            for p,q in [[1,0],[-1,0],[0,1],[0,-1],[1,1],[-1,-1],[1,-1],[-1,1]]:
                if maze[i+p][j+q] == '
                    ans[i-1][j-1] += 1

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

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

for i in range(1, h+1):
  for j in range(1, w+1):
    if s[i][j] == "
      continue
    
    neighbors = [
      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],
    ]
    s[i][j] = str(len(list(filter(lambda x : x == "

for l in s[1:len(s)-1]:
  print(''.join(l[1:len(l)-1]))

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

nums = [[] for _ in range(h)]
for i in range(h):
  for j in range(w):
    if s[i][j] == "
    else:
      t = 0
      for a in range(i-1, i+2):
        if a < 0 or h <= a: continue
        for b in range(j-1, j+2):
          if b < 0 or w <= b: continue
          if s[a][b] == "
      nums[i].append(t)

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

Solution (Python):
h,w=map(int,input().split())
s=[]
for i in range(h):
    s.append(list(input()))
a=["+" for i in range(w)]
s.append(a)
s.insert(0,a)
for i in range(h+2):
    s[i].append("+")
    s[i].insert(0,"+")
for i in range(1,h+1):
    for j in range(1,w+1):
        if s[i][j]==".":
            m=0
            for x in range(3):
                for y in range(3):
                    if s[i-1+x][j-1+y]=="
                        m+=1
            s[i][j]=str(m)
for i in range(1,h+1):
    ans=s[i][1]
    for j in range(w-1):
        ans=ans+s[i][j+2]
    print(ans)

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

neigh = [-1, 0, 1]

for i in range(h):
    for j in range(w):
        cnt = 0
        flag = False
        if s[i][j] == '.':
            for k in neigh:
                for l in neigh:
                    if k==l==0 or i+k < 0 or i+k >= h or j+l < 0 or j+l >= w:
                        continue
                    elif s[i+k][j+l] == '
                        cnt += 1
            s[i] = s[i][:j] + str(cnt) +s[i][j+1:]
    print(s[i])

Solution (Python):
H, W=map(int, input().split())
c=['.'*(W+2)]+['.'+input()+'.' for _ in range(H)]+['.'*(W+2)]
ans=[['0']*(W+2) for _ in range(H+2)]
for h in range(1, H+1):
  for w in range(1, W+1):
    if c[h][w]=='
      ans[h][w]='
    else:
      x=0
      if c[h-1][w-1]=='
        x+=1
      if c[h-1][w]=='
        x+=1
      if c[h-1][w+1]=='
        x+=1
      if c[h][w-1]=='
        x+=1
      if c[h][w+1]=='
        x+=1
      if c[h+1][w-1]=='
        x+=1
      if c[h+1][w]=='
        x+=1
      if c[h+1][w+1]=='
        x+=1
      ans[h][w]=str(x)
for i in range(1, H+1):
  print(''.join(ans[i][1:-1]))