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())
src = [input() for i in range(H)]
 
ans = []
for row in src:
    ans.append(list(row))
 
dxy = [(-1,-1),(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1)]
for y in range(H):
    for x in range(W):
        if src[y][x] == '
        c = 0
        for dx,dy in dxy:
            if x+dx < 0 or x+dx > W-1 or y+dy < 0 or y+dy > H-1: continue
            if src[y+dy][x+dx] == '
        ans[y][x] = c
 
for row in ans:
    print(''.join(list(map(str,row))))

Solution (Python):
H,W = [int(i) for i in input().split()]
li = ['0'] * H
for i in range(H) :
    li[i] = list(input())
for i in range(H) :
    for j in range(W) :
        if li[i][j] == "." :
            count = 0
            a = i -1
            b = i + 1
            c = j - 1
            d = j + 1
            if a >= 0 :
                if li[a][j] == "
                    count+=1
            if b <= H-1 :
                if li[b][j] == "
                    count+=1
            if c >= 0 :
                if li[i][c] == "
                    count+=1
            if d <= W-1 :
                if li[i][d] == "
                    count+=1
            if a >= 0 and c >= 0 :
                if li[a][c] == "
                    count+=1
            if a >= 0 and d <= W-1 :
                if li[a][d] == "
                    count+=1
            if b <= H-1 and c >= 0 :
                if li[b][c] == "
                    count+=1
            if b <= H-1 and d <= W - 1 :
                if li[b][d] == "
                    count+=1
            li[i][j] = str(count)
for i in range(H) :
    a = ''.join(li[i])
    print(a)

Solution (Python):
def MineSweeper():
    H, W = map(int, input().split())
    strlist = []
    newlist = []
    for _ in range(0, H):
        strlist.append(list(input()))
    for i in range(0, H):
        templist = []
        for j in range(0, W):
            templist.append(0)
            if strlist[i][j] == '
                templist[j] = '
            else:
                for k in range(-1, 2):
                    if i + k == -1 or i + k == H:continue

                    for l in range(-1, 2):
                        if l + j == -1 or l + j == W:continue
                        if strlist[i + k][j + l] == '
                            templist[j] += 1
        newlist.append(templist)

    for lst in newlist:
        print(''.join(map(str, lst)))

MineSweeper()

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

c = 0
for i in range(H):
  if S[i] != "
    for j in range(W):
      if S[i][j] == ".":
        S[i][j] = "0"
      else:
        c += 1

if c != H * W:
 for x in range(H):
  for y in range(W):
    if S[x][y] == "
      for z in range(8):
        X = x + dx[z]
        Y = y + dy[z]
        if 0 <= X <= H-1  and  0 <= Y <= W-1:
          if S[X][Y] != "
            S[X][Y] = str(int(S[X][Y]) + 1)


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

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):
h,w=map(int,input().split())

li=[]
for i in range(h):
    li.append(list(input()))

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


sharp_li=[]
for i in range(h):
    for j in range(w):
        if li[i][j]=='
            sharp_li.append([i,j])   

for k in range(len(sharp_li)):
    
    y=sharp_li[k][0]
    x=sharp_li[k][1]
    
    
    if x==0:
        if w!=1:             
            xli=[0,1]
        else:
            xli=[0]
    elif x==w-1:
        xli=[w-2,w-1]
    else:
        xli=[x-1,x,x+1]
    
    if y==0:
        if h!=1:
            yli=[0,1]
        else:
            yli=[0]
    elif y==h-1:
        yli=[h-2,h-1]
    else:
        yli=[y-1,y,y+1]
        
        
    for l in xli:
        for m in yli:
            new_li[m][l]+=1           
           
        
ans=[]             
for n in range(h):
    stred=list(map(str,new_li[n]))
    ans.append(stred)


for o in range(len(sharp_li)):
    ans[sharp_li[o][0]][sharp_li[o][1]]='


for p in range(h):
    tans=''.join(ans[p])
    print(tans)

Solution (Python):
H,W=map(int,input().split(' '))
mine = [input() for i in range(H)]
def mine_chk(h,w):
    cnt = 0
    for i in range(max(0,h-1),min(h+2,H)):
        for j in range(max(0,w-1),min(W,w+2)):
            if mine[i][j] =="
                cnt += 1
    return cnt



for i in range(H):
    for j in range(W):
        if mine[i][j] == "
            print("
        else:
            print(mine_chk(i,j),end="")
    print("")

Solution (Python):
"""import sys
input = sys.stdin.readline().strip"""
h,w=map(int,input().split())

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

ans=[["
for i in range(h):
    for j in range(w):
        if s[i][j]=="
            continue
        cnt=0
        for k in [-1,0,1]:
            for l in [-1,0,1]:
                if k==0 and l==0:
                    continue
                if 0<=i+k<h and 0<=j+l<w:
                    if s[i+k][j+l]=="
                        cnt+=1
        ans[i][j]=cnt

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

Solution (Python):
h,w = list(map(int,input().split()))
s=[i for i in range(h)]
for i in range(h):
  s[i] = input()
count = [[0 for i in range(w)]for j in range(h)]
if h==1and w==1:
  if s=="
    print("
    exit()
  else:
    print(0)
    exit()
  
elif h==1:
  for i in range(w):
    if s[0][i]=="
      if i==0:
        count[0][i+1]+=1
      elif i==w-1:
        count[0][i-1]+=1
      else:
        count[0][i-1]+=1
        count[0][i+1]+=1
  ans=""
  for i in range(w):
    if s[0][i]!="
      ans+=str(count[0][i])
    else:
      ans+="
  print(ans)
  exit()
elif w==1:
  for i in range(h):
    if s[i][0]=="
      if i==0:
        count[i+1][0]+=1
      elif i==h-1:
        count[i-1][0]+=1
      else:
        count[i-1][0]+=1
        count[i+1][0]+=1
  
  for i in range(h):
    ans=""
    if s[i][0]!="
      ans+=str(count[i][0])
    else:
      ans+="
    print(ans)
  exit()


for i in range(h):
  for j in range(w):
    if s[i][j]=="
      if i==0:
        if j==0:
          count[i+1][j]+=1
          count[i+1][j+1]+=1
          count[i][j+1]+=1
        elif j==w-1:
          count[i][j-1]+=1
          count[i+1][j-1]+=1
          count[i+1][j]+=1
        else:
          count[i][j-1]+=1
          count[i][j+1]+=1
          count[i+1][j-1]+=1
          count[i+1][j]  +=1
          count[i+1][j+1]+=1
      elif i==h-1:
        if j==0:
          count[i][j+1]+=1
          count[i-1][j]+=1
          count[i-1][j+1]+=1
        elif j==w-1:
          count[i][j-1]+=1
          count[i-1][j-1]+=1
          count[i-1][j]+=1
        else:
          count[i][j-1]+=1
          count[i][j+1]+=1
          count[i-1][j-1]+=1
          count[i-1][j]+=1
          count[i-1][j+1]+=1
      else:
        if j==0:
          count[i-1][j]+=1
          count[i-1][j+1]+=1
          count[i][j+1]+=1
          count[i+1][j]+=1
          count[i+1][j+1]+=1
        elif j==w-1:
          count[i-1][j-1]+=1
          count[i-1][j]+=1
          count[i][j-1]+=1
          count[i+1][j-1]+=1
          count[i+1][j]+=1
        else:
          count[i-1][j-1]+=1
          count[i-1][j]+=1
          count[i-1][j+1]+=1
          count[i][j-1]+=1
          count[i][j+1]+=1
          count[i+1][j-1]+=1
          count[i+1][j]+=1
          count[i+1][j+1]+=1

for i in range(h):
  ans=""
  for j in range(w):
    if s[i][j]!="
  	  ans+=str(count[i][j])
    else:
      ans+="
  print(ans)

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

for i in range(H):
  for j in range(W):
    if S[i][j] == '
      result[i][j] = '
      continue
    if i-1 >= 0 and j-1 >= 0 and S[i-1][j-1] == '
      result[i][j] += 1
    if i-1 >= 0 and S[i-1][j] == '
      result[i][j] += 1
    if i-1 >= 0 and j+1 < W and S[i-1][j+1] == '
      result[i][j] += 1
    if j-1 >= 0 and S[i][j-1] == '
      result[i][j] += 1
    if j+1 < W and S[i][j+1] == '
      result[i][j] += 1
    if i+1 < H and j-1 >= 0 and S[i+1][j-1] == '
      result[i][j] += 1
    if i+1 < H and S[i+1][j] == '
      result[i][j] += 1
    if i+1 < H and j+1 < W and S[i+1][j+1] == '
      result[i][j] += 1

for i in range(H):
  ans = ''
  for j in range(W):
    ans += str(result[i][j])
  print(ans)

Solution (Python):
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
from bisect import bisect

def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

H, W = MAP()
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):
		tmp = 0
		for k in range(8):
			x = j+dx[k]
			y = i+dy[k]
			if 0 <= x <= W-1 and 0 <= y <= H-1:
				if S[y][x] == "
					tmp += 1
		if not S[i][j] == "
			S[i][j] = tmp

for i in range(H):
	print(*map(str, S[i]), sep="")

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

for i in range(h):
    data[i] = list(data[i])

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

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

for x in range(h):
    for y in range(w):
        if data[x][y] == '
            ans[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
            if data[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=[]
for i in range(h):
  s.append([i for i in input()])
l=[["" for i in range(w)] for j in range(h)]
for hh in range(h):
  for ww in range(w):
    if s[hh][ww]=="
      l[hh][ww]="
    else:  
      cnt=0
      for hhh in [-1,0,1]:
        for www in [-1,0,1]:
          if 0<=hh+hhh<h and 0<=ww+www<w and s[hh+hhh][ww+www]=="
            cnt+=1
      l[hh][ww]=cnt
for i in l:
  for j in i:
    print(j,end="")
  print()

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

s = [''] * h

for l in range(h):
  s[l] = list(input())

result = [''] * h
for i in range(h):
  result[i] = [''] * w
  for j in range(w):
    count = 0
    if s[i][j] == "
      result[i][j] = "
    else:
      for n in [-1, 0, 1]:
        for m in [-1, 0, 1]:
          if (i + n < 0) or (j + m < 0) or (i + n >= h) or (j + m >= w):
            continue
          elif s[i + n][j + m] == "
            count += 1
      result[i][j] = str(count)

for x in range(h):
  print(''.join(result[x]))

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

for i in range(h):
    ans = ""
    for j in range(w):
        if s[i][j] == "
            ans += "
        else:
            ans += str(sum(k[max(0, j-1):min(w, j+2)].count('
    print(ans)

Solution (Python):
import sys
input = sys.stdin.readline
H, W = [int(x) for x in input().rstrip().split()]
s = []
for _ in range(H):
    s.append(list(input().rstrip()))

def bomb(x, y):
    cnt = 0
    for k in [[x+1, y], [x+1, y+1], [x+1, y-1], [x, y+1], [x, y-1], [x-1, y], [x-1, y+1], [x-1, y-1]]:
        if 0 <= k[0] < H and 0 <= k[1] < W:
            if s[k[0]][k[1]] == "
                cnt += 1
    return str(cnt)

for i in range(H):
    for j in range(W):
        if s[i][j] == ".":
            s[i][j] = bomb(i, j)
for l in s:
    print("".join(l))

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

ans = [[0]*w for i in range(h)]
pattern = [[-1, -1], [-1, 0], [-1, 1], [0, -1], [0, 1], [1, -1], [1, 0], [1, 1]]
for y in range(h):
    for x in range(w):
        if graph[y][x] == '
            ans[y][x] = float('INF')

            for dy, dx in pattern:
                ny, nx = y + dy, x + dx
                if ny < 0 or ny >= h or nx < 0 or nx >= w:
                    continue

                ans[ny][nx] += 1
        else:
            continue

for line in ans:
    print(''.join(map(str, line)).replace('inf', '

Solution (Python):
import sys

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

rows = []
for i in range(H):
    chars = list(input())
    rows.append([1 if c == '

for h in range(H):
    for w in range(W):
        if rows[h][w] == 1:
            sys.stdout.write('
            continue

        range_w = (max(0, w - 1), w + 2)
        range_h = (max(0, h - 1), h + 2)

        target_rows = [
            sample_row[range_w[0]:range_w[1]]
            for sample_row in rows[range_h[0]:range_h[1]]
        ]

        conv_result = sum([sum(r) for r in target_rows])
        sys.stdout.write(str(conv_result))
    
    print()

Solution (Python):


h, w = map(int, input().split())

area = []
for i in range(h):
    area.append(input())


for i in range(h):
    for j in range(w):
        if area[i][j] == "
            print("
        else:
            count = 0
            for k in range(max(0, i-1), min(i+1, h-1)+1):
                for l in range(max(0, j-1), min(j+1, w-1)+1):
                    if area[k][l] == "
                        count += 1
            print(count, end = "")
    print()

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

for i in range(H):
  for j in range(W):
    if area[i][j] == '
      print('
    else:
      count = 0
      for k in range(max(0, i-1), min(i+1, H-1)+1):
        for l in range(max(0, j-1), min(j+1, W-1)+1):
          if area[k][l] == '
            count += 1
      print(count, end='')
  print()

Solution (Python):
def minesweeper(s, i, j):
    ans = 0
    for ii in range(-1,2):
        for jj in range(-1,2):
            try:
                if s[i+ii][j+jj] == "
                    ans += 1
            except:
                None
    return ans

h, w = map(int, input().split())
s = [input() for i in range(h)]
for i in range(h):
    for j in range(w):
        if s[i][j] == ".":
            print(minesweeper(s, i, j), end="")
        else:
            print("
    print("")

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


grid = [[0 if cell=="." else "

import itertools
for r in range(H):
  for w in range(W):
    if S[r][w] != "
      continue
    for dx, dy in itertools.product([-1, 0, 1], repeat=2):
      x = r+dx
      y = w+dy
      if x < 0 or x >= H or y < 0 or y >= W:
        continue
      if S[x][y] == "
        continue
      grid[x][y] += 1
for row in grid:
  print(*row, sep="")

Solution (Python):

 
import math
import itertools
import sys
import copy
 




H, W = map(int, input().split())
S = []
StartEnd = []
for _ in range(W+2) :
  StartEnd.append("N")
S.append(StartEnd)
for _ in range(H) :
  l = list(str(input()))
  l.insert(0, "N")
  l.append("N")
  S.append(l)
S.append(StartEnd)


for i in range(H+2) : 
  for j in range(W+2) :
    if S[i][j] == "." :
      cnt = 0
      for m in range(-1, 2) :
        for n in range(-1, 2) :
          if S[i+m][j+n] == "
            cnt += 1
      S[i][j] = str(cnt)

for i in range(1, H+1) :
  ans = ""
  for j in range(W+2) :
    if S[i][j] != "N" :
      ans += S[i][j]
  print (ans)

Solution (Python):
def check_bomb(S_list,h,w):
    num_of_bomb=0
    for i in range(3):
        if h-1+i<0 or h-1+i>len(S_list)-1:
            continue

        for j in range(3):
            if w-1+j<0 or w-1+j>len(S_list[0])-1:
                continue
            if S_list[h-1+i][w-1+j]=="
                num_of_bomb+=1
    return "{}".format(num_of_bomb)



def main():
    input_str1 = list(map(int, input().split()))
    H = input_str1[0]
    W = input_str1[1]
    S_list = []
    for i in range(H):
        S_list.append(input())

    for i in range(H):
        for j in range(W):
            if S_list[i][j]==".":
                temp_list = S_list[i]
                temp_list = temp_list[:j]+check_bomb(S_list,i,j)+temp_list[j+1:]
                S_list[i]=temp_list
                
    for i in range(H):
        print(S_list[i])




main()

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

def bomb(x,y):
    if s[y][x]=='
        return '
    else:
        c=0
        for i in range(-1,2):
            for j in range(-1,2):
                if not (i==0 and j==0):
                    if 0<=x+i<=w-1 and 0<=y+j<=h-1:
                        if s[y+j][x+i]=='
                            c+=1
        return str(c)

for i in range(h):
    l=[]
    for j in range(w):
        l.append(bomb(j,i))
    print(''.join(l))

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

for i in range(h):
    lst = []
    for j in range(w):
        cnt = 0
        if s[i][j]=='.':
            for m in range(3):
                for n in range(3):
                    if 0<=i-1+m<=h-1 and 0<=j-1+n<=w-1:
                        if s[i-1+m][j-1+n]=='
                            cnt += 1
            lst.append(str(cnt))
        else:
            lst.append('

    x.append(''.join(lst))

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

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

for i in range(H):
  for j in range(W):
    if area[i][j] == '
      print('
    else:
      count = 0
      for k in range(max(0, i-1), min(i+1, H-1)+1):
        for l in range(max(0, j-1), min(j+1, W-1)+1):
          if area[k][l] == '
            count += 1
      print(count, end='')
  print()

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

def plus(x,y):

    if (0 <= x < h) and (0 <= y < w):
        
        if s[x][y] == '.':
            s[x][y] = 1
        elif s[x][y] != '
            s[x][y] += 1

for i in range(h):
    for j in range(w):
        if s[i][j] == '.':
            s[i][j] = 0
        if s[i][j] == "
            for k in range(-1,2):
                for l in range(-1,2):
                    plus(i+k,j+l)

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

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

A = [''] * H

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

for h in range(H):
    for w in range(W):
        if S[h][w] == '
            A[h] += '
        else:
            c = 0
            for d in di:
                h1 = h + d[0]
                w1 = w + d[1]
                if 0 <= h1 < H and 0 <= w1 < W:
                    if S[h1][w1] == '
                        c += 1
            A[h] += str(c)

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

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

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

m = []

for _ in range(H):
    S = list(input())
    m.append(S)

a = []
b = [-1,1,0]

for i in range(len(b)):
    for j in range(len(b)):
        a.append([b[i],b[j]])

for i in range(H):
    for j in range(W):
        if m[i][j]=="
            continue
        count = 0
        for k in range(len(a)):
            x = a[k][0]
            y = a[k][1]
            if (x+j)<0 or (y+i)<0 or (x+j)>=W or (y+i)>=H:
                continue
            if m[y+i][x+j]=="
                count = count + 1
        m[i][j]=count

for i in range(H):
    print(*m[i],sep="")

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

m = []

for _ in range(H):
    S = list(input())
    m.append(S)
a = [[-1,1],[0,1],[1,1],[-1,0],[1,0],[-1,-1],[0,-1],[1,-1]]
for i in range(H):
    for j in range(W):
        if m[i][j]=="
            continue
        count = 0
        for k in range(len(a)):
            x = a[k][0]
            y = a[k][1]
            if (x+j)<0 or (y+i)<0 or (x+j)>=W or (y+i)>=H:
                continue
            if m[y+i][x+j]=="
                count = count + 1
        m[i][j]=count

for i in range(H):
    print(*m[i],sep="")

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

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

for i in range(H):
    for j in range(W):
        if list[i][j] == "
            continue
        num = 0
        for k in range(8):
            x = i + dx[k]
            y = j + dy[k]
            if x < 0 or x >= H:
                continue
            if y < 0 or y >= W:
                continue
            if list[x][y] == "
                num += 1
        list[i][j] = num
        
for l in list:
    print(''.join([str(c) for c in l]))

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

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

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

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

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

Solution (Python):
h,w=map(int,input().split())
S=list(input() for _ in range(h))
ans=[[0]*(w+2) for _ 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
for i in range(h):
    for j in range(w):
        if S[i][j]=="
            ans[i+1][j+1]  = "
        print(ans[i+1][j+1],end="")
        if j==w-1 and i<h-1:print("")

Solution (Python):
H, W = map(int, input().split())
S = [input() for _ in range(H)]
result = []
for h in range(H):
    for w in range(W):
        if S[h][w] == "
            result.append("
        else:
            count = 0
            for h2 in range(h - 1, h + 2):
                for w2 in range(w - 1, w + 2):
                    if 0 <= h2 < H and 0 <= w2 < W:
                        if S[h2][w2] == "
                            count += 1
            result.append(str(count))
    print("".join(result))
    result.clear()

Solution (Python):
H,W = map(int,input().split())
S = [list(input()) for i in range(H)]
dx = [1,1,0,-1,-1,-1,0,1]
dy = [0,1,1,1,0,-1,-1,-1]

S_count = []
for i in range(H):
    Count = []
    for j in range(W):
        if S[i][j] == ".":
            count = 0
            for t in range(8):
                if j+dx[t] <0 or j+dx[t] > W-1 or i+dy[t] <0 or i+dy[t] >H-1:
                    pass
                elif S[i+dy[t]][j+dx[t]] == "
                    count += 1
                else:
                    pass
        else:
            count = "
        Count.append(count)
    S_count.append(Count)
for i in range(H):
    R = "".join(map(str,S_count[i]))
    print(R)

Solution (Python):
h, w = map(int, input().split())
bom_dic = {}
for h_num in range(1, h+1):
    bom_dic_child = {}
    num_row = input()
    num_list = []
    for num in num_row:
        num_list.append(num)

    for idx, num in enumerate(num_list):
        bom_dic_child[idx+1] = num
    bom_dic[h_num] = bom_dic_child

result_dic = {}
for row_idx, row_str in bom_dic.items():
    result_dic_row = {}
    for column_idx, cell_str in row_str.items():
        if cell_str == ".":
            bom_count = 0
            for dx in range(-1,2):
                for dy in range(-1,2):
                    x = column_idx + dx
                    y = row_idx + dy
                    if w >= x > 0 and h >= y > 0:
                        terget_str = bom_dic[y][x]
                        if terget_str == "
                            bom_count += 1 
            result_dic_row[column_idx] = bom_count
        else:
            result_dic_row[column_idx] = cell_str
    result_dic[row_idx] = result_dic_row

for print_row in result_dic.values():
    for cell_str in print_row.values():
        print(cell_str, end="")
    print()

Solution (Python):
import numpy as np

h, w = map(int, input().split())

ar = [["."] * w for i in range(h)]

for i in range(h):
    st = input()
    for j, s in enumerate(st):
        ar[i][j] = s

for i in range(h):
    for j in range(w):
        if ar[i][j] == ".":
            sum = 0
            for ii in range(3):
                for jj in range(3):
                    if i+ii >h or j + jj > w:
                        continue
                    elif (i == 0 and ii ==0) or(j ==0 and jj==0):
                        continue
                    
                    if ar[i + ii-1][j + jj-1] == "
                        sum += 1
            ar[i][j] = str(sum)



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

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

for i in range(h):
    for j in range(w):
        if l[i][j] == '
        c = 0
        if i > 0:
            if j > 0 and l[i-1][j-1] == '
            if l[i-1][j] == '
            if j < w-1 and l[i-1][j+1] == '
        if j > 0 and l[i][j-1] == '
        if j < w-1 and l[i][j+1] == '
        if i < h-1:
            if j > 0 and l[i+1][j-1] == '
            if l[i+1][j] == '
            if j < w-1 and l[i+1][j+1] == '
        ans[i][j] = c
        
for row in ans:
    print(''.join(list(map(str, row))))

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


dxdy = [(-1,-1), (-1, 0), (-1, 1), (0, -1), (0, 1), (1, -1), (1, 0), (1, 1)]
ans = [['

for y in range(h):
    for x in range(w):
        if l[y][x] == '
          continue
        ct = 0
        for dx,dy in dxdy:
            if x+dx < 0 or x+dx > w-1 or y+dy < 0 or y+dy > h-1: 
              continue
            if l[y+dy][x+dx] == "
              ct += 1
            
        ans[y][x] = str(ct)
        

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

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

Solution (Python):


def main()->None:
    
    
    
    
    
    H, W = map(int, input().split())
    src = [input() for _ in range(H)]
    ans = []
    for row in src:
        ans.append(list(row))


    dxdy = [(-1,-1), (-1, 0), (-1, 1), (0, -1), (0, 1), (1, -1), (1, 0), (1, 1)]
    for y in range(H):
        for x in range(W):
            if src[y][x] == "
            c = 0
            for dx, dy in dxdy:
                
                if x+dx < 0 or x+dx > W-1 or y+dy < 0 or y+dy > H-1: continue
                if src[y+dy][x+dx] == "
            ans[y][x] = c
    
    for row in ans:
        print("".join(list(map(str, row))))


if __name__ == "__main__":
    main()