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())
m = [input() for _ in range(h)]
vx = [0,1,1,1,0,-1,-1,-1]
vy = [1,1,0,-1,-1,-1,0,1]
ans = [['
for i in range(h):
    for j in range(w):
        if m[i][j] == '
            ans[i][j] = '
            continue
        count = 0
        for k in range(8):
            ny = vy[k] + i
            nx = vx[k] + j
            if 0<=ny and ny<h and 0<=nx and nx<w:
                if m[ny][nx] == '
                    count += 1
        ans[i][j] = str(count)
for i in ans:
    print(''.join(i))

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

di=[1,1,0,-1,-1,-1,0,1]
dj=[0,1,1,1,0,-1,-1,-1]

for i in range(H):
    for j in range(W):
        ans=0
        if S[i][j]=='
            continue
        for k in range(8):
            ni=i+di[k]
            nj=j+dj[k]
            if ni<0 or nj<0 or ni>=H or nj>=W:
                continue
            if S[ni][nj]=='
                ans+=1
        S[i][j]=str(ans)
for i in range(H):
    for j in range(W):
        print(S[i][j],end='')
    print()

Solution (Python):
import sys



def input():
    return sys.stdin.readline().strip()


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


def input_int_list():
    return [int(i) for i in input().split()]


def main():
    h, w = input_int_list()
    grid = []
    grid.append(["."] * (w + 2))
    for _ in range(h):
        grid.append(["."] + list(input()) + ["."])
    grid.append(["."] * (w + 2))

    directions = ((1, 1), (0, 1), (1, 0), (-1, 0), (0, -1), (-1, -1), (-1, 1), (1, -1))

    for i in range(1, h + 1):
        for j in range(1, w + 1):
            cnt = 0
            
            if grid[i][j] == ".":
                for x, y in directions:
                    if grid[i + x][j + y] == "
                        cnt += 1
                grid[i][j] = cnt

    for i in range(1, h + 1):
        print("".join(str(i) for i in grid[i][1:-1]))
    return


if __name__ == "__main__":
    main()

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

ans = [[0 for _ in range(W)] for _ in range(H)]
move = [(-1,-1),(-1,0),(-1,1),(0,-1),(0,1),(1,-1),(1,0),(1,1)]

for i in range(H):
  for j in range(W):
    if S[i][j] == "
      ans[i][j] = "
    else:
      for x, y in move:
        nx, ny = i+x, j+y
        if nx < 0 or H <= nx or ny < 0 or W <= ny:
          continue
        elif S[nx][ny] == "
          ans[i][j] += 1
          
for a in ans:
  for i in range(W):
    a[i] = str(a[i])
  print("".join(a))

Solution (Python):
l = [int(c) for c in input().split()]
H = l[0]
W = l[1]
S = [input() for c in range(H)]

for i in range(H):
    for j in range(W):
        if S[i][j]==".":
            cnt = 0
            for k in range(3):
                for m in range(3):
                    if 0 <= i-1+k and i-1+k <= H-1 and 0 <= j-1+m and j-1+m <= W-1:
                        if S[i-1+k][j-1+m] == "
                            cnt+=1
            S[i]=S[i][:j]+str(cnt)+S[i][j+1:]
    print(S[i])

Solution (Python):
h,w = map(int,input().split())
m = [input() for _ in range(h)]
vx=[0,1,1,1,0,-1,-1,-1]
vy=[1,1,0,-1,-1,-1,0,1]
ans=[['
for i in range(h):
    for j in range(w):
        if m[i][j]=='
            ans[i][j]='
            continue
        count=0
        for k in range(8):
            ny=vy[k]+i
            nx=vx[k]+j
            if 0<=ny and ny<h and 0<=nx and nx<w:
                if m[ny][nx]=='
                    count+=1
        ans[i][j]=str(count)
for i in ans:
    print(''.join(i))

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

s = [['.']*(w+2)]+ [list('.'+ input() + '.') for i in range(h)] + [['.']*(w+2)]
a = [[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] == '
      a[i-1][j-1] = '
    else:
      b = [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]]
      c = b.count('
           
      a[i-1][j-1] = c

for i in range(h):
  a[i] = [str(x) for x in a[i]]
  print(''.join(a[i]))

Solution (Python):
h,w=map(int,input().split())
C=[list(input()) for i in range(h)]
import copy
D=copy.deepcopy(C)
for i in range(h):
    for j in range(w):
        cnt=0
        if C[i][j]=="
            D[i][j]="
        else:
            if i>0 and j>0 and  C[i-1][j-1]=="
                cnt+=1
            if i>0 and  C[i-1][j]=="
                cnt+=1
            if i>0 and j<w-1 and C[i-1][j+1]=="
                cnt+=1
            if j>0  and C[i][j-1]=="
                cnt+=1
            if j<w-1 and C[i][j+1]=="
                cnt+=1
            if i<h-1 and j>0 and  C[i+1][j-1]=="
                cnt+=1
            if i<h-1 and  C[i+1][j]=="
                cnt+=1
            if i<h-1 and j<w-1 and  C[i+1][j+1]=="
                cnt+=1
            D[i][j]=str(cnt)
for k in range(h):
    mojiretu = ''.join(D[k])
    print(mojiretu)

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

mas = [list(map(str,input())) for _ in range(H)]
mas2 = [[0 for _ in range(W+2)] for __ in range(H+2)]

for i in range(1,H+1):
  for j in range(1,W+1):
    if mas[i-1][j-1] == '
      mas2[i][j] = -9
      mas2[i+1][j] += 1
      mas2[i+1][j-1] += 1
      mas2[i][j-1] += 1
      mas2[i-1][j-1] += 1
      mas2[i-1][j] += 1
      mas2[i-1][j+1] += 1
      mas2[i][j+1] += 1
      mas2[i+1][j+1] += 1

mas3 = [l[1:-1] for l in mas2[1:-1]]

for k in range(H):
  print(*["

Solution (Python):
H, W = map(int, input().split())
A = []
for i in range(H):
  A.append(input())
dis = [[-1, -1], [-1, 0], [-1, 1], [0, -1], [0, 1], [1, -1], [1, 0], [1, 1]]
for i in range(H):
  s = ""
  for j in range(W):
    if A[i][j] == "
      s += "
    else:
      count = 0
      for d in dis:
        if 0 <= i + d[0] < H and 0 <= j + d[1] < W:
          if A[i+d[0]][j+d[1]] == "
            count += 1
      s += str(count)
  print(s)

Solution (Python):
def period_to_zero(value:str):
    if value == ".":
        return 0
    return value

H,W = map(int, input().split())
S = list()
S.append([0]*(W+2))
for i in range(H):
    s = list()
    s.append(0)
    s.extend(list(map(period_to_zero, list(input()))))
    s.append(0)
    S.append(s)
S.append([0]*(W+2))

for i in range(1, len(S)-1):
    s_prev = S[i-1]
    s_current = S[i]
    s_next = S[i+1]
    for j in range(1, len(s_current)-1):
        if s_current[j] != "
            s_current[j] = (s_prev[j-1:j+2] + s_next[j-1:j+2] + [s_current[j-1], s_current[j+1]]).count("
        
for i in range(1, len(S)-1):
    s = S[i]
    print(*s[1:len(s)-1],sep="")

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

di = [1, 0, -1, 0, 1, -1, -1, 1]
dj = [0, 1, 0, -1, 1, 1, -1, -1]

for i in range(h):
    for j in range(w):
        if s[i][j] == '
            continue
        cnt = 0
        for d in range(8):
            if i + di[d] <0 or i + di[d] > h - 1 or j + dj[d] < 0 or j + dj[d] > w - 1:
                continue
            if s[i + di[d]][j +dj[d]] == '
                cnt += 1
        ans[i][j] = cnt

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

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

for i in range(h):
    line = ""
    for j in range(w):
        hs = max(0, i - 1)
        he = min(h - 1, i + 1)
        ws = max(0, j - 1)
        we = min(w - 1, j + 1)
        counter = 0
        for hi in range(hs, he + 1):
            for wi in range(ws, we + 1):
                if table[hi][wi] == "
                    counter += 1
        if table[i][j] == "
            line += "
        else:
            line += str(counter)
    print(line)

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

p = [[0] * (w + 2) for i in range(h + 2)]

for i in range(h):
    for j in range(w):
        if s[i][j] == '
            p[i][j] += 1
            p[i][j+1] += 1
            p[i][j+2] += 1
            p[i+1][j] += 1
            p[i+1][j+2] += 1
            p[i+2][j] += 1
            p[i+2][j+1] += 1
            p[i+2][j+2] += 1

for m in range(1, h+1):
    for n in range(1, w+1):
        i = m - 1
        j = n - 1
        if s[i][j] == '
            p[m][n] ='

for m in range(1, h+1):
    print(''.join(map(str, p[m][1:w+1])))

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

ans  =[]
for row in s:
        ans.append(list(row))
        
dxy = [(-1, -1), (-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1)]
for x in range(h):
        for y in range(w):
                if s[x][y] == "
                        continue
                c = 0
                for dx, dy in dxy:
                        if x +dx < 0 or x +dx > h-1 or y + dy < 0 or y + dy > w-1:
                                continue
                        else:
                                if s[x + dx][y + dy] == "
                                        c += 1
                ans[x][y] = c
for row in ans:
        print("".join(list(map(str, row))))

Solution (Python):
h,w = map(int,input().split())
s = [list(input()) for i in range(h)]
for i in range(h):
    for j in range(w):
        if s[i][j] == '.':
            m = 0
            if i-1 >= 0 and j-1 >= 0:
                if s[i-1][j-1] == '
                    m += 1
            if i-1 >= 0:
                if s[i-1][j] == '
                    m += 1
            if i-1 >= 0 and j+1 < w:
                if s[i-1][j+1] == '
                    m += 1
            if j-1 >= 0:
                if s[i][j-1] == '
                    m += 1
            if j+1 < w:
                if s[i][j+1] == '
                    m += 1
            if i+1 < h and j-1 >= 0:
                if s[i+1][j-1] == '
                    m += 1
            if i+1 < h:
                if s[i+1][j] == '
                    m += 1
            if i+1 < h and j+1 < w:
                if s[i+1][j+1] == '
                    m += 1
            s[i][j] = str(m)
for i in range(h):
    z = list(s[i])
    print(''.join(z))

Solution (Python):
h,w = map(int, input().split())
s = list()
t = list()
a = [0 for i in range(w+2)]
s.append(a)
for i in range(h):
    b = str(input())
    c = list(('0'+b+'0').replace('.','0').replace('
    d = list(map(int, c))
    s.append(d)
    t.append(b)
s.append(a)

for j in range(1,h+1):
    for k in range(1,w+1):
        if s[j][k] == 0:
            cnt = 0
            cnt = (s[j-1][k-1]+s[j-1][k]+s[j-1][k+1]) + (s[j][k-1]+s[j][k+1]) + (s[j+1][k-1]+s[j+1][k]+s[j+1][k+1])
            t[j-1] = t[j-1].replace('.', str(cnt), 1)
            
        else:
            continue
for i in range(h):
    print(t[i])

Solution (Python):

import sys


def solve(H: int, W: int, S: "List[str]"):
    for i in range(H):
        for j in range(W):
            if S[i][j] == "
                print("
                continue
            n = 0
            if i - 1 >= 0:
                if S[i - 1][j] == "
                    n += 1
                if j + 1 < W and S[i - 1][j + 1] == "
                    n += 1
                if j - 1 >= 0 and S[i - 1][j - 1] == "
                    n += 1
            if i + 1 < H:
                if S[i + 1][j] == "
                    n += 1
                if j + 1 < W and S[i + 1][j + 1] == "
                    n += 1
                if j - 1 >= 0 and S[i + 1][j - 1] == "
                    n += 1
            if j + 1 < W and S[i][j + 1] == "
                n += 1
            if j - 1 >= 0 and S[i][j - 1] == "
                n += 1
            print(n, end="")
        print()
    return



def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word

    tokens = iterate_tokens()
    H = int(next(tokens))  
    W = int(next(tokens))  
    S = [list(next(tokens)) for _ in range(H)]  
    solve(H, W, S)


if __name__ == "__main__":
    main()

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

for i in range(1, h + 1):
    for j in range(1, w + 1):
        if s[i - 1][j - 1] == "
            ans[i - 1][j - 1] += 1
            ans[i - 1][j] += 1
            ans[i - 1][j + 1] += 1
            ans[i][j - 1] += 1
            ans[i][j + 1] += 1
            ans[i + 1][j - 1] += 1
            ans[i + 1][j] += 1
            ans[i + 1][j + 1] += 1

del ans[0]
del ans[-1]
for i in range(h):
    del ans[i][0]
    del ans[i][-1]
for i, _ in enumerate(s):
    tmp = ""
    for j, word in enumerate(_):
        if word == "
            tmp += "
        else:
            tmp += str(ans[i][j])
    print(tmp, end="\n")

Solution (Python):
import sys
from collections import deque
from bisect import bisect_left, bisect_right, insort_left, insort_right 
from heapq import heapify, heappop, heappush

sys.setrecursionlimit(10**6)
INF = 10**20

def mint():
    return map(int,input().split())
def lint():
    return map(int,input().split())
def judge(x, l=['Yes', 'No']):
    print(l[0] if x else l[1])

H,W = mint()
S = [input() for _ in range(H)]

for i in range(H):
    for j in range(W):
        if S[i][j]=='
            print('
        else:
            tmp = 0
            for a in [-1,0,1]:
                for b in [-1,0,1]:
                    if (a,b)!=(0,0) and 0<=i+a<H and 0<=j+b<W and S[i+a][j+b]=='
                        tmp += 1
            print(tmp,end='')
    print('')

Solution (Python):
from itertools import product as pro
h, w = map(int, input().split())
s = [list(input()) for i in range(h)]
ans = [["
visited = []

def dfs(p):
    if p in visited or s[p[0]][p[1]] == "
        return None

    temp = [1 if s[y+p[0]][x+p[1]] == '
    s[p[0]][p[1]] = sum(temp)


for i in range(len(s)):
    for j in range(len(s[0])):
        dfs([i, j])


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

Solution (Python):
import sys
import math
import itertools
import collections
import heapq
import re
import numpy as np
from functools import reduce

rr = lambda: sys.stdin.readline().rstrip()
rs = lambda: sys.stdin.readline().split()
ri = lambda: int(sys.stdin.readline())
rm = lambda: map(int, sys.stdin.readline().split())
rl = lambda: list(map(int, sys.stdin.readline().split()))
inf = float('inf')
mod = 10**9 + 7


h, w = rm()
s = [rr() for _ in range(h)]
x = [0, 0, 1, -1, 1, 1, -1, -1]
y = [1, -1, 0, 0, 1, -1, 1, -1]
ans = ['']*h
for i in range(h):
    for j in range(w):
        if s[i][j] == '
            ans[i] += '
        else:
            c = 0
            for dx, dy in zip(x, y):
                if 0 <= i + dx < h and 0 <= j + dy < w:
                    if s[i+dx][j+dy] == '
                        c += 1
            ans[i] += str(c)
for i in ans:
    print(i)

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

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

for i in range(h):
  for j in range(w):
    if l[i][j]=='
      ans[i][j]='
    else:
      for p in range(-1,2):
        for q in range(-1,2):
          if 0<=i+p<h and 0<=j+q<w and l[i+p][j+q]=='
            ans[i][j]+=1
            
for i in range(h):
  print(*ans[i],sep='')

Solution (Python):

h, w = map(int, input().split())
s = [list(input()) for _ in range(h)]
ans = [[0] * w for _ in range(h)]
x = [-1, 0, 1, -1, 1, -1, 0, 1]
y = [-1, -1, -1, 0, 0, 1, 1, 1]




for i in range(h):
    for j in range(w):
        if s[i][j] == '
            ans[i][j] = '
        else:
            for k in range(len(x)):
                if 0 <= (i + x[k]) < h and 0 <= (j + y[k]) < w and s[i + x[k]][j + y[k]] == '
                    ans[i][j] += 1

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

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


for h in range(H):
    ans = ""
    for w in range(W):
        if XY[h][w] == "
            ans += "
            continue
        cnt = 0
        for h_diff in [-1, 0, 1]:
            for w_diff in [-1, 0, 1]:
                if h + h_diff < 0 or w + w_diff < 0:
                    continue
                elif h + h_diff >= H or w + w_diff >= W:
                    continue
                else:
                    if XY[h + h_diff][w + w_diff] == "
                        cnt += 1
        ans += str(cnt)
    print(ans)

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

data=[[0]*w for i in range(h)]
for i in range(h):
  for j in range(w):
    if s[i][j]=='
      data[i][j]='
    else:
      for p in range(-1,2):
        for q in range(-1,2):
          if 0<=i+p<h and 0<=j+q<w and s[i+p][j+q]=='
            data[i][j]+=1
for i in range(h):
  print(*data[i],sep='')

Solution (Python):
h,w = map(int,input().split())
l = [["."]*(w+2)]
for i in range(h):
    l.append(["."] + [i for i in input()] + ["."])
l.append(["."]*(w+2))
 
def count(i,j):
    if l[i][j] == "
        return "
    c = 0
    for n in range(i-1,i+2):
        for m in range(j-1,j+2):
            if l[n][m] == "
                c += 1
    return str(c)
 
ans = ["".join([count(i,j) for j in range(1,1+w)]) for i in range(1,1+h)]
 
print("\n".join(ans))

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

S = [input() for _ in range(H)]
M = [[""]*W for _ in range(H)]

def bomb_cnt(i,j):
  min_h = max(0,i-1)
  max_h = min(i+2,H)
  min_w = max(0,j-1)
  max_w = min(j+2,W)
  cnt = 0
  for k in range(min_h,max_h):
    for g in range(min_w,max_w):
      if (S[k][g] == "
        cnt += 1
        
  return str(cnt)

for i in range(H):
  for j in range(W):
    if S[i][j] == "
      M[i][j] = "
    else:
      M[i][j] = bomb_cnt(i,j)
      
      
for i in range(H):
  print(*M[i],sep="")

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

elif (h == 1)&(w!=1):
    for j in range(w):
        if j == 0:
            s[0][j] = s[0][j].replace(".",str(s[0][j+1].count("
        elif j == w-1:
            s[0][j] = s[0][j].replace(".",str(s[0][j-1].count("
        else:
            s[0][j] = s[0][j].replace(".",str([s[0][j-1],s[0][j+1]].count("

elif (h != 1)&(w==1):
    for i in range(h):
        if i == 0:
            s[i][0] = s[i][0].replace(".",str(s[i+1][0].count("
        elif i == h-1:
            s[i][0] = s[i][0].replace(".",str(s[i-1][0].count("
        else:
            s[i][0] = s[i][0].replace(".",str([s[i-1][0],s[i+1][0]].count("
else:
    for i in range(len(s)):
        if i == 0:
            for j in range(w):
                if j == 0:
                    s[i][j] = s[i][j].replace(".",str([s[i][j+1],s[i+1][j],s[i+1][j+1]].count("
                elif j == w-1:
                    s[i][j] = s[i][j].replace(".",str([s[i][j-1],s[i+1][j],s[i+1][j-1]].count("
                else:
                    s[i][j] = s[i][j].replace(".",str([s[i][j-1],s[i][j+1],s[i+1][j],s[i+1][j+1],s[i+1][j-1]].count("
        elif i == h-1:
            for j in range(w):
                if j == 0:
                    s[i][j] = s[i][j].replace(".",str([s[i][j+1],s[i-1][j],s[i-1][j+1]].count("
                elif j == w-1:
                    s[i][j] = s[i][j].replace(".",str([s[i][j-1],s[i-1][j],s[i-1][j-1]].count("
                else:
                    s[i][j] = s[i][j].replace(".",str([s[i][j-1],s[i][j+1],s[i-1][j],s[i-1][j+1],s[i-1][j-1]].count("
        else:
            for j in range(w):
                if j == 0:
                    s[i][j] = s[i][j].replace(".",str([s[i][j+1],s[i-1][j],s[i-1][j+1],s[i+1][j],s[i+1][j+1]].count("
                elif j == w-1:
                    s[i][j] = s[i][j].replace(".",str([s[i][j-1],s[i-1][j],s[i-1][j-1],s[i+1][j],s[i+1][j-1]].count("
                else:
                    s[i][j] = s[i][j].replace(".",str([s[i][j-1],s[i][j+1],s[i-1][j],s[i-1][j+1],s[i-1][j-1],s[i+1][j],s[i+1][j+1],s[i+1][j-1]].count("

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

Solution (Python):
H,W=map(int,input().split())
S=[input() for _ in range(H)]
ans=[[0]*W for _ in range(H)]
x=(-1,0,1,-1,0,1,-1,0,1)
y=(1,1,1,0,0,0,-1,-1,-1)
for i in range(H):
  for j in range(W):
    if S[i][j]==".":
      for k in range(9):
        a=i+y[k]
        b=j+x[k]
        if 0<=a<H and 0<=b<W:
          if S[a][b]=="
            ans[i][j]+=1
for i in range(H):
  for j in range(W):
    if S[i][j]=="
      print("
    else:
      print(ans[i][j],end="")
  print()

Solution (Python):
import sys

readline = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)


def main():
    H, W = map(int, readline().split())

    grid = ["." * (W + 2)]

    for _ in range(H):
        s = "." + input() + "."
        grid.append(s)
    grid.append("." * (W + 2))

    res = []
    for row in range(1, H + 1):
        cur = ""
        for col in range(1, W + 1):
            if grid[row][col] == "
                cur += "
            else:
                cnt = 0
                for r in (row - 1, row, row + 1):
                    for c in (col - 1, col, col + 1):
                        if grid[r][c] == "
                            cnt += 1
                cur += str(cnt)
        res.append(cur)

    for x in res:
        print(x)


if __name__ == '__main__':
    main()

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

result = []
for i, r in enumerate(rl):
    result_r = ""
    pre_r = rl[i - 1] if i - 1 >= 0 else ""
    next_r = rl[i + 1] if i + 1 < H else ""
    for j, p in enumerate(r):
        if p == "
            result_r += "
            continue
        pre_j = j - 1 if j - 1 >= 0 else 0
        next_j = j + 2
        around_str = pre_r[pre_j:next_j] + r[pre_j:next_j] + next_r[pre_j:next_j]
        result_r += str(around_str.count("
    result.append(result_r)

print("\n".join(result))

Solution (Python):
from sys import stdin
def input():
    return stdin.readline().strip()

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

ans = s.copy()
for i in range(h):
    ans[i] = list(ans[i])

for x in range(h):
    for y in range(w):
        if s[x][y] == '.':
            num = 0
            for dx in [-1, 0, 1]:
                for dy in [-1, 0, 1]:
                    if 0 <= x + dx < h and 0 <= y + dy < w and s[x+dx][y+dy] == '
                        num += 1
            ans[x][y] = str(num)

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

Solution (Python):

def mine_count(x, y, ans):
    ans[y][x] = 0
    for nx, ny in (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1), (-1, 0), (-1, 1):
        if 0 <= y+ny < H and 0 <= x+nx < W:
            if ans[y+ny][x+nx] == "
                ans[y][x] += 1
    return ans


H, W = map(int, input().split())
S = [list(input()) for _ in range(H)]
ans = S
for i in range(H):
    for j in range(W):
        if S[i][j] == ".":
            ans = mine_count(j, i, ans)

for row in ans:
    for item in row:
        print(item, end="")
    print()

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

search = [(row,col) for row in range(-1,2) for col in range(-1,2) if not(row == 0 and col == 0)]


for i in range(h):
    ans = ""
    for j in range(w):
        if(grid[i][j] == "."):
            bomb = 0
            for k in range(8):
                row,col = search[k]
                if(0<=i+row<h and 0<=j+col<w):
                    if(grid[i+row][j+col] == "
                        bomb += 1
            grid[i][j] = bomb
        ans+=str(grid[i][j])
    print(ans)

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

Solution (Python):
h, w = (int(x) for x in input().split())
s = []
ans = []
rev = ""
for row in range(1, h+1):
    for col, t in enumerate(input()):
        s.append([(row,col+1),t])

for index, factor in enumerate(s):
    count =0
    if factor[1] != "
        if w == 1:
            for point in [index-1, index+1]:
                if 0 <= point and point < len(s) and s[point][1] == "
                    count += 1
        elif (index+1)%w == 1:
            for point in [index +1,index -w+1, index -w, index +w, index +w+1]:
                if 0 <= point and point < len(s) and s[point][1] == "
                    count += 1
        elif (index+1)%w == 0:
            for point in [index -1,index -w, index -w-1, index +w-1, index +w]:
                if 0 <= point and point < len(s) and s[point][1] == "
                    count += 1
        else:
            for point in [index -1, index +1,index -w+1, index -w, index -w-1, index +w-1, index +w, index +w+1]:
                if 0 <= point and point < len(s) and s[point][1] == "
                    count += 1
        ans.append(count)
    else:
        ans.append("

while(ans != []):
    for num in range(w):
        rev += str(ans[0])
        del ans[0]
    rev += "\n"

print(rev)

Solution (Python):
dx = [1, 0, -1, 0, 1, -1, -1, 1]
dy = [0, 1, 0, -1, 1, 1, -1, -1]


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


for i in range(H):
    for j in range(W):
        if S[i][j] != '.':
            continue
        counter = 0
        for d in range(8):
            ni = i + dx[d]
            nj = j + dy[d]
            if ni < 0 or ni >= H or nj < 0 or nj >= W:
                continue
            if S[ni][nj] == '
                counter += 1
        S[i] = S[i].replace('.', str(counter), 1)

for row in S:
    print(row)

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

def count(i,j):
    if l[i][j] == "
        return "
    c = 0
    for n in range(i-1,i+2):
        for m in range(j-1,j+2):
            if l[n][m] == "
                c += 1
    return str(c)

ans = ["".join([count(i,j) for j in range(1,1+w)]) for i in range(1,1+h)]

print("\n".join(ans))

Solution (Python):

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


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



result = [[0 if v == '.' else '


for i in range(H):
    for j in range(W):
        if S[i][j] != '.':
            continue
        for d in range(8):
            ni = i + dx[d]
            nj = j + dy[d]
            if ni < 0 or ni >= H or nj < 0 or nj >= W:
                continue
            if S[ni][nj] == '
                result[i][j] += 1

for row in result:
    
    print(*row, sep = '')

Solution (Python):
dx = [1, 0, -1, 0, 1, -1, -1, 1]
dy = [0, 1, 0, -1, 1, 1, -1, -1]


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



result = [[0 if v == '.' else '


for i in range(H):
    for j in range(W):
        if S[i][j] != '.':
            continue
        for d in range(8):
            ni = i + dx[d]
            nj = j + dy[d]
            if ni < 0 or ni >= H or nj < 0 or nj >= W:
                continue
            if S[ni][nj] == '
                result[i][j] += 1

for row in result:
    print(*row, sep = '')

Solution (Python):
H,W=list(map(int,input().split()))
l=[list(input()) for i in range(H)]
ans_l=[[0]*W for i in range(H)]
for i in range(H):
   for j in range(W):
      if l[i][j] == ".":
         tmp=0
         for x in range(max(0,j-1),min(W-1,j+1)+1):
            for y in range(max(0,i-1),min(H-1,i+1)+1):
               if l[y][x]=="
                  tmp+=1
         ans_l[i][j]=str(tmp)
      else:
         ans_l[i][j]="
for i in ans_l:
   print(*i,sep="")

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

Solution (Python):
import sys, itertools
input = lambda: sys.stdin.readline().rstrip() 
sys.setrecursionlimit(10**7)
INF = 10**20
def I(): return int(input())
def F(): return float(input())
def S(): return input()
def LI(): return [int(x) for x in input().split()]
def LI_(): return [int(x)-1 for x in input().split()]
def LF(): return [float(x) for x in input().split()]
def LS(): return input().split()

def resolve():
    H, W = LI()
    s = [list(S()) for _ in range(H)]

    for i, j in itertools.product(range(H), range(W)):
        if s[i][j] != '
            cnt = 0
            for k, l in itertools.product(range(-1, 2), repeat=2):
                if 0 <= i + k < H and 0 <= j + l < W:
                    if s[i+k][j+l] == '
                        cnt += 1
            s[i][j] = str(cnt)

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

if __name__ == '__main__':
    resolve()

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):
h,w=map(int,input().split())
s=["."*(w+2)]
for i in range(h):
    s.append("."+input()+".")
s.append("."*(w+2))
dx=[-1,0,1,1,1,0,-1,-1]
dy=[1,1,1,0,-1,-1,-1,0]
ans=[]
for i in range(1,h+1):
    wp=""
    for j in range(1,w+1):
        if s[i][j]=="
            wp+="
            continue
        count=0
        for k in range(8):
            if s[i+dy[k]][j+dx[k]]=="
                count+=1
        wp+=str(count)
    ans.append(wp)
print(*ans,sep="\n")