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())
s=[]
dx=[1,0,-1,0,1,-1,-1,1]
dy=[0,1,0,-1,1,1,-1,-1]
for i in range(h):
    s.append(list(input()))
for y in range(h):
    for x in range(w):
        if(s[y][x]=="."):
            num = 0
            for d in range(8):
                nx = x+dx[d]
                ny = y+dy[d]
                if(nx<0 or w<=nx):
                    continue
                if(ny<0 or h<=ny):
                    continue
                if(s[ny][nx] == "
                    num+=1
            s[y][x] = str(num)
for i in range(h):
    print("".join(s[i]))

Solution (Python):

H,W=(int(i) for i in input().split())
Field=[]
for i in range(H):
    Field.append(list(input()))
for i in range(0,H):
    for j in range(W):
        if Field[i][j]=="
        cnt=0
        for x in range(-1,2):
            for y in range(-1,2):
                if (x==0 and y==0) or 0>x+i or 0>y+j or x+i>=H or y+j>=W:
                    continue
                if Field[i+x][j+y]=="
                    cnt+=1
        Field[i][j]=str(cnt)

for i in range(H):
    Out=Field[i][0]
    for j in range(1,W):
        Out+=Field[i][j]
    print(Out)

Solution (Python):








H, W = map(int, input().split())
S = ''
for _ in range(H):
    S += input()
    

def cell_of(x, y):
    return S[W*x + y]

def neighbor_indice_of(x, y):
    return [
        (x+m, y+n)
        for m in range(-1, 2)
        for n in range(-1, 2)
        if 0 <= x+m < H and 0 <= y+n < W
    ]

def neighbors_of(x, y):
    return [cell_of(i, j) for i, j in neighbor_indice_of(x, y)]

def mine(x, y):
    cell = cell_of(x, y)
    if cell != '.':
        return cell
    return str(neighbors_of(x, y).count('


for x in range(H):
    print(''.join([mine(x, y) for y in range(W)]))

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

way = ((1,1),(1,0),(1,-1),(0,1),(0,-1),(-1,1),(-1,0),(-1,-1))
ans = [[0 for j in range(w)] for i in range(h)]

for i in range(h):
    for j in range(w):
        if s[i][j] == '
            ans[i][j] = '
            continue
        for k in way:
            if not (0 <= i+k[0] < h and 0 <= j+k[1] < w): continue
            if s[i+k[0]][j+k[1]] == '

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

Solution (Python):



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

result = [[0 for i in range(W)] for j in range(H)]

for h in range(H):
    for w in range(W):
        if a[h][w] == '
            result[h][w] = '
        else:
            indice = [(h-1, w-1), (h, w-1), (h-1, w+1), (h-1, w),
                      (h+1, w+1), (h, w+1), (h+1, w-1), (h+1, w)]
            for tp in indice:
                if tp[0] >= 0 and tp[0] < H and tp[1] >= 0 and tp[1] < W \
                   and a[tp[0]][tp[1]] == '
                    result[h][w] += 1

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

Solution (Python):
h,w = (int(i) for i in input().split())
x = [input() for i in range(h)]
for i in range(h):
	answer = ""
	for j in range(w):
		if x[i][j] == ".":
			num,y = 0,[[i-1,j-1],[i-1,j],[i-1,j+1],[i,j-1],[i,j+1],[i+1,j-1],[i+1,j],[i+1,j+1]]
			for k in y:
				if -1<k[0] and k[0]<h and -1<k[1] and k[1]<w:
					if x[k[0]][k[1]] == "
						num += 1
			answer += str(num)
		else:
			answer += "
	print(answer)

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):
import sys

stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline()

h,w = na()
mp = []
for i in range(h):
    mp.append([_ for _  in ns().strip()])

for i in range(h):
    for j in range(w):
        if mp[i][j] != "
            bo = 0
            for k in range(-1,2):
                for l in range(-1,2):
                    ni = i + k
                    nj = j + l
                    if 0 <= ni < h and 0 <= nj < w and mp[ni][nj] == "
                        bo += 1
            mp[i][j] = chr(bo + 48)
for i in mp:
    print("".join(i))

Solution (Python):
h,w=map(int,input().split())
lis=[input() for i in range(h)]
outs=[]
for x in range(h):
    s=""
    for y in range(w):
        if lis[x][y]==".":
            c=0
            for xi,yi in [(1,1),(1,0),(1,-1),(0,1),(0,-1),(-1,1),(-1,0),(-1,-1)]:
                if not (xi+x==-1 or yi+y==-1 or xi+x==h or yi+y==w):
                    c+=(1 if lis[xi+x][yi+y]=="
            s+=str(c)
        else:
            s+="
    outs.append(s)
for i in outs:
    print(i)

Solution (Python):
H, W = map(int, input().split())
S = [input() for _ in range(H)]
for i in range(H):
    for j in range(W):
        count = 0
        if S[i][j] != "
            for k in [-1,0,1]:
                for l in [-1,0,1]:
                    if 0<=i+k<=H-1 and 0<=j+l<=W-1:
                        if S[i+k][j+l] == "
                            count += 1
            S[i] = S[i].replace(".",str(count),1)
for i in range(H):
    print(S[i])

Solution (Python):
H,W=map(int,input().split())
list = []
for i in range(H):
  list.append(input())
result = ['']*H
for j in range(H):
  for k in range(W):
    if list[j][k] == '
      result[j] = result[j] + '
    else:
      count = 0
      for n in range(-1,2):
        for m in range(-1,2):
          if 0 <= n+j and n+j <=(H-1) and 0<= m+k and m+k <=(W-1):
            if list[n+j][m+k] == '
              count += 1
      result[j] = result[j] + str(count)
for x in range(H):
	print(result[x])

Solution (Python):






def is_filled_with_all_bombs(s, height, width):
    bomb_count = 0

    for i in range(height):
        bomb_count += s[i][0].count('

    return bomb_count == height * width


def replace_dot_with_number(s, height: int, width: int) -> list:
    results = list()
    mod_s = ''

    for i in range(height):
        for j in range(width):
            if s[i][0][j] == '.':
                bomb_count = count_bombs_in_eigth_cells(s, i, j)
                mod_s += str(bomb_count)
            else:
                mod_s += '
        results.append(mod_s)
        mod_s = ''

    return results


def count_bombs_in_eigth_cells(s, x: int, y: int) -> int:
    count = 0
    height = len(s)
    width = len(s[0][0])

    for i in range(x - 1, x + 2):
        if (i == x - 1 < 0):
            continue
        if (i == x + 1 > height - 1):
            continue
        for j in range(y - 1, y + 2):
            if (j == y - 1 < 0):
                continue
            if (j == y + 1 > width - 1):
                continue

            if s[i][0][j] == '
                count += 1
    return count


def output(results):
    for line in results:
        print(''.join(line))


if __name__ == '__main__':
    height, width = list(map(int, input().split()))
    s = [list(map(str, input().split())) for _ in range(height)]

    if is_filled_with_all_bombs(s, height, width):
        output(s)
    else:
        results = replace_dot_with_number(s, height, width)
        output(results)

Solution (Python):



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

def ncell(i, j):
    DX = [i-1, i, i+1, i-1, i+1, i-1, i, i+1]
    DY = [j-1, j-1, j-1, j, j, j+1, j+1, j+1]
    XX = []
    YY = []
    for k in range(8):
        if (0<=DX[k]<=H-1 and 0<=DY[k]<=W-1):
            XX.append(DX[k]); YY.append(DY[k])
    return [XX, YY]
for i in range(H):
    st = ""
    for j in range(W):
        if S[i][j] == ".":
            dd = ncell(i, j)
            dx = dd[0]; dy = dd[1];
            m = 0
            for k in range(len(dx)):
                if S[dx[k]][dy[k]] == "
                    m += 1
            st += str(m)
        else:
            st += "
    print(st)

Solution (Python):

H, W = map(int, input().split())
A = [list(input()) for _ in range(H)]
ds = [(-1, -1), (0, -1), (1, -1), (-1, 0), (1, 0), (-1, 1), (0, 1), (1, 1)]
for i in range(H):
    for j, c in enumerate(A[i]):
        if c == '.':
            cnt = 0
            for d in ds:
                x = j + d[0]
                y = i + d[1]
                if 0 <= x <= W - 1 and 0 <= y <= H - 1 and A[y][x] == '
                    cnt += 1
            A[i][j] = str(cnt)

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

Solution (Python):
H, W = map(int, input().split())
S = [input() for i in range(H)]
offset_h = (-1,-1,-1,0,0,1,1,1)
offset_w = (-1,0,1,-1,1,-1,0,1)
for h in range(H):
  for w in range(W):
    if S[h][w] == "
      print("
    else:
      bomb = 0
      for i,j in zip(offset_h, offset_w):
        if h+i >= 0 and h+i < H and w+j >=0 and w+j < W and S[h+i][w+j]=="
          bomb += 1
            
      print(bomb, end="")
     
  print("")

Solution (Python):
import sys
import math
import collections
import itertools
import array
import inspect


sys.setrecursionlimit(10000)


def chkprint(*args):
    names = {id(v):k for k,v in inspect.currentframe().f_back.f_locals.items()}
    print(', '.join(names.get(id(arg),'???')+' = '+repr(arg) for arg in args))


def to_bin(x):
    return bin(x)[2:]


def dim2input(N):
    li = []
    for _ in range(N):
        li.append(input().split())
    return li



dp = None

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

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

    for idx_h in range(H):
        for idx_w in range(W):
            if S[idx_h][idx_w] == "
                continue

            bomb = 0

            for dh in range(-1, 2):
                new_h = idx_h + dh
                for dw in range(-1, 2):
                    new_w = idx_w + dw

                    if dh == 0 and dw == 0:
                        continue

                    if 0 <= new_h < H and 0 <= new_w < W and S[new_h][new_w] == "
                        bomb += 1

            S[idx_h][idx_w] = bomb

    for row in S:
        for cell in row:
            print(cell, end="")
        print()

main()

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

x = [-1, -1, 0, 1, 1, 1, 0, -1]
y = [0, 1, 1, 1, 0, -1, -1, -1]
for px in range(h):
    for py in range(w):
        if s[px][py] == '

        cou = 0
        for d in range(len(x)):
            tempx = px + x[d]
            tempy = py + y[d]

            if tempx < 0 or tempx >= h or tempy < 0 or tempy >= w:
                continue
            if s[tempx][tempy] == '
                cou += 1

        s[px] = s[px].replace('.', str(cou), 1)

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

Solution (Python):
h,w=map(int,input().split())
a=""
for i in range(w+2):
    a=a+"a"
S=[a]
for i in range(h):
    S.append("a"+input()+"a")
S.append(a)
for i in range(1,h+1):
    for j in range(1,w+1):
        if S[i][j]==".":
            num=0
            for u in range(-1,2):
                for v in range(-1,2):
                    if S[i+u][j+v]=="
                        num+=1
            S[i]=S[i][:j]+str(num)+S[i][j+1:]
for i in range(1,h+1):
    print(S[i][1:w+1])

Solution (Python):
h,w=map(int,input().split())
lis=[]
outp=[[0 for i in range(w)] for j in range(h)]
outs=[""]*h
for i in range(h):
    lis.append(input())
for i in range(h):
    for j in range(w):
        if(lis[i][j]=="
            for k in range(-1,2):
                for l in range(-1,2):
                    if(i+k>=0 and i+k<h and j+l>=0 and j+l<w):
                        outp[i+k][j+l]+=1
for i in range(h):
    for j in range(w):
        if(lis[i][j]=="
            outs[i]+="
        else:
            outs[i]+=str(outp[i][j])
for i in range(h):
    print(outs[i])

Solution (Python):
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] == ".":
            S[i][j] = 0
if H == 1 and W == 1:
    print(S[0][0])
elif H == 1:
    if S[0][0] == "
        if S[0][1]!="
    if S[0][W-1]== "
        if S[0][W-2]!="
    for i in range(1,W-1):
        if S[0][i] =="
            if S[0][i-1]!="
            if S[0][i+1]!="
    
    for i in range(H):
        print("".join(map(str,S[i][:])))


elif W == 1:
    if S[0][0] == "
        if S[1][0]!="
    if S[H-1][0]== "
        if S[H-2][0]!="
    for i in range(1,H-1):
        if S[i][0] =="
            if S[i-1][0]!="
            if S[i+1][0]!="
    for i in range(H):
        print("".join(map(str,S[i][:])))

else:
    
    if S[0][0] == "
        if S[1][0] !="
        if S[0][1] !="
        if S[1][1] !="
    if S[0][W-1] == "
        if S[0][W-2] !="
        if S[1][W-2] !="
        if S[1][W-1] !="
    if S[H-1][0] == "
        if S[H-2][0] !="
        if S[H-1][1] !="
        if S[H-2][1] !="
    if S[H-1][W-1] == "
        if S[H-2][W-1] !="
        if S[H-2][W-2] !="
        if S[H-1][W-2] !="
    
    for i in range(1,H-1):
        if S[i][0] == "
            if S[i-1][0] !="
            if S[i+1][0] !="
            if S[i][1] !="
            if S[i-1][1] !="
            if S[i+1][1] !="

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

    for i in range(1,W-1):
        if S[0][i] == "
            if S[0][i-1] !="
            if S[0][i+1] !="
            if S[1][i] !="
            if S[1][i-1] !="
            if S[1][i+1] !="

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

    for i in range(1,H-1):
        for j in range(1,W-1):   
            if S[i][j] == "
                if S[i-1][j-1] !="
                if S[i-1][j] !="
                if S[i-1][j+1] !="
                if S[i][j-1] !="
                if S[i][j+1] !="
                if S[i+1][j-1] !="
                if S[i+1][j] !="
                if S[i+1][j+1] !="
    for i in range(H):
        print("".join(map(str,S[i][:])))

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

dirx = [-1, -1, 0, 1, 1, 1, 0, -1]
diry = [0, 1, 1, 1, 0, -1, -1, -1]
for i in range(h):
    for j in range(w):
        if s[i][j] == '
        
        bumb_num = 0
        for d in range(len(dirx)):
            tempi = i + dirx[d]
            tempj = j + diry[d]

            if tempi < 0 or tempi >= h or tempj < 0 or tempj >= w: continue 
            if s[tempi][tempj] == '
        
        s[i] = s[i].replace('.', str(bumb_num), 1)

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

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

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

for i in range(H):
	for j in range(W):
		cnt=0
		if S[i][j]!="
			for x in dx:
				for y in dy:
					if 0<=i+y<H and 0<=j+x<W:
						if S[i+y][j+x]=="
							cnt+=1
			S[i][j]=str(cnt)				
		
for s in S:
	print("".join(s))

Solution (Python):
H, W = map(int, input().split())
S = []
for i in range(H):
    S.append(input())
for i in range(H):
    for j in range(W):
        sum = 0
        if(S[i][j] == '
            print('
            continue
        for a in range(-1, 2):
            for b in range(-1, 2):
                if(i + a < 0 or i + a >= H or j + b < 0 or j + b >= W):
                    continue
                if(S[i + a][j + b] == '
                    sum += 1
        print(sum, end = "")
    print()

Solution (Python):
H, W = map(int, input().split())
M = [[j if j == "

for i in range(H):
    M[i].append(0)
    M[i].insert(0, 0)
HH = [0 for i in range(W+2)]
M.append(HH)
M.insert(0, HH)

for i in range(1, H+1):
    for j in range(1, W+1):
        if M[i][j] == "
            if M[i-1][j-1] != "
                M[i-1][j-1] += 1
            if M[i-1][j] != "
                M[i-1][j] += 1
            if M[i-1][j+1] != "
                M[i-1][j+1] += 1
            if M[i][j-1] != "
                M[i][j-1] += 1
            if M[i][j+1] != "
                M[i][j+1] += 1
            if M[i+1][j-1] != "
                M[i+1][j-1] += 1
            if M[i+1][j] != "
                M[i+1][j] += 1
            if M[i+1][j+1] != "
                M[i+1][j+1] += 1
for i in range(1, H+1):
    for j in range(1, W+1):
        print(M[i][j], end="")
    print()

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

    for x in range(h):
        for y in range(w):
            cnt = 0
            if S[x][y] == ".":
                if x != 0:
                    if S[x - 1][y] == "
                        cnt += 1
                if y != 0:
                    if S[x][y - 1] == "
                        cnt += 1
                if x != h - 1:
                    if S[x + 1][y] == "
                        cnt += 1
                if y != w - 1:
                    if S[x][y + 1] == "
                        cnt += 1
                if x >= 1 and y >= 1:
                    if S[x - 1][y - 1] == "
                        cnt += 1
                if x >= 1 and y < w - 1:
                    if S[x - 1][y + 1] == "
                        cnt += 1
                if x < h - 1 and y >= 1:
                    if S[x + 1][y - 1] == "
                        cnt += 1
                if x < h - 1 and y < w - 1:
                    if S[x + 1][y + 1] == "
                        cnt += 1
                S[x][y] = cnt 
            print(S[x][y], end = "")
        print()



if __name__ == "__main__":
    main()

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

    for x in range(h):
        for y in range(w):
            cnt = 0
            if S[x][y] == ".":
                if x != 0:
                    if S[x - 1][y] == "
                        cnt += 1
                if y != 0:
                    if S[x][y - 1] == "
                        cnt += 1
                if x != h - 1:
                    if S[x + 1][y] == "
                        cnt += 1
                if y != w - 1:
                    if S[x][y + 1] == "
                        cnt += 1
                if x >= 1 and y >= 1:
                    if S[x - 1][y - 1] == "
                        cnt += 1
                if x >= 1 and y < w - 1:
                    if S[x - 1][y + 1] == "
                        cnt += 1
                if x < h - 1 and y >= 1:
                    if S[x + 1][y - 1] == "
                        cnt += 1
                if x < h - 1 and y < w - 1:
                    if S[x + 1][y + 1] == "
                        cnt += 1
                S[x][y] = cnt 

    for x in range(h):
        for y in range(w):
            print(S[x][y], end = "")
        print()

if __name__ == "__main__":
    main()

Solution (Python):



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

def ncell(i, j):
    DX = [i-1, i, i+1, i-1, i+1, i-1, i, i+1]
    DY = [j-1, j-1, j-1, j, j, j+1, j+1, j+1]
    XX = []
    YY = []
    for k in range(8):
        if (0<=DX[k]<=H-1 and 0<=DY[k]<=W-1):
            XX.append(DX[k]); YY.append(DY[k])
    return [XX, YY]
for i in range(H):
    st = ""
    for j in range(W):
        if S[i][j] == ".":
            dd = ncell(i, j)
            dx = dd[0]; dy = dd[1];
            m = 0
            for k in range(len(dx)):
                if S[dx[k]][dy[k]] == "
                    m += 1
            st += str(m)
        else:
            st += "
    print(st)

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

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

A=[-1,-1,-1,0,0,1,1,1]
B=[-1,0,1,-1,1,-1,0,1]
Ts=""
T=[]
t=0
for i in range(H):
    for j in range(W):

        if S[i][j]=="
            Ts+="
            continue

        for a,b in zip(A,B):
            ni=i+a
            nj=j+b

            if ni<0 or ni>=H or nj <0 or nj>=W: continue
            if S[ni][nj]=="
        Ts+=str(t)
        t=0

    T.append(Ts)
    Ts=""

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

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

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

        if s[i][j] != '
            s[i] = s[i].replace('.',str(count),1)
                
for i in range(h):
    print(s[i])

Solution (Python):
H, W = map(int, input().split())
S = [list(input()) for i in range(H)]
ans = [[0] * (W + 2) for i in range(H + 2)]
mine = []
for i in range(H):
	for j in range(W):
		if S[i][j] != "
			continue
		mine.append((i, j))
		for v in range(3):
			for h in range(3):
				ans[i + v][j + h] += 1

for i, j in mine:
	ans[i + 1][j + 1] = "
for l in ans[1: H + 1]:
	print("".join(map(str, l[1: W + 1])))

Solution (Python):

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

d = {}
for i in range(h):
    for j in range(w):
        d[(i+1, j+1)] = xs[i][j]


def neighbore(d, coor):
    i, j = coor
    mine = 0
    for di in range(-1, 2):
        for dj in range(-1, 2):
            if di == 0 and dj == 0:
                continue
            if d.get((i+di, j+dj), ".") == "
                mine  += 1
    return str(mine)


for i in range(1, h+1):
    line = []
    for j in range(1, w+1):
        if d[i, j] == ".":
            line.append(neighbore(d, (i, j)))
        elif d[(i,j)] == "
            line.append("
        else:
            assert(False)
    print("".join(line))

Solution (Python):
h,w = map(int,input().split())
arr=[]
arr.append(['.']*(w+2))

for _ in range(h):
    arr.append(list('.'+input()+'.'))
arr.append(['.']*(w+2))

ans = []

for i in range(h):
    ans.append([])
    for j in range(w):
        cnt=0
        if arr[i+1][j+1]=='
            cnt='
        else:
            for hh in range(3):
                for ww in range(3):
                    if not(hh==1 and ww==1) and arr[i+hh][j+ww]=='
                        cnt+=1
            cnt=str(cnt)
        ans[i].append(cnt)
for line in ans:
    print(''.join(line))

Solution (Python):
h, w = [int(x) for x in input().split()]
m = []
m.append(list("." * (w + 2)))
for _ in range(h):
    m.append(["."] + list(input()) + ["."])
m.append(list("." * (w + 2)))
for i in range(1, h + 1):
    for j in range(1, w + 1):
        if m[i][j] == ".":
            c = 0
            if m[i-1][j-1] == "
            if m[i-1][j]   == "
            if m[i-1][j+1] == "
            if m[i][j-1]   == "
            if m[i][j]     == "
            if m[i][j+1]   == "
            if m[i+1][j-1] == "
            if m[i+1][j]   == "
            if m[i+1][j+1] == "
            print(str(c), end="")
        else:
            print("
    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] == '
        else:
            l += str(sum([ss[max(0, j-1):min(w, j+2)].count('
    print(l)

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

for i in range(h):
    for j in range(w):
        if s[i][j]=='
            continue
        c=0
        for dx in range(-1, 2):
            for dy in range(-1, 2):
                nx=i+dx
                ny=j+dy
                if nx>=0 and nx<h and ny>=0 and ny<w:
                    if s[nx][ny]=='
                        c+=1
        ans[i][j]=str(c)
for i in range(h):
    print(''.join(ans[i]))

Solution (Python):

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

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

for i in range(h):
    s = ''
    for j in range(w):
        if S[i][j] == '
            s += S[i][j]
        else:
            x = 0
            for dx in [-1, 0, 1]:
                for dy in [-1, 0, 1]:
                    ii = i + dy
                    jj = j + dx
                    if 0 <= ii < h and 0 <= jj < w and S[ii][jj] == '
                        x += 1
            s += str(x)
    print(s)

Solution (Python):
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]=='
            continue
        c=0
        for di in range(-1,2):
            if(i+di<0 or i+di>=h):
                continue
            for dj in range(-1,2):
                if(j+dj<0 or j+dj>=w):
                    continue
                if(s[i+di][j+dj]=='
                    c+=1
        s[i][j]=c

for i in s:
    i=map(str,i)
    si="".join(i)
    print(si)

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

def search(h,w):
        if s[h][w]=='
                return '
        count=0
        for i in range(-1,2):
                for j in range(-1,2):
                        if 0<=h+i and h+i<H and 0<=w+j and w+j<W:
                                if i!=0 or j!=0:
                                        if s[h+i][w+j]=='
                                                count+=1
        return count

for h in range(H):
        for w in range(W):
                print(search(h,w),end='')
        print()

Solution (Python):
H, W = map(int, raw_input().split())
grid = [raw_input() for r in range(H)]
for r in range(H):
    line = ""
    for c in range(W):
        if grid[r][c] == "
            line += "
        else:
            cnt = 0
            for dr in range(-1, 2):
                for dc in range(-1, 2):
                    nr, nc = r+dr, c+dc
                    if (0 <= nr < H) and (0 <= nc < W) and (grid[nr][nc] == "
                        cnt += 1
            line += str(cnt)
    print line

Solution (Python):
h, w = map(int, raw_input().split())
table = list()
table.append([0]*(w+2))
for _ in xrange(h):
    table.append([0]+list(map(lambda c:0 if c == "." else c, list(raw_input())))+[0])
table.append([0]*(w+2))

xy = [0, 1, 0, -1, 1, 1, -1, -1, 0]
for i in xrange(1, h+1):
    for j in xrange(1, w+1):
        if table[i][j] == "
            for k in xrange(8):
                if type(table[i+xy[k]][j+xy[k+1]]) == type(int()):
                    table[i+xy[k]][j+xy[k+1]] += 1


for i in xrange(1, h+1):
    print "".join(map(str, table[i][1:-1]))

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

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

for i in range(H):
    for j in range(W):
        if T[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 T[i+dy][j+dx] == '
                    cnt += 1
        T[i][j] = str(cnt)

for t in T:
    print(''.join(t))

Solution (Python):



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


def check(li, x, y):
    round_list = [[-1,-1],[-1,0],[-1,1],[0,-1],[0,1],[1,-1],[1,0],[1,1]]
    count = 0
    num = 0
    for i, j in round_list:
        if 0 <= x + i < h and 0 <= y + j < w:
            if li[x+i][y+j] == "
                count += 1
    return count


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

for i in range(h):
    for j in range(w):
        if m[i][j] != "
            m[i][j] = check(m, i, j)


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

Solution (Python):
num = [int(x) for x in input().split()]
array = []
for i in range(num[0]):
    array.append(list(input()))


for j in range(num[0]):
    for k in range(num[1]):
        cnt = 0
        if array[j][k] != "
            try:
                if array[j-1][k] == "
                    cnt += 1
            except IndexError:
                pass
            try:
                if array[j-1][k-1] == "
                    cnt += 1
            except IndexError:
                pass
            try:
                if array[j-1][k+1] == "
                    cnt += 1
            except IndexError:
                pass
            try:
                if array[j][k-1] == "
                    cnt += 1
            except IndexError:
                pass
            try:
                if array[j][k+1] == "
                    cnt += 1
            except IndexError:
                pass
            try:
                if array[j+1][k] == "
                    cnt += 1
            except IndexError:
                pass
            try:
                if array[j+1][k-1] == "
                    cnt += 1
            except IndexError:
                pass
            try:
                if array[j+1][k+1] == "
                    cnt += 1
            except IndexError:
                pass
            array[j][k] = str(cnt)

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

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

A = [list(input()) for i in range(H)]

for i in range(H):
	for j in range(W):
		if A[i][j] != '
			count = 0
			for k in range(-1,2):
				for l in range(-1,2):
					if -1 < i + k and i + k < H and -1 < j + l and j + l < W:
						if A[i+k][j+l] == '
							count += 1
			
			A[i][j] = str(count)

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