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):
def f(s):
    if s == "
        return 1
    else:
        return 0

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

for i in range(h):
    for j in range(w):
        if s[i][j] == ".":
            cnt = 0
            if 0 <= i-1:
                cnt += f(s[i-1][j])
            if i+1 < h:
                cnt += f(s[i+1][j])
            if 0 <= j-1:
                cnt += f(s[i][j-1])
            if j+1 < w:
                cnt += f(s[i][j+1])
            
            if 0 <= i-1 and 0 <= j-1:
                cnt += f(s[i-1][j-1])
            if 0 <= i-1 and j+1 < w:
                cnt += f(s[i-1][j+1])
            if i+1 < h and 0 <= j-1:
                cnt += f(s[i+1][j-1])
            if i+1 < h and j+1 < w:
                cnt += f(s[i+1][j+1])
            
            
            s[i][j] = cnt

for i in range(h):
    for j in range(w):
        print(s[i][j], end="")
    if i != h-1:
        print("\n")

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

def bombNumber(y, x):
    tmp = 0
    for dx in range(-1, 2):
        for dy in range(-1, 2):
            if x + dx == -1 or x + dx == w or y + dy == -1 or y + dy == h:
                continue

            if S[y + dy][x + dx] == "
                tmp = tmp + 1

    return(tmp)


for i in range(h):
    for j in range(w):
        if S[i][j] == ".":
            S[i][j] = str(bombNumber(i, j))

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

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

for y in range(h):
    for x in range(w):
        if g[y][x] == '.':
            cnt = 0
            for dx in range(-1, 2):
                for dy in range(-1, 2):
                    nx = x + dx
                    ny = y + dy
                    if nx < 0 or w <= nx or ny < 0 or h <= ny:
                        continue
                    if g[ny][nx] == "
                        cnt += 1
            g[y][x] = str(cnt)

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

Solution (Python):
H, W = map(int, input().split())
S = ['.' * (W + 2)] + ['.' + input() + '.' for _ in range(H)] + ['.' * (W + 2)]
ans = [[] for _ in range(H)]
N = [(1, 1), (1, 0), (1, -1), (0, 1), (0, -1), (-1, 1), (-1, 0), (-1, -1)]
for h in range(1, H + 1):
    for w in range(1, W + 1):
        if S[h][w] == '
            ans[h - 1].append('
        else:
            ans[h - 1].append(str(sum([S[h + nh][w + nw] == '
for h in range(H):
    print(''.join(ans[h]))

Solution (Python):
H, W = map(int, input().split())
S = [input() for _ in range(H)]
ans = [[''] * W for _ in range(H)]
N = [(1, 1), (1, 0), (1, -1), (0, 1), (0, -1), (-1, 1), (-1, 0), (-1, -1)]
for h in range(H):
    for w in range(W):
        if S[h][w] == '
            ans[h][w] = '
        else:
            ans[h][w] = str(sum([S[h + nh][w + nw] == "

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

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

def func1(a, th):
    tmp = [a]
    if a-1 >= 0:
        tmp.append(a-1)

    if a+1 < th:
        tmp.append(a+1)
    return tmp

def func(x,y):
    count = 0
    for i in func1(x, h):
        for j in func1(y, w):
            if map_list[i][j] == "
                count += 1
    return str(count)

for x, rows in enumerate(map_list):
    new_lines = ""
    for y, p in enumerate(rows):
        if p == ".":
            new_lines += func(x, y)
        else:
            new_lines += p
    print(new_lines)

Solution (Python):

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

dx = [0, 0, 1, -1, 1, -1, -1, 1]
dy = [1, -1, 0, 0, 1, -1, 1, -1]
ans = [['' for _ in range(w)] for _ in range(h)]
for i in range(h):
    for j in range(w):
        if s[i][j] == '
            ans[i][j] = '
        else:
            tmp = 0
            for k in range(8):
                if 0 <= i + dy[k] < h and 0 <= j + dx[k] < w:
                    if s[i + dy[k]][j + dx[k]] == '
                        tmp += 1
            ans[i][j] = str(tmp)
for i in range(h):
    print(''.join(ans[i]))

Solution (Python):
import sys 
MOD = 10 ** 9 + 7
INF = 10**11
from heapq import heapify,heappush,heappop

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

def main():
    h,w = map(int,input().split())
    grid = [list(input()) for _ in range(h)]

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

if __name__=='__main__':
    main()

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

output = [[0] * W for _ in range(H)]
for i in range(H):
    for j in range(W):
        if maze[i][j] == '
            output[i][j] = '

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

for i in range(H):
    for j in range(W):
        if output[i][j] == '
            continue
        
        for dxi, dyi in zip(dx, dy):
            nx = j + dxi
            ny = i + dyi
            if 0 <= nx < W and 0 <= ny < H: 
                if maze[ny][nx] == '
                    
                    output[i][j] += 1
                
for i in range(H):
    print(''.join(map(str, output[i])))

Solution (Python):
import numpy as np

def check_mass(i,j):
  c=0
  if i>0:
    if j>0:
      if mass[i-1][j-1]=="
        c+=1
    if mass[i-1][j]=="
        c+=1
    if j<W-1:
      if mass[i-1][j+1]=="
        c+=1
  if True:
      if j>0:
        if mass[i][j-1]=="
          c+=1
      if mass[i][j]=="
          c+=1
      if j<W-1:
        if mass[i][j+1]=="
          c+=1
  if i<H-1:
      if j>0:
        if mass[i+1][j-1]=="
          c+=1
      if mass[i+1][j]=="
          c+=1
      if j<W-1:
        if mass[i+1][j+1]=="
          c+=1
  return c

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



for i in range(H):
  for j in range(W):
    if mass[i][j] == ".":
      c=check_mass(i,j)
      mass[i][j] = str(c)

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

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

ans_field = [['']*(w+2) for i in range(h+2)]

for i in range(1,w+1):
    for j in range(1,h+1):
        if field[j-1][i-1] == '
            ans_field[j][i] = '




for i in range(1,w+1):
    for j in range(1,h+1):
        if ans_field[j][i] == '
            continue
        c = 0
        for k in range(3):
            if ans_field[j-1][i-1+k] == '
                c += 1
        
        if  ans_field[j][i-1] == '
            c += 1
        
        if  ans_field[j][i+1] == '
            c += 1
        
        for k in range(3):
            if ans_field[j+1][i-1+k] == '
                c += 1
        
        ans_field[j][i] = str(c)

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

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

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

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

for i in range(h):
    for j in range(w):
        if s[i][j] == '
            li = []
            for k in range(-1,2):
                li.append((i-1, j+k))
            li.append((i, j-1))
            li.append((i, j+1))
            for k in range(-1, 2):
                li.append((i+1, j+k))
            for k in li:
                if 0 <= k[0] < h and 0 <= k[1] < w:
                    ans[k[0]][k[1]] += 1

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):
        print(ans[i][j], end = '')
    print()

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

def count(a,b,H,W):
  if S[a][b]=="
    return "
  else:
    cnt=0
    if a>0 and b>0:
      if S[a-1][b-1]=="
    if a>0 and b+1<W:
      if S[a-1][b+1]=="
    if a+1<H and b>0:
      if S[a+1][b-1]=="
    if a+1<H and b+1<W:
      if S[a+1][b+1]=="

    if a>0:  
      if S[a-1][b]=="
    if a+1<H:
      if S[a+1][b]=="
    if b>0:
      if S[a][b-1]=="
    if b+1<W:
      if S[a][b+1]=="
    return str(cnt)
    

for j in range(H):
  for k in range(W):
    T[j][k]=count(j,k,H,W)
    
for r in range(H):
  print("".join(T[r]))

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):
import sys
from math import sqrt
from collections import Counter

input = sys.stdin.readline


def I():
    return int(input())


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


def LI():
    return list(MI())


def LIN(n: int):
    return [I() for _ in range(n)]


inf = float("inf")
mod = 10 ** 9 + 7


def main():
    h, w = MI()
    s = [["."] + list(input().rstrip()) + ["."] for _ in range(h)]

    empty = ["."] * (w + 2)
    s.append(empty)
    s.insert(0, empty)
    for i in range(1, h + 1):
        for j in range(1, w + 1):
            if s[i][j] == "
                continue
            else:
                bomb = 0
                for row in [-1, 0, 1]:
                    for col in [-1, 0, 1]:
                        if s[i + row][j + col] == "
                            bomb += 1
                s[i][j] = str(bomb)
        print("".join(s[i][1 : w + 1]))

    pass


if __name__ == "__main__":
    main()

Solution (Python):
import itertools

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

op =[]
for i in itertools.product([-1, 0, 1], repeat=2):
    op.append(i)


for i, s1 in enumerate(S):
    for j, s2 in enumerate(s1):
        if(s2 == '.'):
            counter = 0
            for o in op:
                if(i == 0 and o[0] == -1):
                    continue
                elif(i == H-1 and o[0] == 1):
                    continue
                elif(j == 0 and o[1] == -1):
                    continue
                elif(j == W-1 and o[1] == 1):
                    continue
                else:
                    if(S[i + o[0]][j + o[1]] == "
                        counter += 1
            ans[i][j] = str(counter)

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

Solution (Python):
def minesweep(s,h,w):
    
    List = [s[h-1][w-1]
            ,s[h-1][w]
            ,s[h-1][w+1]
            ,s[h][w-1]
            ,s[h][w+1]
            ,s[h+1][w-1]
            ,s[h+1][w]
            ,s[h+1][w+1]]
    
    count = 0
    
    for i in range(8):
        if List[i]=="
            count+=1
            
    print(count,end="")
    
H,W = map(int,input().split(" "))

s  = [""]*(H+2)
s[0] = "0"*(W+2)
for i in range(1,H+1):
    s[i]="0"+input()+"0"
s[-1]="0"*(W+2)

for i in range(1,H+1):
    for j in range(1,W+1):
        if s[i][j]=="
            print("
        else:
            minesweep(s,i,j)
    print("\n",end="")

Solution (Python):
h,w = map(int,input().split())
l =[]
q =[]
n=[]
l.append("."*(w+2))
for i in range(h):
  l.append("."+ input()+".")
l.append("."*(w+2))
for i in range(1,h+1):
  for t in range(1,w+1):
    if l[i][t]==".":
      q.append(l[i][t-1])
      q.append(l[i][t+1])
      q.append(l[i-1][t-1])
      q.append(l[i-1][t])
      q.append(l[i-1][t+1])
      q.append(l[i+1][t-1])
      q.append(l[i+1][t])
      q.append(l[i+1][t+1])
      z = q.count("
      n.append(z)
      q = []
    else:
      n.append("
for t in range(0,h):
  c = n[w*t:w*(t+1)]
  c = map(str,c)
  print(''.join(c))

Solution (Python):
H, W = map(int, input().split())
S = ['.' + input() + '.' for _ in range(H)]
S = ['.'*(W+2)] + S + ['.'*(W+2)]
ans = [[0]*W for _ in range(H)]

for i in range(1, H+1):
    for j in range(1, W+1):
        if S[i][j] != '
            tmp = S[i-1][j-1]=='
            tmp += S[i][j-1]=='
            tmp += S[i+1][j-1]=='
            tmp += S[i+1][j]=='
            tmp += S[i+1][j+1]=='
            tmp += S[i][j+1]=='
            tmp += S[i-1][j+1]=='
            tmp += S[i-1][j]=='
            tmp = str(tmp)
        else:
            tmp = '
        ans[i-1][j-1] = tmp
        
for i in ans:
    print(*i, sep='')

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] == ".":
            cnt = 0
            for k in range(i - 1, i + 2):
                for l in range(j - 1, j + 2):
                    if 0 <= k < h and 0 <= l < w:
                        if s[k][l] == "
                            cnt += 1
            s[i][j] = cnt

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

Solution (Python):
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, acos, atan, asin
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
def input(): return sys.stdin.readline().strip()
def STR(): return input()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
sys.setrecursionlimit(10 ** 9)
inf = sys.maxsize
mod = 10 ** 9 + 7

h, w = MAP()
s = [STR() for _ in range(h)]
dx = [1, -1, 0, 0, 1, 1, -1, -1]
dy = [0, 0, 1, -1, 1, -1, 1, -1]
for i in range(h):
    for j in range(w):
        if s[i][j] == '.':
            ans = 0
            for k in range(8):
                nx = j + dx[k]
                ny = i + dy[k]
                if 0 <= nx < w and 0 <= ny < h and s[ny][nx] == '
                    ans += 1
            print(ans, end='')
        else:
            print('
    print('')

Solution (Python):
H, W = map(int, input().split())
s = [list(input()) for _ in range(H)]
a = [[0] * (W + 2) for _ in range(H + 2)]
for i in range(H):
    for j in range(W):
        tmp_i = i + 1
        tmp_j = j + 1

        if s[i][j] == "
            a[tmp_i + 1][tmp_j] += 1
            a[tmp_i - 1][tmp_j] += 1
            a[tmp_i][tmp_j + 1] += 1
            a[tmp_i][tmp_j - 1] += 1
            a[tmp_i + 1][tmp_j + 1] += 1
            a[tmp_i + 1][tmp_j - 1] += 1
            a[tmp_i - 1][tmp_j + 1] += 1
            a[tmp_i - 1][tmp_j - 1] += 1
for i in range(H):
    for j in range(W):
        tmp_i = i + 1
        tmp_j = j + 1
        if s[i][j] == "
            a[tmp_i][tmp_j] = "
ans = []
for i in range(1,H+1):
    for j in range(1,W+1):
        ans.append(str(a[i][j]))
    print("".join(ans))
    ans = []

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] == '
            continue
        cnt = 0
        for dx in [-1, 0, 1]:
            for dy in [-1, 0, 1]:
                if not 0 <= i + dy < h or not 0 <= j + dx < w:
                    continue
                if S[i + dy][j + dx] == '
                    cnt += 1
        S[i][j] = str(cnt)
for s in S:
    print("".join(s))

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


dh = [1, 0, -1, 0, 1, -1, 1, -1]
dw = [0, 1, 0, -1, 1, 1, -1, -1]

for i in range(H):
    for j in range(W):
        if lists[i][j] == "
            continue

        count = 0

        for k in range(8):
            nh = i + dh[k]
            nw = j + dw[k]

            if nh < 0 or H <= nh or nw < 0 or W <=nw:
                continue

            if lists[nh][nw] == "
                count += 1

        lists[i][j] = str(count)

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

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

for i in range(h):
    for j in range(w):
        if s[i][j]=='
            ans[i][j]='
        else:
            cnt=0
            for ii in [-1,0,1]:
                for jj in [-1,0,1]:
                    def chk(a,b):
                        if a>=0 and a<h and b>=0 and b<w:
                            return True
                        else:
                            return False
                    if chk(i+ii,j+jj):
                        if s[i+ii][j+jj]=='
                            cnt+=1
            ans[i][j]=cnt
for i in range(h):
    print(''.join(list(map(str,ans[i]))))

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

copyS=copy.deepcopy(s)


def check(y,x,s):
	ans=0
	for i in range(3):
		for j in range(3):
			X=x+i-1
			Y=y+j-1
			if 0<=X<w and 0<=Y<h:
				if s[Y][X]=="
					ans+=1
	return ans

for i in range(h):
	for j in range(w):
		if s[i][j]==".":
			copyS[i][j]=str(check(i,j,s))

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

Solution (Python):

import sys
try:
    from typing import List
except ImportError:
    pass


def solve(H: int, W: int, S: "List[str]"):

    def isbomb(i: int, j: int):
        return 0 <= i < H and 0 <= j < W and S[i][j] == "

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

    for i, Si in enumerate(S):
        print("".join(
            c if c == "
            str(sum(
                isbomb(i + di, j + dj)
                for di, dj in ds
            ))
            for j, c in enumerate(Si)
        ))


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 = [next(tokens) for _ in range(H)]  
    solve(H, W, S)


if __name__ == '__main__':
    main()

Solution (Python):
import sys
from io import StringIO
import unittest

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

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

    def put(s, i, j):
        if 0 <= i-1:
            s[i-1][j] += 1
        if 0 <= i-1 and 0 <= j-1:
            s[i-1][j-1] += 1
        if 0 <= j-1:
            s[i][j-1] += 1
        if i+1 < h:
            s[i+1][j] += 1
        if i+1 < h and j+1 < w:
            s[i+1][j+1] += 1
        if j+1 < w:
            s[i][j+1] += 1
        if 0 <= i-1 and j+1 < w:
            s[i-1][j+1] += 1
        if 0 <= j-1 and i+1 < h:
            s[i+1][j-1] += 1
        return s
    
    for i in range(h):
        for j in range(w):
            if s[i][j] == '
                ans = put(ans, i, j)
    for i in range(h):
        for j in range(w):
            if s[i][j] == '
                ans[i][j] = '
    
    ans_str = ''
    for i in range(h):
        for j in range(w):
            ans_str += str(ans[i][j])
        ans_str += '\n'
    ans_str = ans_str[:-1]
    print(ans_str)
    



class TestClass(unittest.TestCase):
    def assertIO(self, input, output):
        stdout, stdin = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(input)
        resolve()
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, stdin
        self.assertEqual(out, output)

    def test_入力例_1(self):
        input = """3 5
.....
.
....."""
        output = """11211
1
11211"""
        self.assertIO(input, output)

    def test_入力例_2(self):
        input = """3 5



        output = """


        self.assertIO(input, output)

    def test_入力例_3(self):
        input = """6 6



.


        output = """


4


        self.assertIO(input, output)


if __name__ == "__main__":
    
    resolve()

Solution (Python):
h, w = [int(x) for x in input().split()]
matrix = [list(input()) for _ in range(h)]
surrounding_distances = (-1, 0, 1)


def count_bombs(i, j):
    for da in surrounding_distances:
        for db in surrounding_distances:
            if da == db == 0:
                
                continue
            si = i + da
            sj = j + db
            if si < 0 or si >= h or sj < 0 or sj >= w:
                continue
            if matrix[si][sj] == '
                yield 1


for i in range(h):
    for j in range(w):
        if matrix[i][j] == '.':
            matrix[i][j] = str(sum(count_bombs(i, j)))

[print(''.join(row)) for row in matrix]

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

for h in range(H):
    for w in range(W):
        count=0
        if S[h][w]=='
            continue
        
        if w!=0 and h!=0 and S[h-1][w-1]=='
            count+=1
        
        if w!=0 and S[h][w-1]=='
            count+=1
        
        if w!=0 and h!=H-1 and S[h+1][w-1]=='
            count+=1
        
        if h!=0 and S[h-1][w]=='
            count+=1
        
        if h!=0 and w!=W-1 and S[h-1][w+1]=='
            count+=1
        
        if w!=W-1 and S[h][w+1]=='
            count+=1
        
        if w!=W-1 and h!=H-1 and S[h+1][w+1]=='
            count+=1
        
        if h!=H-1 and S[h+1][w]=='
            count+=1
        
        S[h][w]=str(count)

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

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

for h in range(H):
    for w in range(W):
        count=0
        if S[h][w]=='
            continue
        
        if w==0 or h==0:
            pass
        elif S[h-1][w-1]=='
            count+=1

        
        if w==0:
            pass
        elif S[h][w-1]=='
            count+=1

        
        if w==0 or h==H-1:
            pass
        elif S[h+1][w-1]=='
            count+=1

        
        if h==0:
            pass
        elif S[h-1][w]=='
            count+=1

        
        if h==0 or w==W-1:
            pass
        elif S[h-1][w+1]=='
            count+=1

        
        if w==W-1:
            pass
        elif S[h][w+1]=='
            count+=1

        
        if w==W-1 or h==H-1:
            pass
        elif S[h+1][w+1]=='
            count+=1

        
        if h==H-1:
            pass
        elif S[h+1][w]=='
            count+=1
        
        S[h][w]=str(count)

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

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]=='
      continue
    cnt = 0
    for x in [-1, 0, 1]:
      for y in [-1, 0, 1]:
        if not 0 <= i+y < h or not 0 <= j+x < w:
          continue
        if S[i+y][j+x]=='
          cnt += 1
    S[i][j] = str(cnt)
for s in S:
  print(''.join(s))

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

output_rows = [['

for i in range(H):
    for j in range(W):
        if rows[i][j] == '
            continue
        else:
            total = 0
            for dy in range(-1, 1 + 1):
                for dx in range(-1, 1 + 1):

                    y, x = i + dy, j + dx
                    if x < 0 or x > W - 1 or y < 0 or y > H - 1:
                        continue
                    else:
                        if rows[y][x] == '
                            total += 1
            output_rows[i][j] = str(total)

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

Solution (Python):


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

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



field=[]
for _ in range(H):
  field.append(list(input()))

for y in range(H):
  for x in range(W):
    if field[y][x]=="
      continue
    count=0
    for k in range(8):
      tmp_y=y+dy[k]
      tmp_x=x+dx[k]

      if tmp_y<0 or tmp_y>=H or tmp_x<0 or tmp_x>=W:
        continue
      if field[tmp_y][tmp_x]=="
        count+=1
    field[y][x]=str(count)



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

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]=='
            continue
        cnt=0
        for k in [-1,0,1]:
            if i+k<0 or h<=i+k:
                continue
            for l in [-1,0,1]:
                if j+l<0 or j+l>=w:
                    continue
                cnt+=1*(s[i+k][j+l]=='
        s[i][j]=str(cnt)
for i in range(h):
    print("".join(s[i]))

Solution (Python):
H, W = map(int, input().split(' '))
masu = [input() for i in range(H)]
dx = [1, 1, 0, -1, -1, -1, 0, 1]
dy = [0, -1, -1, -1, 0, 1, 1, 1]
ans = []
for i in range(H):
    a = []
    for j in range(W):
        if (masu[i][j] == '
            a.append('
            continue
        cnt = 0
        for d in range(len(dx)):
            x = i + dx[d]
            y = j + dy[d]
            if (x < 0 or H <= x):
                continue
            if (y < 0 or W <= y):
                continue
            if (masu[x][y] == '
                cnt += 1
        a.append(str(cnt))
    ans.append(a)
for b in ans:
    print("".join(b))

Solution (Python):
h,w = map(int,input().split())
s = [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 s[i][j] == "
                           for k in range(max(0,i-1),min(h,(i+1)+1)):
                                    for l in range(max(0,j-1),min(w,(j+1)+1)):
                                             ans[k][l] += 1
for i in range(h):
         for j in range(w):
                  if s[i][j] == "
                           ans [i][j] = "
for i in range(h):
         answer = list(map(str,ans[i]))
         answer=''.join(answer)
         print(answer)

Solution (Python):
tate, yoko = map(int,input().split())
map_list = []
for t in range(tate):
    map_list.append(list(input()))

new_map_list = [[0] * (yoko + 1) for i in range(tate + 1)]

def outofrange_check(t, y):
    if t >= tate or y >= yoko:
        return True


for t in range(tate):
    for y in range(yoko):
        if map_list[t][y] == '
            for t_2 in range(t-1, t+2):
                for y_2 in range(y-1, y+2):
                    new_map_list[t_2][y_2] += 1


for t in range(tate):
    for y in range(yoko):
        if map_list[t][y] == '
            new_map_list[t][y] = '

for i in range(tate):
    for j in range(yoko):
        print(new_map_list[i][j], end='')
    print()

Solution (Python):
h, n = (int(x) for x in input().split())
dp = [[0 for i in range(n+1)] for j in range(h+1)]
for i in range(h):
    t=input()
    t=list(t)
    for j in range(n):
        if t[j] == '
            dp[i][j] = '

for i in range(h):
    for j in range(n):
        if dp[i][j] == '
            if dp[i-1][j-1] != '
                dp[i-1][j-1]+=1
            if dp[i][j-1] != '
                dp[i][j-1]+=1
            if dp[i+1][j-1] != '
                dp[i+1][j-1]+=1
            if dp[i-1][j] != '
                dp[i-1][j]+=1
            if dp[i-1][j+1] != '
                dp[i-1][j+1]+=1
            if dp[i+1][j] != '
                dp[i+1][j]+=1
            if dp[i][j+1] != '
                dp[i][j+1]+=1
            if dp[i+1][j+1] != '
                dp[i+1][j+1]+=1

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

Solution (Python):
def masucnt(masu,i,j):
  if i<0 or j<0:
    return 0
  s="."
  try:
    s=masu[i][j]
  except:
    s=""
  return 1 if s=="

h,w=map(int,input().split())
masu=[""]*h

for i in range(h):
  masu[i]=input().strip()

for i in range(h):
  for j in range(len(masu[i])):
    n=0
    s=masu[i][j]
    if s=="
      if j==(len(masu[i])-1):
        print("
      else:
        print("
    else:
      n+=masucnt(masu,i-1,j-1)
      n+=masucnt(masu,i-1,j)
      n+=masucnt(masu,i-1,j+1)
      n+=masucnt(masu,i,j-1)
      n+=masucnt(masu,i,j+1)
      n+=masucnt(masu,i+1,j-1)
      n+=masucnt(masu,i+1,j)
      n+=masucnt(masu,i+1,j+1)
      if j==(len(masu[i])-1):
        print(n)
      else:
        print(n,end="")

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

mine = []
for i in range(h):
    mine.append(list(input().replace(".","0")))

for i in range(h):
    for j in range(w):
        if mine[i][j] == "
            for k in range(i-1,i+2):
                if k > (h-1) or k < 0:
                    continue
                for l in range(j-1,j+2):
                    if l > (w-1) or l < 0:
                        continue
                    elif mine[i][j] == mine[k][l]:
                        continue
                    mine[k][l] = int(mine[k][l])
                    mine[k][l] += 1
for i in mine:
    print("".join(map(str,i)))