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):
D = [-1,0,1]

def count(y,x):
    res = 0
    for dy in D:
        for dx in D:
            ny = y+dy
            nx = x+dx
            if 0 <= ny < H and 0 <= nx < W and S[ny][nx] == "
                res += 1
    return res
    
[H,W] = list(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] != "
            cnt = count(i,j)
            S[i] = S[i][:j] + str(cnt) + S[i][j+1:]
for i in range(H):
    print(S[i])

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

dummy = ''
for i in range(W):
    dummy += '.'

extended = [dummy] + grid + [dummy]
extended = ['.'+v+'.' for v in extended]

result = []
for h in range(1, H+1):
    row = []
    for w in range(1, W+1):
        if extended[h][w] == '
            row.append('
        else:
            count = 0
            if extended[h-1][w-1] == '
            if extended[h-1][w] == '
            if extended[h-1][w+1] == '
            if extended[h][w-1] == '
            if extended[h][w+1] == '
            if extended[h+1][w-1] == '
            if extended[h+1][w] == '
            if extended[h+1][w+1] == '
            row.append(count)
    result.append(row)

out = '\n'.join([''.join(map(str,r)) for r in result])
print(out)

Solution (Python):

hw_list = list(map(int, input().split()))
h_max = hw_list[0]
w_max = hw_list[1]

s_list = []

for i in range(h_max):
    work_list = input()
    s_list.append(work_list)
answer_list = []

for h in range(h_max):
    work_list = s_list[h]
    work_answer = []
    for w, target in enumerate(work_list):
        if target == '.':
            tonari_list = [(w-1, h-1), (w, h-1), (w+1, h-1),
                           (w-1, h), (w+1, h),
                           (w-1, h+1), (w, h+1), (w+1, h+1)]
            count = 0
            for x, y in tonari_list:
                if 0 <= x < w_max and 0 <= y < h_max:
                    if s_list[y][x] == '
                        count += 1
                else:
                    continue
            work_answer.append(str(count))
        else:
            work_answer.append('
    answer_list.append(work_answer)

for a_list in answer_list:
    print(''.join(a_list))

Solution (Python):
import copy

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

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 S[i][j] == ".":
            cnt = 0
            for dir in range(8):
                if 0 <= i + dy[dir] < H and 0 <= j + dx[dir] < W:
                    cnt += (S[i + dy[dir]][j + dx[dir]] == "
                
            ans[i][j] = str(cnt)
for h in range(H):
    print("".join(ans[h]))

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

def count(ch):
  return 1 if ch == '

for i in range(H):
  for j in range(W):
    num = 0
    if mine[i][j] == '
      continue
    else:
      for k in range(-1,2):
        for l in range(-1,2):
          if 0<=i+k<H and 0<=j+l<W:
            num += (1 if mine[i+k][j+l]=='
      mine[i][j] = num
      
for i in range(H):
  for j in range(W):
    print(mine[i][j],end='')
  print()

Solution (Python):
H,W= map(int,input().split())
emp_H= [['.']*(W+2)]
mine = [['.']+list(input())+ ['.'] for i in range(H)]
mine = emp_H + mine + emp_H

def count(ch):
  return 1 if ch == '

for i in range(1,H+1):
  for j in range(1,W+1):
    if mine[i][j] == '
      continue
    else:
      mine[i][j] = count(mine[i-1][j-1])+count(mine[i][j-1])+count(mine[i+1][j-1])+count(mine[i-1][j])+count(mine[i][j])+count(mine[i+1][j])+count(mine[i-1][j+1])+count(mine[i][j+1])+count(mine[i+1][j+1])
for i in range(1,H+1):
  for j in range(1,W+1):
    print(mine[i][j],end='')
  print()

Solution (Python):
H,W=map(int,input().split())
Mine=[]
for i in range(H):
  Mine.append(list(input()))
for i in range(H):
  answer=[]
  for j in range(W):
    if Mine[i][j]=='
      answer.append('
    else:
      count=0
      if i!=0:
        if j!=0:
          if Mine[i-1][j-1]=='
            count+=1
        if j!=W-1:
          if Mine[i-1][j+1]=='
            count+=1
        if Mine[i-1][j]=='
          count+=1
      if i!=H-1:
        if j!=0:
          if Mine[i+1][j-1]=='
            count+=1
        if j!=W-1:
          if Mine[i+1][j+1]=='
            count+=1
        if Mine[i+1][j]=='
          count+=1
      if j!=0:
        if Mine[i][j-1]=='
          count+=1
      if j!=W-1:
        if Mine[i][j+1]=='
          count+=1
      answer.append(str(count))
  print("".join(answer))

Solution (Python):
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
    b = 0
    if i > 0 and S[i-1][j] == '
      b += 1
    if i > 0 and j < W-1 and S[i-1][j+1] == '
      b += 1
    if j < W-1 and S[i][j+1] == '
      b += 1
    if i < H-1 and j < W-1 and S[i+1][j+1] == '
      b += 1
    if i < H-1 and S[i+1][j] == '
      b += 1
    if i < H-1 and j > 0 and S[i+1][j-1] == '
      b += 1
    if j > 0 and S[i][j-1] == '
      b += 1
    if i > 0 and j > 0 and S[i-1][j-1] == '
      b += 1
    
    if j == 0:
      S[i] = str(b) + S[i][j+1:]
    elif j == W-1:
      S[i] = S[i][:j] + str(b)
    else:
      S[i] = S[i][:j] + str(b) + S[i][j+1:]
    

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

Solution (Python):
def calc(x, y):
    x1 = x - 1
    x2 = x - 1
    x3 = x - 1
    x4 = x
    x5 = x
    x6 = x + 1
    x7 = x + 1
    x8 = x + 1
    y1 = y - 1
    y2 = y
    y3 = y + 1
    y4 = y - 1
    y5 = y + 1
    y6 = y - 1
    y7 = y
    y8 = y + 1
    return x1, y1, x2, y2, x3, y3, x4, y4, x5, y5, x6, y6, x7, y7, x8, y8

h, w = map(int, input().split())
s = []
for _ in range(h):
    s.append(list(input()))
ans = []
for i in range(h * w):
    y = i % w
    x = i // w
    x1, y1, x2, y2, x3, y3, x4, y4, x5, y5, x6, y6, x7, y7, x8, y8 = calc(x, y)
    s_list = [[x1, y1], [x2, y2], [x3, y3], [x4, y4],
              [x5, y5], [x6, y6], [x7, y7], [x8, y8]]
    if s[x][y] == ".":
        c = 0
        for xx, yy in s_list:
            if ((xx >= 0) & (yy >= 0)) & ((xx < h) & (yy < w)):
                if s[xx][yy] == "
                    c += 1
        ans.append(str(c))
    else:
        ans.append("
sss = ""
for i, anss in enumerate(ans):
    if (i % w) == w - 1:
        sss += anss + "\n"
    else:
        sss += anss
print(sss[:-1])

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 = [int(c) for c in input().split()]

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

directions = [[-1, 0], [-1, 1], [0, 1], [1, 1], [1, 0], [1, -1], [0, -1], [-1, -1]]


def find_bom(board, row_i, i, direction):
    if -1 < row_i + direction[0] < h and -1 < i + direction[1] < w:
        if board[row_i+direction[0]][i+direction[1]] == "
            return 1
        else:
            return 0
    else:
        return 0


new_board = []
for i in range(h):
    new_row = ''
    for j in range(w):
        if board[i][j] != "
            bom_count = 0
            for d in directions:
                bom_count += find_bom(board, i, j, d)
            new_row += str(bom_count)
        else:
            new_row += '
    new_board.append(new_row)

print('\n'.join(new_board))

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

Solution (Python):
def checkmine(i,j):
    count=0
    for a in range(-1,2):
        for b in range(-1,2):
            if 0<=a+i<n and 0<=b+j<m:
                if field[a+i][b+j]=="
    return count

n,m=map(int,input().split())
field=[]
for i in range(n):field.append(list(input()))
for i in range(n):
    for j in range(m):
        if field[i][j]==".":field[i][j]=checkmine(i,j)
for i in field:print(''.join(map(str,i)))

Solution (Python):
h,w = map(int,input().split())
lis = [input() for x in range(h)]
ans_lis = [[0 for x in range(w)] for y in range(h)]

def cnt(y,x):
    if y >= h or x >= w or y < 0 or x < 0:
        return 0
    if lis[y][x] == '
        return 1
    return 0

for y in range(h):
    for x in range(w):
        ans = 0
        if lis[y][x] == '.':
            ans += cnt(y+1,x-1)
            ans += cnt(y+1,x)
            ans += cnt(y+1,x+1)
            ans += cnt(y,x-1)
            ans += cnt(y,x+1)
            ans += cnt(y-1,x-1)
            ans += cnt(y-1,x)
            ans += cnt(y-1,x+1)
            ans_lis[y][x] = str(ans)
        else:
            ans_lis[y][x] = '
for x in range(h):
    print(''.join(ans_lis[x]))

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

for i in range(H):
    for j in range(W):
        if S[i][j] == '.':
            ct = 0
            for k in range(8):
                x = j + [0, 1, 0, -1, 1, 1, -1, -1][k]
                y = i + [1, 0, -1, 0, 1, -1, 1, -1][k]
                if 0 <= x < W and 0 <= y < H:
                    if S[y][x] == '
                        ct += 1
            S[i][j] = str(ct)

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

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

for i in range(1, H+1):
    for j in range(1, W+1):
        if exS[i][j]=='
            continue
        else:
            exS[i][j] = str((exS[i][j-1]+exS[i][j+1]\
                      +exS[i-1][j-1]+exS[i-1][j+1]+exS[i-1][j]\
                      +exS[i+1][j-1]+exS[i+1][j+1]+exS[i+1][j]).count('

def show(arr):
    text = ''
    for i in range(1, len(arr)-1):
        if i == 0:
            continue
        elif i == len(arr)-2:
            text += ''.join(arr[i][1:-1])
        else:
            text += ''.join(arr[i][1:-1]) + '\n'
    return text

print(show(exS))

Solution (Python):
line = input().rstrip().split(' ')
h = int(line[0])
w = int(line[1])


glid = []

for i in range(h):
    glid.append(list(input()))

for y in range(h):
    for x in range(w):
        
        if glid[y][x] == '
            continue
        
        num = 0
        for dx,dy in ((-1,-1), (0,-1), (1,-1), (-1,0), (1,0), (-1,1), (0,1), (1,1)):
            ny = y + dy
            nx = x + dx
            if ny < 0 or nx < 0 or ny >= h or nx >= w:
                continue
            if glid[ny][nx] == '
                num += 1
        glid[y][x] = num


for y in range(h):
    line = ''
    for x in range(w):
        line += str(glid[y][x])
    print(line)

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



glid = [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 glid[i][j] == '
            ans[i][j] = '
            continue
            
        count = 0
        for k in range(-1,2):
            if i+k < 0 or i+k >= h:
                continue
            for l in range(-1,2):
                if j+l < 0 or j+l >= w:
                    continue
                
                if glid[i+k][j+l] == '
                    count += 1
        ans[i][j] = count

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

Solution (Python):
H, W = map(int, input().split())
S = [["."] * (W + 2)] + [["."] + list(input()) + ["."] for _ in range(H)] + [["."] * (W + 2)]
for h in range(1, H + 1):
    for w in range(1, W + 1):
        if S[h][w] == "
            continue
        count = 0
        for dh in range(-1, 2):
            for dw in range(-1, 2):
                if S[h + dh][w + dw] == "
                    count += 1
        S[h][w] = str(count)
    print("".join(S[h][1:W+1]))

Solution (Python):
def count_bombs(S, h, w):
    count = 0
    for i in range(h-1, h+2):
        for j in range(w-1, w+2):
            if i < 0 or j < 0 or i >= len(S) or j >= len(S[0]):
                continue
            if S[i][j] == "
                count += 1
    return count


H, W = map(int, input().split())
S = [input() for _ in range(H)]
for h in range(H):
    for w in range(W):
        if S[h][w] == "
            print("
            continue
        print(count_bombs(S, h, w), end="")
    print()

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

def count(y, x):
	directions = ((1, 0), (1, 1), (0, 1), (-1, 1), (-1, 0), (-1, -1), (0, -1), (1, -1))
	c = 0
	for d in directions:
		px, py = x + d[0], y + d[1]
		if px < 0 or W <= px or py < 0 or H <= py:
			continue
		if stage[py][px] == "
			c += 1
	return c

for y in range(H):
	for x in range(W):
		if stage[y][x] == ".":
			stage[y][x] = count(y, x)

for s in stage:
	print(*s, sep="")

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

from itertools import product

for h, w in product(range(H), range(W)):
  if SS[h][w] != '
    bom = 0
    for dh, dw in product([-1, 0, 1], repeat=2):
      bh, bw = h+dh, w+dw
      if bh < 0 or H-1 < bh:
        continue
      if bw < 0 or W-1 < bw:
        continue
      if SS[bh][bw] == '
        bom += 1
    SS[h][w] = str(bom)
print(*[''.join(s) for s in SS], sep='\n')

Solution (Python):
H,W=map(int,input().split())
S=[]
for i in range(H):
    S.append(list(input()))
for h in range(H):
    for w in range(W):
        
        if S[h][w]=='
            continue
        x1 = max(0,w-1)
        x2 = min(W,w+2)
        y1 = max(0,h-1)
        y2 = min(H,h+2)
        cnt=0
        for h2 in range(y1,y2):
            for w2 in range(x1,x2):
                if S[h2][w2]=='
                    cnt+=1
        
        S[h][w]=str(cnt)
for i in range(H):
    print(''.join(S[i]))

Solution (Python):
h,w=map(int,input().split())
b=["n"*(w+2)]
for i in range(h):b.append("n"+input()+"n")
b.append(b[0])
def num(i,j):
  a=0
  def same(i,j):
    global b
    if b[i][j]=="
    return 0
  a+=same(i-1,j-1)
  a+=same(i-1,j)
  a+=same(i-1,j+1)
  a+=same(i,j-1)
  a+=same(i,j+1)
  a+=same(i+1,j-1)
  a+=same(i+1,j)
  a+=same(i+1,j+1)
  return a
for i in range(len(b)):
  if 0<i<h+1:
    for j in range(len(b[i])):
      if b[i][j]==".":b[i]=b[i][:j]+str(num(i,j))+b[i][j+1:]
for i in range(1,h+1):print(b[i][1:w+1])

Solution (Python):
h,w=map(int,input().split())
T=["."+input()+"." for i in range(h)]
S="."*(w+2)
T.insert(0,S)
T.append(S)
Ans=["" for i in range(h)]
for i in range(1,h+1):
  for j in range(1,w+1):
    count=0
    if T[i][j]=="
      A="
    elif T[i][j]==".":
      for m in range(3):
        for n in range(3):
          if T[i-1+m][j-1+n]=="
            count+=1
          A=str(count)
    Ans[i-1]+=A
print(*Ans,sep="\n")

Solution (Python):
h,w=map(int,input().split())
S=[list(input()) for _ in range(h)]
dx=[-1,0,1]
dy=[-1,0,1]
for i in range(h):
    for j in range(w):
        count=0
        if S[i][j]=='
            continue
        else:
            for l in dx:
                for m in dy:
                    if i+l<0 or i+l>h-1 or j+m<0 or j+m>w-1:
                        continue
                    if S[i+l][j+m]=='
                        count+=1
        S[i][j]=str(count)
for i in S:
    k=''.join(i)
    print(k)

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

ans = [0] * H
for i in range(H):
    ans[i] = [0] * W

for i in range(1, H + 1):
    for j in range(1, W + 1):
        if S[i][j] == '.':
            ans[i - 1][j - 1] += S[i - 1][j - 1 : j + 2].count('
            ans[i - 1][j - 1] += S[i][j - 1 : j + 2].count('
            ans[i - 1][j - 1] += S[i + 1][j - 1 : j + 2].count('
        else:
            ans[i - 1][j - 1] = '

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

Solution (Python):
H, W = [int(i) for i in input().split()]
minelist = [list(input()) for i in range(H)]
dx = [-1, 0 , 1]
dy = [-1, 0 , 1]
for i in range(H):
    for j in range(W):
        count = 0
        if minelist[i][j] == "
            continue
        if minelist[i][j] == ".":
            for l in dx:
                for m in dy:
                    if (i + l < 0) or (i + l > H -1 ) or (j + m < 0) or (j + m > W -1):
                        continue
                    if minelist[i + l][j + m] == "
                        count += 1
        minelist[i][j] = str(count)
for i in minelist:
    k = "".join(i)
    print(k)

Solution (Python):
H, W = map(int, input().split())
S = ['.' + input() + '.' for i in range(H)]
S.insert(0, '.' * (W + 2))
S.insert(len(S), '.' * (W + 2))
def mine(x):
    if x == '
        count.append(1)
for i in range(1, H+1):
    tmp = ''
    for j in range(1, W+1):
        if S[i][j] == '
            tmp += '
        else:
            count = []
            mine(S[i-1][j-1]); mine(S[i-1][j]); mine(S[i-1][j+1])
            mine(S[i][j-1]);                    mine(S[i][j+1])
            mine(S[i+1][j-1]); mine(S[i+1][j]); mine(S[i+1][j+1])
            tmp += str(sum(count))
    print(tmp)

Solution (Python):
def count(i,j):
  count=0
  if list[i-1][j-1]=='
    count += 1
  if list[i-1][j]=='
    count += 1
  if list[i-1][j+1]=='
    count += 1
  if list[i][j-1]=='
    count += 1
  if list[i][j+1]=='
    count += 1
  if list[i+1][j-1]=='
    count += 1
  if list[i+1][j]=='
    count += 1
  if list[i+1][j+1]=='
    count += 1
  return count

h,w=map(int, input().split())
s_init='0'*(w+2)
list=[]
list.append(s_init)
for i in range(h):
  s=str(input())
  list.append('0'+s+'0')
list.append(s_init)
for i in range(1,h+1):
  for j in range(1,w+1):
    if list[i][j]=='.':
      list[i]=list[i][:j]+str(count(i,j))+list[i][j+1:]
for i in range(h):
  print(list[i+1][1:-1])

Solution (Python):
import math
import functools
import itertools
import numpy as np
import sys
MAX_INT = int(10e10)
MIN_INT = -MAX_INT
mod = 1000000007
sys.setrecursionlimit(1000000)
def IL(): return list(map(int,input().split()))
def SL(): return input().split()
def I(): return int(sys.stdin.readline())
def S(): return input()

def judge(n,m):
    if (0 <= n < H) and (0 <= m < W) and s[n][m]=="
        return True

def cnt(a,b):
    l = [-1,0,1]
    num = 0
    for i in l:
        for j in l:
            if i == j == 0:
                continue
            else:
                if judge(a+i,b+j):
                    num += 1
    return num

H,W = IL()
s = [[i for i in S()] for i in range(H)]
ans = [["

for i in range(H):
    for j in range(W):
        if s[i][j] == ".":
            ans[i][j] = cnt(i,j)

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

Solution (Python):
n,m=map(int,input().split())

hoge=[]
hoge.append(["."]*(m+2))
for i in range(n):
  hoge.append(["."]+list(input())+["."])
hoge.append(["."]*(m+2))

for i in range(n):
  for j in range(m):
    a=i+1
    b=j+1
    if(hoge[a][b]=="
      print("
    else:
      ans=0
      if(hoge[a-1][b-1]=="
        ans+=1
      if(hoge[a-1][b]=="
        ans+=1
      if(hoge[a-1][b+1]=="
        ans+=1
      if(hoge[a][b-1]=="
        ans+=1
      if(hoge[a][b+1]=="
        ans+=1
      if(hoge[a+1][b-1]=="
        ans+=1
      if(hoge[a+1][b]=="
        ans+=1
      if(hoge[a+1][b+1]=="
        ans+=1
      print(ans,end="")
  print("")

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([t[max(0,j-1):min(w,j+2)].count("
    print(l)

Solution (Python):
h, w = [int(item) for item in input().split()]
field = ["%" * (w+2)] + ["%" + input() + "%" for _ in range(h)] + ["%" * (w+2)]
for i in range(1,h+1):
    for j in range(1,w+1):
        count = 0
        if field[i][j] == ".":
            for k in [-1,0,1]:
                for l in [-1,0,1]:
                    if field[i+k][j+l] == "
                        count += 1
            field[i] = field[i][:j] + str(count) + field[i][j+1:]

for line in field[1:h+1]:
    for c in line:
        if c !="%":
            print(c, end="")
    print("")

Solution (Python):
from sys import exit
H,W = [int(n) for n in input().split()]
S = [0]*H
dxy = [[1,0],[0,1],[0,-1],[-1,0],[1,1],[1,-1],[-1,1],[-1,-1]]
for i in range(H):
    S[i] = list(str(input()))

for i in range(H):
    for j in range(W):
        if S[i][j] == ".":
            cnt = 0
            for d in dxy:
                if(0<= i+d[0] < H and 0<= j+d[1] < W and S[i+d[0]][j+d[1]] == "
                    cnt+=1
            S[i][j] = cnt

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

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

for i in range(h):
    for j in range(w):
        if s[i][j] == '.':
            count = 0
            for i_ in range(i-1, i+2):
                for j_ in range(j-1, j+2):
                    if 0<= i_ <h and 0<= j_ <w:
                        if s[i_][j_] == '
                            count += 1
            s[i]=s[i][:j]+str(count)+s[i][j+1:]

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

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

T = [[0 for j in range(W)] for i in range(H)]
for i in range(H):
    for j, sj in enumerate(S[i]):
        if sj != '.':
            T[i][j] = sj
            continue

        count = 0
        if 0 <= i-1 and 0 <= j-1 and S[i-1][j-1] == '
            count += 1
        if 0 <= i-1 and S[i-1][j] == '
            count += 1
        if 0 <= i-1 and j+1 < W and S[i-1][j+1] == '
            count += 1
        if 0 <= j-1 and S[i][j-1] == '
            count += 1
        if j+1 < W and S[i][j+1] == '
            count += 1
        if i+1 < H and 0 <= j-1 and S[i+1][j-1] == '
            count += 1
        if i+1 < H and S[i+1][j] == '
            count += 1
        if i+1 < H and j+1 < W and S[i+1][j+1] == '
            count += 1
            
        T[i][j] = count

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

Solution (Python):
h, w = [int(n) for n in input().split()]

B = [['.'] * (w + 2)]
for i in range(h):
    s = ['.'] + list(input()) + ['.']
    B.append(s)
B.append(['.'] * (w + 2))

for i in range(1, h+1):
    for j in range(1, w+1):

        if B[i][j] == '
            continue
        else:
            
            B[i][j] = str((B[i-1][j-1:j+2] + [B[i][j-1]] + [B[i][j+1]] + B[i+1][j-1:j+2]) .count('



for i in range(1, h+1):
    print(''.join(B[i][1:w+1]))

Solution (Python):
H,W = map(int,input().split())
Ss = [["."]+[j for j in input()]+["."] for i in range(H)]
Ss.insert(0,["."]*len(Ss[0]))
Ss.append(["."]*len(Ss[0]))
ans = []
for i in range(1,len(Ss)-1):
    nums = []
    for j in range(1,len(Ss[i])-1):
        num = 0
        if Ss[i][j]==".":
            if Ss[i+1][j]=="
                num+=1
            if Ss[i+1][j+1]=="
                num+=1
            if Ss[i+1][j-1]=="
                num+=1
            if Ss[i-1][j]=="
                num+=1
            if Ss[i-1][j+1]=="
                num+=1
            if Ss[i-1][j-1]=="
                num+=1
            if Ss[i][j+1]=="
                num+=1
            if Ss[i][j-1]=="
                num+=1
            nums.append(str(num))
        else:
            nums.append("
    print("".join(nums))

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

def count(x,y):
  p = 0
  if x >= 1 and y >= 1 and S[x-1][y-1] == "
    p += 1
  if x >= 1 and S[x-1][y] == "
    p += 1
  if x >= 1 and y <= w - 2 and S[x-1][y+1] == "
    p += 1
  if y >= 1 and S[x][y-1] == "
    p += 1
  if y <= w - 2 and S[x][y+1] == "
    p += 1
  if x <= h - 2 and y >= 1 and S[x+1][y-1] == "
    p += 1
  if x <= h - 2 and S[x+1][y] == "
    p += 1
  if x <= h - 2 and y <= w - 2 and S[x+1][y+1] == "
    p += 1
  p = str(p)
  return(p)

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

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

glid = [0] * ( h+ 2)

h_index = [-1, -1, -1, 0, 0, 1, 1, 1]
w_index = [-1, 0, 1, -1, 1, -1, 0, 1]

glid[0]=[0] * ( w + 2 )
glid[h + 1 ] = [0] * ( w + 2 )

for i in range(h):
	temp1 = [0]
	temp2 = list(input())
	temp1.extend(temp2)
	temp1.append(0)
	glid[ i + 1 ] = temp1



for j in range(h):
	for k in range(w):
		if glid[ j + 1 ][ k + 1 ] == "
			continue
		else:
			cnt = 0
			for n in range(8):
				if glid[ j  + 1 + h_index[ n ] ][ k + 1 + w_index[ n ]] == "
					cnt += 1
			glid[ j + 1 ][ k +1 ] = str(cnt)

for h_print in range ( h ):
	for w_print in range ( w ):
		print(glid[h_print + 1][w_print + 1], end="")
	print()

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

for i in range(1, H + 1):
    for j in range(1, W + 1):
        if S[i][j] == '.':
            S[i][j] = str([S[i - 1][j - 1], S[i - 1][j], S[i - 1][j + 1], S[i][j - 1], S[i][j + 1], S[i + 1][j - 1], S[i + 1][j], S[i + 1][j + 1]].count('

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

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

H, W = map(int, input().split())
grid = [list(input().replace("\n","")) for _ in range(H)]
dir4 = ((0, 1), (0, -1), (1, 0), (-1, 0))
dir8 = ((0, 1), (0, -1), (1, 0), (-1, 0), (-1, -1), (1, -1), (-1, 1), (1, 1))

for i in range(H):
    for j in range(W):
        if grid[i][j] != ".": continue
        cnt = 0
        for d in dir8:
            nx = i + d[0]
            ny = j + d[1]
            if not (0 <= nx < H): continue
            if not (0 <= ny < W): continue
            if grid[nx][ny] == "
        grid[i][j] = str(cnt)
    print(*grid[i], sep="")

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:
      x = 0
      for dh in [-1, 0, 1]:
        for dw in [-1, 0, 1]:
          if 0 <= i + dh < h and 0 <= j + dw < w and s[i + dh][j + dw] == "
            x += 1
      l += str(x)
  print(l)

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

for i in range(h):
  for j in range(w):
    if s[i][j]=='
      continue
    cnt=0
    for dx in [-1,0,1]:
      for dy in [-1,0,1]:
        if not(0<=dx+i<=h-1 and 0<=dy+j<=w-1):
          continue
        if s[i+dx][j+dy]=='
          cnt+=1
    s[i][j]=str(cnt)
for m in s:
  print(''.join(m))

Solution (Python):
import numpy as np

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

ans = np.array([['0']*W for i in range(H)])
for i in range(H):
    for j in range(W):
        if S[i, j] == '
            ans[i, j] = '
        else:
            ans[i, j] = np.sum(S[max(0, i-1):min(i+2, H), max(0, j-1):min(j+2, W)] == '

for x in ans:
    print(''.join(list(x)))

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

H, W = map(int, input().split())
grid = [list(input()) for _ in range(H)]
dir4 = ((0, 1), (0, -1), (1, 0), (-1, 0))
dir8 = ((0, 1), (0, -1), (1, 0), (-1, 0), (-1, -1), (1, 1), (-1, 1), (1, -1))

for i in range(H):
    for j in range(W):
        if grid[i][j] != "
        for d in dir8:
            nx = i + d[0]
            ny = j + d[1]
            if not (0 <= nx < H): continue
            if not (0 <= ny < W): continue
            if grid[nx][ny] == ".":
                grid[nx][ny] = "1"
            elif grid[nx][ny] == "
                pass
            else:
                grid[nx][ny] = str(int(grid[nx][ny]) + 1)

for s in grid:
    print("".join(s).replace(".", "0").replace("\n",""))

Solution (Python):
h,w=map(int,input().split())
l=[list(input()) for i in range(h)]
dx=[1,1,1,0,0,-1,-1,-1]
dy=[1,0,-1,1,-1,1,0,-1]
for i in range(h):
  for j in range(w):
    if l[i][j]=='.':
      count=0
      for k in range(8):
        nx=i+dx[k]
        ny=j+dy[k]
        if nx<0 or ny<0 or nx>=h or ny>=w:
          continue
        else:
          if l[nx][ny]=='
            count+=1
      l[i][j]=str(count) 
for i in l:
  print(''.join(i))

Solution (Python):
h,w=map(int,input().split())
l=[[0]*(w+2)]
for i in range(h):
  a=list(input())
  a.insert(0,0)
  a.append(0)
  l.append(a)
l.append([0]*(w+2))
pointx=[-1,0,1]
pointy=[-1,0,1]
for i in range(1,h+1):
  for j in range(1,w+1):
    if l[i][j]=='.':
      count=0
      for k in pointx:
        for m in pointy:
          if l[i+k][j+m]=='
            count+=1
      l[i][j]=str(count)

del l[0]
del l[h]
for i in range(h):
  l[i]=l[i][1:w+1]
for i in l:
  print(''.join(i))