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

h,w = map(int,(input().split()))
s = [list(map(str,input())) for i in range(h)]
l = [[0]*(w+2) for i in range(h+2)]


def addFlag(i,j):
    for x in range(3):
        for y in range(3):
            if l[i+x][j+y] != '
                l[i+x][j+y] += 1
                
for i in range(h):
    for j in range(w):
        if s[i][j] == '
            l[i+1][j+1] = '
            addFlag(i,j)

l = np.delete(l,[0,h+1],0)
l = np.delete(l,[0,w+1],1)


for i in l:
    ans = ''.join(map(str,i))
    print(ans)

Solution (Python):
h,w = map(int,input().split())
s = [list(input()) for _ in range(h)]
bomb_count = 0
for i in range(h):
  for j in range(w):
    if s[i][j] == "
      continue
    else:
      for k in range(3):
        for l in range(3):
          a = i-1+k
          b = j-1+l
          if a < 0:
            continue
          elif a > h-1:
            continue
          if b < 0:
            continue
          elif b > w-1:
            continue
          if s[a][b] == "
            bomb_count += 1
      s[i][j] = str(bomb_count)
      bomb_count = 0
for i in range(h):
  print("".join(s[i]))

Solution (Python):
h, w = map(int, input().split())
field = [list(input()) for _ in range(h)]
dx = [1, 0, -1, 0, 1, -1, -1, 1]
dy = [0, 1, 0, -1, 1, 1, -1, -1]
for i in range(h):
    for j in range(w):
        if field[i][j] == "
            continue
        num = 0
        for k in range(8):
            ni = i + dy[k]
            nj = j + dx[k]
            if ni < 0 or h <= ni:
                continue
            if nj < 0 or w <= nj:
                continue
            if field[ni][nj] == "
                num += 1
        field[i][j] = str(num)
for out in field:
    print("".join(out))

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


for i in range(1,h+1):
    s = ''
    for j in range(1,w+1):
        directions = [(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1),(-1,0),(-1,1)]
        alist = [(i+dir[0],j+dir[1]) for dir in directions]
        cnt = 0
        for a in alist:
            if l[a[0]][a[1]] == '
                cnt += 1
        if l[i][j] == '
            s += '
        else:
            s += str(cnt)
    print(s)

Solution (Python):
from typing import List


def search_bomb(i: int, j: int, ss: List[str]) -> int:
    count = 0
    for k in range(i - 1, i + 2):
        for l in range(j - 1, j + 2):
            if 0 <= k <= h - 1 and 0 <= l <= w - 1:
                if ss[k][l] == '
                    count += 1
    return count


def answer(ss: List[str]) -> List[str]:
    result = list('' for _ in range(h))

    for i, s in enumerate(ss):
        line = ''
        for j, c in enumerate(s):
            if c == '.':
                line += str(search_bomb(i, j, ss))
            elif c == '
                line += '
        result[i] = line

    return result


def main():
    ss = [input() for _ in range(h)]
    for line in answer(ss):
        print(line)


if __name__ == '__main__':
    h, w = map(int, input().split())
    main()

Solution (Python):



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


def check(S, i, j):
    if S[i][j] == '
        return '

    num = 0
    for _i in range(max(0, i-1), min(H, i+2)):
        for _j in range(max(0, j-1), min(W, j+2)):
            if S[_i][_j] == '
                num += 1
    return str(num)


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

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

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

for i in range(H):
    for j in range(len(L[i])):
        if L[i][j] == '.':
            cnt = 0
            for k in range(-1, 2):
                for l in range(-1, 2):
                    if ((i+k) < H) and ((j+l) < len(L[i])) and ((j+l) >= 0) and ((i+k) >= 0):
                        if L[i+k][j+l] == '
                            cnt += 1
            L[i][j] = str(cnt)

for i in range(len(L)):
    print(''.join(L[i]))

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


import copy

dir = [(i, j) for i in range(-1, 2) for j in range(-1, 2)]
ans = copy.deepcopy(s)
for i in range(h):
    for j in range(w):
        if s[i][j] == '.':
            cnt = 0
            for y, x in dir:
                iy, jx = i+y, j+x
                if 0 <= iy < h and 0 <= jx < w:
                    if s[iy][jx] == '
                        cnt += 1
            ans[i][j] = cnt




for i in range(h):
    moji = map(str,ans[i])
    print("".join(moji))

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


for i in range(len(x)):
    x[i].insert(0, ".")
    x[i].append(".")

x.insert(0, ["."]*(W+2))
x.append(["."]*(W+2))


tmp = 0
for i in range(1, H+1):
    for j in range(1, W+1):
        if x[i][j] == "
            x[i][j] = "
            continue
        if x[i-1][j-1] == "
            tmp += 1
        if x[i-1][j] == "
            tmp += 1
        if x[i-1][j+1] == "
            tmp += 1
        if x[i][j-1] == "
            tmp += 1
        if x[i][j+1] == "
            tmp += 1
        if x[i+1][j-1] == "
            tmp += 1
        if x[i+1][j] == "
            tmp += 1
        if x[i+1][j+1] == "
            tmp += 1
        x[i][j] = str(tmp)
        tmp = 0

del x[0]
del x[-1]

for i in range(len(x)):
    del x[i][0]
    del x[i][-1]

for i in range(len(x)):
    print(''.join(x[i]))

Solution (Python):
def input_int():
    return(int(input()))

def input_int_list():
    return(list(map(int,input().split())))

def input_int_map():
    return(map(int,input().split()))

def run():
    h, w = input_int_map()
    rows = []

    for _ in range(h):
        rows.append(list(input()))

    for row in range(h):
        for col in range(w):
            replace_space(row, col, rows)

    for row in rows:
        print("".join(map(str, row)))

def replace_space(row: int, col:int, rows: []):

    if rows[row][col] != '.':
        return

    h = len(rows)
    w = len(rows[0])

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

    bomb_count = 0
    for y_diff, x_diff in nums:
        y = row + y_diff
        x = col + x_diff

        if y >= h or y < 0:
            continue

        if x >= w or x < 0:
            continue

        if rows[y][x] == '
            bomb_count += 1

    rows[row][col] = bomb_count

run()

Solution (Python):
import copy

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

dir = [(i, j) for i in range(-1, 2) for j in range(-1, 2)]
ans = copy.deepcopy(s)
for i in range(h):
    for j in range(w):
        if s[i][j] == '.':
            cnt = 0
            for y, x in dir:
                iy, jx = i+y, j+x
                if 0 <= iy < h and 0 <= jx < w:
                    if s[iy][jx] == '
                        cnt += 1
            ans[i][j] = cnt
for i in ans:
    print(''.join(map(str, i)))

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

for i in range(h):
    S = input()
    for j, s in enumerate(S):
        if s == "
            ans[i+1][j+1] = "
            if ans[i][j+1] != "
                ans[i][j+1] += 1
            if ans[i+1][j] != "
                ans[i+1][j] += 1
            if ans[i][j] != "
                ans[i][j] += 1
            if ans[i+1][j + 2] != "
                ans[i+1][j + 2] += 1
            if ans[i][j + 2] != "
                ans[i][j + 2] += 1
            if ans[i + 2][j+1] != "
                ans[i + 2][j+1] += 1
            if ans[i + 2][j + 2] != "
                ans[i + 2][j + 2] += 1
            if ans[i + 2][j] != "
                ans[i + 2][j] += 1

for a in ans[1:-1]:
    print("".join(map(str, a))[1:-1])

Solution (Python):
from itertools import product
H,W=list(map(int, input().split()))
M=[list(input()) for _ in range(H)]
S=set(product([-1,0,1],repeat=2))-{(0,0)}

for i in range(H):
  for j in range(W):
    if M[i][j]=='
      continue
    c=0
    for k in S:
      h=i+k[0]
      w=j+k[1]
      if 0<=h<H and 0<=w<W:
        if M[h][w]=='
          c+=1
    M[i][j]=str(c)
for i in range(H):
  print(''.join(M[i]))

Solution (Python):
import sys

input = sys.stdin.readline
H, W = map(int, input().split())
S = [(input()) for _ in range(H)]
T = [[0 for _ in range(W)] for _ in range(H)]

for h in range(H):
    for w in range(W):
        for i in range(h - 1, h + 2):
            for j in range(w - 1, w + 2):
                if i < 0 or j < 0 or i >= H or j >= W:
                    continue
                else:
                    if S[h][w] == '
                        T[h][w] = -1
                    elif S[i][j] == '
                        T[h][w] += 1

for t in T:
    print((''.join(map(str, t))).replace('-1', '

Solution (Python):
def resolve():
    h,w=map(int,input().split())
    s=[]
    for _ in range(h):
        s.append(list(input()))
    dx=[-1,-1,-1,0,0,1,1,1]
    dy=[-1,0,1,-1,1,-1,0,1]
    for x in range(h):
        for y in range(w):
            if s[x][y]=='
                for k in range(8):
                    nx=x+dx[k]
                    ny=y+dy[k]
                    if not(nx<h and 0<=nx and ny<w and 0<=ny) or s[nx][ny]=='
                        continue
                    if s[nx][ny]=='.':
                        s[nx][ny]='1'
                    else:
                        s[nx][ny]=str(ord(s[nx][ny])-ord('0')+1)
    for i in range(h):
        for j in range(w):
            if s[i][j]=='.':
                s[i][j]='0'
    for i in range(h):
        print(''.join(s[i]))
resolve()

Solution (Python):
import sys
def input(): return sys.stdin.readline().strip()
def mapint(): return map(int, input().split())
sys.setrecursionlimit(10**9)

H, W = mapint()
S = [list(input()) for _ in range(H)]
ans = [['
for i in range(H):
    for j in range(W):
        if S[i][j] == '
            continue
        cnt = 0
        for h in range(-1, 2):
            for w in range(-1, 2):
                nh = i + h
                nw = j + w
                if nh<0 or nh>=H or nw<0 or nw>=W:
                    continue
                if S[nh][nw]=='
                    cnt += 1
        ans[i][j] = str(cnt)

for a in ans:
    print(''.join(a))

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

sl = [list(input()) for _ in range(h)]
ans = [[0]*(w+2) for _ in range(h+2)]


for i in range(h):
    for j in range(w):
        row = i+1
        col = j+1
        
        if sl[i][j] == '
            ans[row][col] = '
            if ans[row-1][col] != '
            if ans[row+1][col] != '
            if ans[row][col-1] != '
            if ans[row][col+1] != '
            if ans[row-1][col-1] != '
            if ans[row-1][col+1] != '
            if ans[row+1][col-1] != '
            if ans[row+1][col+1] != '



for i in range(h+2):
    if i != 0 and i != h+1:
        print(*ans[i][1:-1], sep='')

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

for i in range(h):
    S = input()
    for j, s in enumerate(S):
        if s == "
            ans[i][j] = "
            if i != 0:
                if ans[i - 1][j] != "
                    ans[i - 1][j] += 1
            if j != 0:
                if ans[i][j - 1] != "
                    ans[i][j - 1] += 1
            if i != 0 and j != 0:
                if ans[i - 1][j - 1] != "
                    ans[i - 1][j - 1] += 1
            if j != w - 1:
                if ans[i][j + 1] != "
                    ans[i][j + 1] += 1
            if j != w - 1 and i != 0:
                if ans[i - 1][j + 1] != "
                    ans[i - 1][j + 1] += 1
            if i != h - 1:
                if ans[i + 1][j] != "
                    ans[i + 1][j] += 1
            if i != h - 1 and j != w - 1:
                if ans[i + 1][j + 1] != "
                    ans[i + 1][j + 1] += 1
            if i != h - 1 and j != 0:
                if ans[i + 1][j - 1] != "
                    ans[i + 1][j - 1] += 1

for a in ans:
    print("".join(map(str, a)))

Solution (Python):
import copy

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

a = []
for i in range(-1, 2):
    for j in range(-1, 2):
        a.append((i, j))

ans = [[0]*w for _ in range(h)]
for i in range(h):
    for j in range(w):
        if s[i][j] == '
            ans[i][j] = '

for i in range(h):
    for j in range(w):
        if s[i][j] == '.':
            cnt = 0
            for y, x in a:
                iy, jx = i+y, j+x
                if 0 <= iy < h and 0 <= jx < w:
                    if s[iy][jx] == '
                        cnt += 1
            ans[i][j] = cnt
for i in ans:
    print(''.join(map(str, i)))

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

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

def s(y, x):
  if x >= W or x < 0 or y >= H or y < 0:
    return 0

  return 1 if SS[y][x] == '

for i in range(H):
  S = SS[i]
  l = []
  for j in range(W):
    c = S[j]
    if c == '.':
      l.append(str(sum([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)])))
    else:
      l.append('
  print(''.join(l))

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

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

def getBombNum(x, y):
  total = 0
  for i in range(-1, 2):
    for j in range(-1, 2):
      if i == 0 and j == 0:
        if getBomb(x, y) == 1:
          return -1
      else:
        total += getBomb(x + i, y + j)
  return total

table = [0] * (W * H)
for y in range(H):
  for x in range(W):
    num = getBombNum(x, y)
    if num < 0:
      print("
    else:
      print(num, end="")
  print()

Solution (Python):
from collections import Counter
import math
import statistics
import itertools
a,b=map(int,input().split())






g = [list(map(lambda x: '{}'.format(x), list(input()))) for _ in range(a)]





for i in range(a):
    for k in range(b):
        count=0
        if g[i][k]==".":
            if k-1>=0:
                if g[i][k-1]=="
                    count+=1
            if i-1>=0:
                if g[i-1][k]=="
                    count+=1
            if i-1>=0 and k-1>=0:
                if g[i-1][k-1]=="
                    count+=1
            if k+1<=b-1:
                if g[i][k+1]=="
                    count+=1
            if i+1<=a-1:
                if g[i+1][k]=="
                    count+=1
            if i+1<=a-1 and k+1<=b-1:
                if g[i+1][k+1]=="
                    count+=1
            if i-1>=0 and k+1<=b-1:
                if g[i-1][k+1]=="
                    count+=1
            if i+1<=a-1 and k-1>=0:
                if g[i+1][k-1]=="
                    count+=1
            g[i][k]=str(count)


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

Solution (Python):
H, W = map(int, input().split())
L = ['.' + input() + '.' for _ in range(H)]
L = ['.' * (W + 2)] + L + ['.' * (W + 2)]
A = []
for i in range(1, H + 1):
  S = ""
  for j in range(1, W + 1):
    if L[i][j] == '.':
      t = [L[i-1][j-1], L[i-1][j], L[i-1][j+1], L[i][j+1], L[i+1][j+1], L[i+1][j], L[i+1][j-1], L[i][j-1]]
      S += str(t.count('
    else:
      S += '
  A.append(S)
[print(i) for i in A]

Solution (Python):
h,w=map(int,input().split())
l = [input() for _ in range(h)]
k = [['
dx = [0,0,1,-1,1,1,-1,-1]
dy = [1,-1,0,0,1,-1,1,-1]
for i in range(h):
    for j in range(w):
        if l[i][j] == '.':
            cnt = 0
            for di in range(8):
                x = j+dx[di]
                y = i+dy[di]
                if x<0 or y<0 or x >=w or y >=h:
                    continue
                if l[y][x] == '
                    cnt +=1
            k[i][j] = cnt
for i in range(h):
    print(''.join(map(str, k[i])))

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

for i in range(h):
  m.append(input())

ans=[["
for i in range(h):
  for j in range(w):
    cnt = 0
    if h > i-1 >= 0:
      if m[i-1][j] == "
        cnt += 1
      if w > j-1 >=0 and m[i-1][j-1] == "
        cnt += 1
      if w > j+1 >=0 and m[i-1][j+1] == "
        cnt += 1
    if h > i+1 >= 0:
      if m[i+1][j] == "
        cnt += 1
      if w > j-1 >=0 and m[i+1][j-1] == "
        cnt += 1
      if w > j+1 >=0 and m[i+1][j+1] == "
        cnt += 1
      
    if w > j-1 >=0 and m[i][j-1] == "
      cnt += 1
    if w > j+1 >=0 and m[i][j+1] == "
      cnt += 1
    if m[i][j] != "
      ans[i][j] = str(cnt)

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

Solution (Python):
h,w=map(int,input().split())
a=[list(input()) for i in range(h)]
b=a
for i in range(h):
  for j in range(w):
    if a[i][j]!="
      b[i][j]=a[i][max(0,j-1):min(w,j+2)].count("
      if i!=0:
        b[i][j]+=a[i-1][max(0,j-1):min(w,j+2)].count("
      if i!=h-1:
        b[i][j]+=a[i+1][max(0,j-1):min(w,j+2)].count("
for i in range(h):
  print("".join(str(j) for j in b[i]))

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

for i in range(h):
    s = input()
    sl.append(s)

def scheck(s):
    if s == "
        return 1
    else:
        return 0
        
def check(i,j,sl):
    cnt = 0
    for k in range(-1,2):
        for l in range(-1,2):
            if h > i+k >= 0 and w > j+l >= 0:
                cnt += scheck(sl[i+k][j+l])
    return str(cnt)

for i in range(h):
    for j in range(w):
        if sl[i][j] == ".":
            bl[i][j] = check(i,j,sl)
        else:
            bl[i][j] = "

for b in bl:
    t = ''.join(b)
    print(t)

Solution (Python):
lst = input().split()
H = int(lst[0])
W = int(lst[1])

field = []
for i in range(H):
   l = []
   s = input()
   for j in range(W):
      l.append(s[j])
   field.append(l)

def count(x, y):
   result = 0
   for P 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]]:
      try:
         if (0 <= P[0]) and (0 <= P[1]) and (field[P[1]][P[0]] == '
            result += 1
      except:
         pass
   return result

for i in range(H):
   for j in range(W):
      if field[i][j] == '.':
         field[i][j] = str(count(j, i))

def list2str(L):
   result = ''
   for t in L:
      result += t
   return result

for I in field:
   print(list2str(I))

Solution (Python):
import sys
H, W = map(int, input().split())
if H == 1:
    if W == 1:
        s = input()
        if s == "
            print("
        else:
            print(0)
    else:
        cnt = [0 for _ in range(W)]
        s = list(input())
        for w in range(W):
            if w == 0:
                cnt[w] += s[w+1] == "
            elif w == W-1:
                cnt[w] += s[w-1] == "
            else:
                cnt[w] += s[w+1] == "
                cnt[w] += s[w-1] == "
        for w in range(W):
            if s[w] == "
                cnt[w] = "
        ans = map(str, cnt)
        print("".join(ans))
    sys.exit(0)
if W == 1:
    cnt = [0 for _ in range(H)]
    s = []
    for h in range(H):
        s.append(input())
    for h in range(H):
        if h == 0:
            cnt[h] += s[h+1] == "
        elif h == H-1:
            cnt[h] += s[h-1] == "
        else:
            cnt[h] += s[h-1] == "
            cnt[h] += s[h+1] == "
    for h in range(H):
        if s[h] == "
            print("
        else:
            print(cnt[h])
    sys.exit(0)
    
S = []
cnt = [[0 for _ in range(W)] for _ in range(H)]
for h in range(H):
    s = list(input())
    for w in range(W):
        if w == 0:
            cnt[h][w] += s[w+1] == "
        elif w == W-1:
            cnt[h][w] += s[w-1] == "
        else:
            cnt[h][w] += s[w+1] == "
            cnt[h][w] += s[w-1] == "
    S.append(s)
    
for h in range(H):
    for w in range(W):
        if h == 0:
            cnt[h][w] += S[h+1][w] == "
            if w == 0:
                cnt[h][w] += S[h+1][w+1] == "
            elif w == W-1:
                cnt[h][w] += S[h+1][w-1] == "
            else:
                cnt[h][w] += S[h+1][w-1] == "
                cnt[h][w] += S[h+1][w+1] == "
        elif h == H-1:
            cnt[h][w] += S[h-1][w] == "
            if w == 0:
                cnt[h][w] += S[h-1][w+1] == "
            elif w == W-1:
                cnt[h][w] += S[h-1][w-1] == "
            else:
                cnt[h][w] += S[h-1][w-1] == "
                cnt[h][w] += S[h-1][w+1] == "
        else:
            cnt[h][w] += S[h+1][w] == "
            if w == 0:
                cnt[h][w] += S[h+1][w+1] == "
            elif w == W-1:
                cnt[h][w] += S[h+1][w-1] == "
            else:
                cnt[h][w] += S[h+1][w-1] == "
                cnt[h][w] += S[h+1][w+1] == "
            cnt[h][w] += S[h-1][w] == "
            if w == 0:
                cnt[h][w] += S[h-1][w+1] == "
            elif w == W-1:
                cnt[h][w] += S[h-1][w-1] == "
            else:
                cnt[h][w] += S[h-1][w-1] == "
                cnt[h][w] += S[h-1][w+1] == "
                
for h in range(H):
    s = S[h]
    for w in range(W):
        if s[w] == "
            cnt[h][w] = "
    ans = map(str, cnt[h])
    print("".join(ans))

Solution (Python):


def get_count(S, H, W, i, j):
    num = 0
    start_i = start_j = -1
    end_i = end_j = 2
    if W == 1:
        start_i = 0 
        end_i = 1
    else:
        if i == 0:
            start_i = 0
        elif i == W-1:
            end_i = 1
    if H == 1:
        start_j = 0
        end_j = 1
    else:
        if j == 0:
            start_j = 0
        elif j == H-1:
            end_j = 1

    for y in range(start_j, end_j, 1):
        for x in range(start_i, end_i, 1):
            if S[j+y][i+x] == "
                num += 1

    return num


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

for y in range(H):
    for x in range(W):
        if S[y][x] == ".":
            num = get_count(S, H, W, x, y)
            S[y][x] = str(num)

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

Solution (Python):
h,w=map(int,input().split())
S=[list(input()) for i in range(h)]
ans= [["" for i in range(w)] for j in range(h)]
for i in range(h):
  for j in range(w):
    if S[i][j]=="
      ans[i][j]="
    else:
      cnt = 0
      for k in range(-1,2):
        for l in range(-1,2):
          if 0<=i+k<h and 0<=j+l<w and S[i+k][j+l]=="
            cnt += 1
      ans[i][j]=str(cnt)
for i in range(h):
  for j in range(w):
    print(ans[i][j],end="")
  print("")

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

for i in range(h):
    t.append([])
    for j in range(w):
        mines = 0
        if s[i][j] == '.':
            if i > 0 and j > 0:
                if s[i - 1][j - 1] == '
                    mines += 1
            if i < h - 1 and j > 0:
                if s[i + 1][j - 1] == '
                    mines += 1
            if i > 0 and j < w - 1:
                if s[i - 1][j + 1] == '
                    mines += 1
            if i < h - 1 and j < w - 1:
                if s[i + 1][j + 1] == '
                    mines += 1
            if i > 0:
                if s[i - 1][j] == '
                    mines += 1
            if i < h - 1:
                if s[i + 1][j] == '
                    mines += 1
            if j > 0:
                if s[i][j - 1] == '
                    mines += 1
            if j < w - 1:
                if s[i][j + 1] == '
                    mines += 1
            t[i].append(str(mines))
        else:
            t[i].append('

for i in range(len(t)):
    print(''.join(t[i]))

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

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

    for i in range(H):
        for k in range(W):

            if S[i][k]!="

                c = 0; 

                if i>0 and k>0 and S[i-1][k-1]=="
                    c += 1

                if i>0  and S[i-1][k]=="
                    c += 1

                if i>0 and k<W-1 and S[i-1][k+1]=="
                    c+= 1

                if k>0 and S[i][k-1]=="
                    c += 1
           
                if k<W-1 and S[i][k+1]=="
                    c += 1

                if i<H-1 and k>0 and S[i+1][k-1]=="
                    c += 1

                if i<H-1 and S[i+1][k]=="
                    c += 1

                if i<H-1 and k<W-1 and S[i+1][k+1]=="
                    c += 1

                S[i][k] = str(c)

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


if __name__ == "__main__":
    solve()

Solution (Python):
import sys

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

for i in range(H):
  for j in range(W):
    c=0
   
    if l[i][j]=="
      pass
   
    else:
      
      if 0<i and 0<j and l[i-1][j-1]=='
        c+=1
      
      if 0<i and l[i-1][j]=='
        c+=1
      
      if 0<i and j<W-1 and l[i-1][j+1]=='
        c+=1
      
      if 0<j and l[i][j-1]=='
        c+=1
      
      if j<W-1 and l[i][j+1]=='
        c+=1
      
      if i<H-1 and 0<j and l[i+1][j-1]=='
        c+=1
      
      if i<H-1 and l[i+1][j]=='
        c+=1
      
      if i<H-1 and j<W-1 and l[i+1][j+1]=='
        c+=1
        
              
      l[i][j]=str(c)


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

Solution (Python):
X,Y = map(int,input().split())
F = list(list(input()) for _ in range(X))
DX = [-1,-1,-1,0,0,1,1,1]
DY = [-1,0,1,-1,1,-1,0,1]

for i in range(X):
  for j in range(Y):
    if F[i][j] == '.':
      cnt = 0
      for k in range(8):
        x = i + DX[k]
        y = j + DY[k]
        if 0 <= x < X and 0 <= y < Y and F[x][y] == '
          cnt += 1
      F[i][j] = str(cnt)
      
for f in F:
  print(''.join(f))

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

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

for i in range(H):
    for j in range(W):
        if S[i][j] == '
            continue
        c = 0
        for ii in range(i-1,i+2):
            if ii < 0 or ii >= H:
                continue
            for jj in range(j-1,j+2):
                if jj < 0 or jj >= W:
                    continue
                if S[ii][jj] == '
                    c += 1
        S[i][j] = str(c)

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

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

for h in range(H):
    new_s = ''
    for w in range(W):
        if S[h][w] == '
            new_s += '
            continue
        count = 0
        ary = [-1, 0, 1]
        for i in ary:
            for j in ary:
                if h+i < 0 or h+i >= H or w+j < 0 or w+j >=W: continue
                if S[h+i][w+j] == '
                    count += 1
        new_s += str(count)
    
    S[h] = new_s

for s in S:
    print(s)

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] == ".":
            c = 0
            for x in range(-1, 2):
                for y in range(-1, 2):
                    if x == 0 and y == 0:
                        continue
                    else:
                        if 0 <= i+y and 0 <= j+x and i+y < h and j+x < w:
                            if s[i+y][j+x] == "
                                c += 1
            s[i][j] = str(c)
for a in range(h):
    print("".join(s[a]))

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

for i in range(h):
  for j in range(w):
    if s[i][j] == "
      continue
    
    tmp = 0
    for k in range(8):
      ni = i + dx[k]
      nj = j + dy[k]
      
      if ni < 0 or ni >= h:
        continue
      if nj < 0 or nj >= w:
        continue
      if s[ni][nj] == "
        tmp += 1
    s[i][j] = str(tmp)

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

Solution (Python):
import typing
from typing import List, Counter
from itertools import product
class Grid:
    def __init__(self, field: List[str]):
        self.setField(field)
    
    def setField(self, field) -> None:
        self.column: int = len(field)
        self.row: int = len(field[0])
        self.field: List[List[str]] = [list(line) for line in field]

    def searchBomb(self) -> None:
        for i,j in product(range(self.column), range(self.row)):
            conn = tuple((i-1+k//3, j-1+k%3) for k in range(9))
            conn = tuple((x,y) for x,y in conn if 0<=x<self.column and 0<=y<self.row)
            if self.field[i][j] == '.':
                self.field[i][j] = str(Counter(self.field[x][y] for x,y in conn)['
    
    def print(self) -> None:
        for line in self.field:
            print(''.join(line))

def main():
    with open(0) as f:
        H, W = map(int, f.readline().split())
        S = [line.strip() for line in f.readlines()]
    grid = Grid(S)
    grid.searchBomb()
    grid.print()

if __name__ == '__main__':
    main()