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())
s0 = [list(input()) for _ in range(h)]
s = [["0"]*(w+2)]+[["0"]+s0[i]+["0"] for i in range(h)]+[["0"]*(w+2)]
for i in range(1, h+1):
    for j in range(1, w+1):
        if s[i][j] == ".":
            s[i][j] = "{0}".format([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):
h,w=map(int,input().split())
from collections import defaultdict
data=defaultdict(int)
for y in range(h):
    line=input()
    for x in range(w):
        if line[x]=='
            data[(y,x)]=-1
            for i in range(-1,2):
                for j in range(-1,2):
                    if data[(y+i,x+j)]!=-1:
                        data[(y+i,x+j)]+=1
for y in range(h):
    s=''
    for x in range(w):
        if data[(y,x)]==-1:s+='
        else:s+=str(data[(y,x)])
    print(s)

Solution (Python):


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


def is_bomb(x, y):
    return S[y][x] == "

def count_neibor(x, y):
    dxs = [1, -1, 0, 0, 1, 1, -1, -1]
    dys = [0, 0, 1, -1, 1, -1, 1, -1]
    count = 0
    for dx, dy in zip(dxs, dys):
        check_x = x + dx
        check_y = y + dy
        if 0 <= check_x < W and 0 <= check_y < H:
            count += 1 if S[check_y][check_x] == "
    return count

result = []
for y in range(H):
    row = []
    for x in range(W):
        if is_bomb(x, y):
            row.append("
        else:
            row.append(str(count_neibor(x, y)))
    result.append(row)

for c in result:
    print(''.join(c))

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


S.append("."*(W+2))
for i in range(H):
    S.append("."+input()+".")
S.append("."*(W+2))


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


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


for y in range(1, H+1):
    for x in range(1, W+1):
        count = 0

        for i in range(8):
                if S[y+dy[i]][x+dx[i]] == "
                    count +=1
                search[y-1][x-1] = count

                if S[y][x] == "
                    search[y-1][x-1] = "


for i in range(H):
    row = list(map(str, search[i]))
    print(''.join(row))

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

for hh in range(h):
    for ww in range(w):
        if li[hh][ww] == '
            print('
        else:
            w0 = max(ww - 1, 0)
            w1 = min(ww + 2, w)
            h0 = max(hh - 1, 0)
            h1 = min(hh + 2, h)
            cnt = sum(li[hx][w0: w1].count('
            print(cnt, end='')
    print('')

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

x = [0,1,1,1,0,-1,-1,-1]
y = [1,1,0,-1,-1,-1,0,1]

for i in range(h):
    for j in range(w):
        if a[i][j] == '.':
            a[i][j] = 0
            for k in range(8):
                if (0 <= i+x[k] <= h-1) and (0 <= j+y[k] <= w-1):
                    if a[i+x[k]][j+y[k]] == '
                        a[i][j] += 1
            a[i][j] = str(a[i][j])

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

Solution (Python):


h,w = map(int, input().split())
r=[[0 for i in range(h+2)] for j in range(w+2)]
for i in range(0,h):
  s=input()
  for j in range(0,w):
    if s[j]=='
      r[j+1][i+1]=-1
      for y in range(-1,2):
        for x in range(-1,2):
          if x==0 and y==0:
            continue
          elif r[j+1+x][i+1+y]!=-1:
            r[j+1+x][i+1+y]+=1
for i in range(0,h):
  mz=""
  for j in range(0,w):
    if r[j+1][i+1]==-1:
      mz=mz+'
    else:
      mz=mz+str(r[j+1][i+1])
  print(mz)

Solution (Python):
h,w=map(int,input().split())
s=[list(input())for i in range(h)]
dx=[1,1,0,-1,-1,-1,0,1]
dy=[0,1,1,1,0,-1,-1,-1]
for i in range(h):
    for j in range(w):
        cnt=0
        if s[i][j]==".":
            for k in range(8):
                x=i+dx[k]
                y=j+dy[k]
                if 0<=x and x<h and 0<=y and y<w and s[x][y]=="
                    cnt+=1
            s[i][j]=str(cnt)
for i in s:
    print(*i,sep="")

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

    

    ans = []
    for i in range(h):
        e = []
        for j in range(w):
            e.append(0)
        ans.append(e)

    for i in range(h):
        for j in range(w):
            count = 0
            v = b[i][j]
            if v == "
                ans[i][j] = '
                continue

            for k in range(-1, 2):
                for l in range(-1, 2):
                    if i + k < 0 or i + k >= h:
                        pass
                    elif j + l < 0 or j + l >= w:
                        pass
                    else:
                        if b[i + k][j + l] == "
                            count += 1
            ans[i][j] = count

    for l1 in ans:
        for l2 in l1:
            print(l2, end="")
        print("")


if __name__ == '__main__':
    main()

Solution (Python):
h, w = map(int, input().split())
S = [list(input()) for i 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):

def getInt(): return int(input())
def getIntList(): return [int(x) for x in input().split()]
def zeros(n): return [0 for i in range(n)]
def db(x): 
    global debug
    if debug: print(x)
debug = False
H,W = getIntList()
S = zeros(H+2)
S[0] = '.'*(W+2)
for i in range(H): S[i+1] = '.' + input() + '.'
S[H+1] = '.'*(W+2)
db(S)
for i in range(1,H+1):
    for j in range(1,W+1):
        if S[i][j]=='
        c = 0
        for k in range(3):
            if S[i-1][j+k-1]=='
        if S[i][j-1]=='
        if S[i][j+1]=='
        for k in range(3):
            if S[i+1][j+k-1]=='
        S[i] = S[i][:j] + str(c) + S[i][j+1:]
    db(S[i])
db(S)
for i in range(H):
    print(S[i+1][1:W+1])

Solution (Python):
h, w = map(int, input().split())
S = [input() for i 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 = map(int, input().split())
A = []
A.append([0] * (w + 2))
for i in range(h):
    A.append([0] + list(input()) + [0])
A.append([0] * (w + 2))

for i in range(h + 2):
    for j in range(w + 2):
        if A[i][j] == '.':
            A[i][j] = 0
            
for i in range(h + 2):
    for j in range(w + 2):
        if A[i][j] == '
            if A[i-1][j-1] != '
                A[i-1][j-1] += 1
            if A[i-1][j] != '
                A[i-1][j] += 1
            if A[i-1][j+1] != '
                A[i-1][j+1] += 1
            if A[i][j-1] != '
                A[i][j-1] += 1
            if A[i][j+1] != '
                A[i][j+1] += 1
            if A[i+1][j-1] != '
                A[i+1][j-1] += 1
            if A[i+1][j] != '
                A[i+1][j] += 1
            if A[i+1][j+1] != '
                A[i+1][j+1] += 1
                
for i in range(1, h + 1):
    print(''.join(map(str, A[i][1:w+1])))

Solution (Python):
import numpy as np


def main():
    N, W = list(map(int, input().split()))
    bomb_map = np.zeros((N+2, W+2))
    for i in range(1,N+1):
        bomb_map[i,1:-1] = np.array(list(map(lambda x: 0 if x =='.' else 1, input())))

    new_map = bomb_map[:-2,:-2] + bomb_map[:-2,1:-1] + bomb_map[:-2,2:] \
                + bomb_map[1:-1,:-2] + bomb_map[1:-1,1:-1] + bomb_map[1:-1,2:] \
                    + bomb_map[2:,:-2] + bomb_map[2:,1:-1] + bomb_map[2:,2:]

    new_map = new_map * (bomb_map[1:-1,1:-1] * -2 + 1)

    result = [[str(int(i)) if i >= 0 else '


    for l in result:
        print("".join(l))


if __name__ == '__main__':
    main()

Solution (Python):
H,W = map(int,input().split())
ans = []
board = []
for _ in range(H):
    row = list(input())
    board.append(row)
for i in range(H):
    row = ""
    for q in range(W):
        neighbor = []
        if q > 0: 
            neighbor.append(board[i][q-1]) 
            if i > 0:
                neighbor.append(board[i-1][q-1]) 
            if i < (H - 1):
                neighbor.append(board[i+1][q-1]) 
        if i > 0: 
            neighbor.append(board[i-1][q])
        if q < (W - 1): 
            neighbor.append(board[i][q+1]) 
            if i > 0:
                neighbor.append(board[i-1][q+1]) 
            if i < (H - 1):
                neighbor.append(board[i+1][q+1]) 
        if i < (H - 1): 
            neighbor.append(board[i+1][q])
        if board[i][q] == "
            row += board[i][q]
        else:
            row += str(neighbor.count("
    print(row)

Solution (Python):
H,W = map(int,input().split())
ans = []
board = []
for _ in range(H):
    row = list(input())
    board.append(row)
for i in range(H):
    row = ""
    for q in range(W):
        neighbor = []
        if q > 0: 
            neighbor.append(board[i][q-1]) 
            if i > 0:
                neighbor.append(board[i-1][q-1]) 
            if i < (H - 1):
                neighbor.append(board[i+1][q-1]) 
        if i > 0: 
            neighbor.append(board[i-1][q])
        if q < (W - 1): 
            neighbor.append(board[i][q+1]) 
            if i > 0:
                neighbor.append(board[i-1][q+1]) 
            if i < (H - 1):
                neighbor.append(board[i+1][q+1]) 
        if i < (H - 1): 
            neighbor.append(board[i+1][q])
        if board[i][q] == "
            row += board[i][q]
        else:
            row += str(neighbor.count("
    print(row)

Solution (Python):
import sys
r,c = map(int,input().split())
masume = [[0 for i in range(c)] for j in range(r)]
for i in range(r):
	masume[i] = list(input())

for ri in range(r):
	for ci in range(c):
		if masume[ri][ci] == '
			sys.stdout.write(masume[ri][ci])
			continue
		count = 0
		for ar in [-1,0,1]:
			for ac in [-1,0,1]:
				chkr = ri + ar
				chkc = ci + ac
				if chkr >= 0 and chkr < r and chkc >= 0 and chkc < c and masume[chkr][chkc] == '
					count+= 1
		sys.stdout.write(str(count))
	print("")

Solution (Python):
import numpy as np
h, w = map(int, input().split())
num_array = np.zeros((h, w))
space_array = np.ones((h, w))

mine_map = np.array(
    [np.array([0 if i == "." else 1 for i in input()]) for j in range(h)])
space_array = mine_map.copy()

for i, s in enumerate(mine_map):
    for j, m in enumerate(s):
        if m == 1: 
            il, iu = i-1, i+2
            jl, ju = j-1, j+2
            il, jl = max(il, 0), max(jl, 0)
            iu, ju = min(iu, h), min(ju, w)
            num_array[il:iu, jl:ju] += 1

for i, s in enumerate(space_array):
    out = ["
    print("".join(out))

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

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

Solution (Python):
h,w = (int(i) for i in input().split())

s = [[i for i in input()] for j in range(h)]


mylist=[[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]=='
            if i-1 >= 0 and j-1 >= 0:
                mylist[i-1][j-1]+=1
            if i-1 >= 0:
                mylist[i-1][j]+=1
            if i-1 >= 0 and j+1 <= w-1:
                mylist[i-1][j+1]+=1
            if j-1 >= 0:
                mylist[i][j-1]+=1
            if j+1 <= w-1:
                mylist[i][j+1]+=1
            if i+1 <= h-1 and j-1 >= 0:
                mylist[i+1][j-1]+=1
            if i+1 <= h-1:
                mylist[i+1][j]+=1
            if i+1 <= h-1 and j+1 <= w-1:
                mylist[i+1][j+1]+=1

for i in range(h):
    for j in range(w):
        if s[i][j]=='
            mylist[i][j]='
            
for i in range(h):
  for j in range(w):
    print(mylist[i][j], end="")
  print('')

Solution (Python):
import sys

sys.setrecursionlimit(200000)


def input():
    return sys.stdin.readline()[:-1]


def ii(t: type = int):
    return t(input())


def il(t: type = int):
    return list(map(t, input().split()))


def imi(N: int, t: type = int):
    return [ii(t) for _ in range(N)]


def iml(N: int, t: type = int):
    return [il(t) for _ in range(N)]


class UnionFind:
    def __init__(self, n):
        self.par = [i for i in range(n)]
        self.rank = [0] * (n)

    
    def find(self, x):
        if self.par[x] == x:
            return x
        else:
            self.par[x] = self.find(self.par[x])
            return self.par[x]

    
    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if self.rank[x] < self.rank[y]:
            self.par[x] = y
        else:
            self.par[y] = x
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1

    
    def same_check(self, x, y):
        return self.find(x) == self.find(y)


def solve():
    H, W = il()
    S = imi(H, str)
    from itertools import product

    dst = ((i, j) for i, j in product(range(-1, 2), repeat=2))
    dst = list(dst)
    B = [[0] * W for _ in range(H)]
    sharp_pos = []
    for i, j in product(range(H), range(W)):
        if S[i][j] == "
            B[i][j] = "
            sharp_pos.append((i, j))
    for i, j in sharp_pos:
        for d in dst:
            x = i + d[1]
            y = j + d[0]
            if 0 <= x < H and 0 <= y < W:
                if S[i][j] == "
                    B[x][y] += 1
    for b in B:
        print("".join(map(str, b)))


if __name__ == "__main__":
    solve()

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

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

for i in range(H):
    for j in range(W):
        count = 0
        if S[i][j] == '
            continue
        else:
            for k in range(8):
                ni = i + dx[k]
                nj = j + dy[k]
                if 0 <= ni and ni < H and 0 <= nj and nj < W:
                    if S[ni][nj] == '
                        count += 1
            S[i][j] = str(count)

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

Solution (Python):


import sys


INF = float('inf')
MOD = 10 ** 9 + 7

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def II(): return int(sys.stdin.readline())
def SI(): return input()


h, w = LI()
M = [SI() for _ in range(h)]

d = ((0, 1), (-1, 1), (-1, 0), (-1, -1),
     (0, -1), (1, -1), (1, 0), (1, 1))

ans = []
for y in range(h):
    row = ''
    for x in range(w):
        cnt = 0
        if M[y][x] == '
            row += '
            continue

        for dy, dx in d:
            yy, xx = y + dy, x + dx
            if 0 <= yy < h and 0 <= xx < w:
                if M[yy][xx] == '
                    cnt += 1
        row += str(cnt)
    ans.append(row)

print(*ans, sep='\n')

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

Solution (Python):
a,b=map(int,input().split())
s=["."*(b+2)]+["0"+input()+"0"for i in range(a)]+["."*(b+2)]
for i in range(1,a+1):
    for j in range(1,b+1):
        c=0
        if s[i][j]==".":
            if s[i-1][j]=="
                c+=1
            if s[i][j-1]=="
                c+=1
            if s[i+1][j]=="
                c+=1
            if s[i][j+1]=="
                c+=1
            if s[i-1][j-1]=="
                c+=1
            if s[i-1][j+1]=="
                c+=1
            if s[i+1][j-1]=="
                c+=1
            if s[i+1][j+1]=="
                c+=1
            t=s[i]
            s[i]=t[:j]+str(c)+t[j+1:]
for i in range(1,a+1):
    t=s[i]
    print(t[1:b+1])

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 = map(int, input().split())
matrix = []
for i in range(h):
  matrix.append(list(input()))
output = [''] * h
for i in range(h):
  matrix[i] = ['.'] + matrix[i] + ['.']
blank = ['.'] * (w + 2)
matrix = [blank] + matrix + [blank]
for i in range(h):
  for j in range(w):
    if (matrix[i + 1][j + 1] == '
      output[i] += '
    else:
      o = ord('0')
      for k in range(3):
        for l in range(3):
          if (matrix[i + k][j + l] == '
            o += 1
      output[i] += chr(o)
for i in range(h):
  print(output[i])

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

Solution (Python):
h,w=map(int,input().split())
l=[]
for i in range(h):
  l.append(input())
al=[]
for i in range(h):
  s=''
  for j in range(w):
    if l[i][j]=='
      s+='
      continue
    c=0
    if i>0 and j>0 and l[i-1][j-1]=='
      c+=1
    if i>0 and l[i-1][j]=='
      c+=1
    if i>0 and j<w-1 and l[i-1][j+1]=='
      c+=1
    if j>0 and l[i][j-1]=='
      c+=1
    if j<w-1 and l[i][j+1]=='
      c+=1
    if i<h-1 and j>0 and l[i+1][j-1]=='
      c+=1
    if i<h-1 and l[i+1][j]=='
      c+=1
    if i<h-1 and j<w-1 and l[i+1][j+1]=='
      c+=1
    s+=str(c)
  al.append(s)
for i in range(h):
  print(al[i])

Solution (Python):
def surround(h, w):
    ret = 0
    for i in [-1, 0, 1]:
        ret += int(ans[h-1][w+i]=="
        ret += int(ans[h+1][w+i]=="
    for i in [-1, 1]:
        ret += int(ans[h][w+i]=="
    return str(ret)


Height, Width = map(int, input().split())
ans = [list(input()) for _ in range(Height)]
ans = [["."]*Width] + ans + [["*"]*Width]

for i in range(Height+2):
    ans[i] = ["."] + ans[i] + ["."]
for h in range(1, Height+1):
    for w in range(1, Width+1):
        if ans[h][w] == "
            continue
        ans[h][w] = surround(h, w)

ans = ans[1:-1]
for i in range(Height):
    print("".join(ans[i][1:-1]))

Solution (Python):
a, b = map(int, input().split())

A = []
A.append(list('.'*(b+2)))
for i in range(a):
    A.append(list('.'+input()+'.'))
A.append(list('.'*(b+2)))
count=0
for j in range(1, a+1):
    for k in range(1, b+1):
        if(A[j][k] == '
            print('
        else:
            for l in range(-1, 2):
                for m in range(-1, 2):
                    if(A[j+l][k+m]== '
                        count+=1
            print(count, end='')
        count=0
    print()

Solution (Python):
from sys import stderr, setrecursionlimit
setrecursionlimit(2147483647)
def getInt():
    return int(input())
def getInts():
    return [int(i) for i in input().split()]
def getIntLines(n=1):
    res = []
    for _ in range(n):
        res.append(getInt())
    return res
def getIntsLines(n=1):
    res = []
    for _ in range(n):
        res.append(getInts())
    return res
def debug(*args, sep=" ", end="\n"):
    for item in args:
        stderr.write(str(item))
        stderr.write(sep)
    stderr.write(end)

h, w = getInts()
grid = [['.' for _ in range(w+2)]]
for _ in range(h):
    tmp = ['.']
    tmp.extend(list(input()))
    tmp.append('.')
    grid.append(tmp)
grid.append(['.' for _ in range(w+2)])

for i in range(1,h+1):
    for j in range(1,w+1):
        if grid[i][j] == '
            continue
        count = 0
        for k in range(-1,2):
            for l in range(-1,2):
                if grid[i+k][j+l] == '
                    count += 1
        grid[i][j] = count
for l in grid[1:-1]:
    print(*l[1:-1], sep='')

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

game = []
game.append([0 for _ in range(W + 2)])
game.extend([[0 if c == '.' else -1 for c in line] for line in S])
game.append([0 for _ in range(W + 2)])

for y in range(H + 2):
    for x in range(W + 2):
        if game[y][x] == -1:
            for i in (-1, 0, 1):
                for j in (-1, 0, 1):
                    if game[y + i][x + j] != -1:
                        game[y + i][x + j] += 1

for line in game[1:-1]:
    print(''.join(map(str, line[1:-1])).replace('-1', '

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=map(int,input().split())
ms=[]
for i in range(h):
    m=input()
    ms.append(list(m))
count=0
for i in range(h):
    for j in range(w):
        if ms[i][j]==".":
            if i>0 and j>0:
                if ms[i-1][j-1]=="
                    count+=1
            if i>0 :
               if ms[i-1][j]=="
                    count+=1
            if i>0 and w-1>j:
                if ms[i-1][j+1]=="
                    count+=1
            if j>0 :
                if ms[i][j-1]=="
                    count+=1
            if w-1>j :
                if ms[i][j+1]=="
                    count+=1
            if h-1>i and j>0 :
                if ms[i+1][j-1]=="
                    count+=1
            if h-1>i:
                if ms[i+1][j]=="
                    count+=1
            if h-1>i and w-1>j:
                if ms[i+1][j+1]=="
                    count+=1
            ms[i][j]=str(count)
            count=0

for i in range(h):
    temp="".join([ms[i][j] for j in range(w)])
    print(temp)

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 += s[i][j]
        else:
            l += str(sum([t[max(0,j-1):min(j+2,w)].count('
                     for t in s[max(0,i-1):min(h,i+2)]]))
    print(l)

Solution (Python):
h, w = map(int, input().split())
board = [list(input()) for _ in range(h)]
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):
        if board[i][j] is '.':
            board[i][j] = 0
            for k in range(8):
                ni = i + dy[k]
                nj = j + dx[k]
                if ni >= 0 and ni < h and nj >= 0 and nj < w:
                    board[i][j] += 1 if board[i + dy[k]][j + dx[k]] is '
for s in board:
    print(''.join(map(str, s)))

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

for i in range(h):
    for j in range(w):
        count = 0
        if s[i][j] == '
            continue
        else:
            for d in range(8):
                if (0 <= i+dy[d] < h) and (0 <= j+dx[d] < w):
                    if s[i+dy[d]][j+dx[d]] == '
                        count += 1
            s[i][j] = str(count)

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

Solution (Python):
H,W=map(int,input().split())
map=[['.' for _ in range(W+2)] for _ in range(H+2)]
for i in range(H):
    s=list(input())
    map[i+1]=['.']+s+['.']
for i in range(H):
    for j in range(W):
        if map[i+1][j+1]=='.':
            cnt=0
            if map[i][j+1]=='
                cnt+=1
            if map[i][j+2]=='
                cnt+=1
            if map[i+1][j+2]=='
                cnt+=1
            if map[i+2][j+2]=='
                cnt+=1
            if map[i+2][j+1]=='
                cnt+=1
            if map[i+2][j]=='
                cnt+=1
            if map[i+1][j]=='
                cnt+=1
            if map[i][j]=='
                cnt+=1
            map[i+1][j+1]=str(cnt)
for i in range(H):
    for j in range(W):
        print(map[i+1][j+1],end='')
    print()

Solution (Python):
H, W = map(int, input().split())
S = [input() for i in range(H)]
count = 0
l = [''] * H
S_new = S
for j in range(len(S)):
    S_new[j] = '.' + S[j] + '.'
S_new.insert(H, '.'*(W+2))
S_new.insert(0, '.'*(W+2))


for i in range(1, H+1):
    for j in range(1, W+1):
        if S_new[i][j] == '
            l[i-1] += '
        else:
            for k in range(-1, 2):
                for s in range(-1, 2):
                    if S_new[i-k][j-s] == '
                        count += 1
            l[i-1] += str(count)
            count = 0
for i in range(len(l)):
    print(l[i])

Solution (Python):
g,r=map(int,input().split())
A=["."*(r+2)]
for i in range(g):
    w="."+input()+"."
    A.append(w)
A.append("."*(r+2))
for i in range(1,g+1):
    for j in range(1,r+1):
        if A[i][j]==".":
            B=[]
            for m in [i-1,i,i+1]:
                for k in [j-1,j,j+1]:
                    B.append(A[m][k])
            A[i]=A[i][1:r+1:].replace(".",str(B.count("
            A[i]="."+A[i]+"."
for i in range(1,g+1):
    print(A[i][1:r+1:])

Solution (Python):
def abc075_b():
    h, w = map(int, input().split())
    s = [None] * (h + 2)
    ans = [[None] * w for _ in range(h)]

    s[0] = s[-1] = ["."] * (w + 2)
    for i in range(1, h + 1):
        s[i] = ["."] + list(input()) + ["."]

    for i in range(1, h + 1):
        for j in range(1, w + 1):
            if s[i][j] == "
                ans[i - 1][j - 1] = "
            else:
                ans[i - 1][j - 1] = 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 ln in ans:
        print("".join(ln))


abc075_b()

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

if h == 1 and w == 1:
    if s[0][0] == '
        v = '
    else:
        v = 0
    t[0][0] = str(v)
    print(''.join(t[0]))
    
elif h ==1 and w !=1:
    for k in range(w):
        if k == 0:
            u = s[0][1]
        elif k == w-1:
            u = s[0][k-1]
        else:
            u = s[0][k-1] +s[0][k+1]
        v = u.count('
        if s[0][k] == '
            v = '
        t[0][k] = str(v)
    print(''.join(t[0]))

elif h !=1 and w == 1:
    for j in range(h):
        if j == 0:
            u = s[1][0]
        elif j == h-1:
            u = s[j-1][0]
        else:
            u = s[j-1][0] + s[j+1][0]
        v = u.count('
        if s[j][0] == '
            v = '
        t[j][0] = str(v)
        print(''.join(t[j]))

else:
    for j in range(h):
        if j == 0:
            for k in range(w):
                if k == 0:
                    u = s[0][1] + s[1][0] + s[1][1]
                elif k == w-1:
                    u = s[0][k-1] +s[1][k-1] + s[1][k]
                else:
                    u = s[0][k-1] +s[0][k+1] + s[1][k-1] + s[1][k] + s[1][k+1]
                v = u.count('
                if s[j][k] == '
                    v = '
                t[j][k] = str(v)
        elif j == h-1:
            for k in range(w):
                if k == 0:
                    u = s[j][1] + s[j-1][0] + s[j-1][1]
                elif k == w-1:
                    u = s[j][k-1] +s[j-1][k-1] + s[j-1][k]
                else:
                    u = s[j][k-1] +s[j][k+1] + s[j-1][k-1] + s[j-1][k] + s[j-1][k+1]
                v = u.count('
                if s[j][k] == '
                    v = '
                t[j][k] = str(v)
        else:
             for k in range(w):
                if k == 0:
                    u = s[j][1] + s[j-1][0] + s[j-1][1] + s[j+1][0] + s[j+1][1]
                elif k == w-1:
                    u = s[j][k-1] +s[j-1][k-1] + s[j-1][k] + s[j+1][k-1] + s[j+1][k]
                else:
                    u = s[j][k-1] +s[j][k+1] + s[j-1][k-1] + s[j-1][k] + s[j-1][k+1] + s[j+1][k-1] + s[j+1][k] + s[j+1][k+1]
                v = u.count('
                if s[j][k] == '
                    v = '
                t[j][k] = str(v)
        print(''.join(t[j]))

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

for i in range(H):
  rs = ''
  for j in range(W):
    if(S[i][j] == '
      rs += '
    else:
      tmp = 0
      for s in S[max(0, i-1): min(H, i+2)]:
        tmp += s[max(0, j-1): min(W, j+2)].count('
      rs += str(tmp)
  print(rs)

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

for i in range(0,H):
    for j in range(0,W):
        count=0

        if le[i][j]=='
            answer[i][j]='
            continue

        if (i!=H-1):
            if(le[i+1][j]=='
                count+=1

            if (j!=W-1):
                if(le[i+1][j+1]=='
                    count+=1

            if (j!=0):
                if(le[i+1][j-1]=='
                    count+=1


        if (i!=0):
            if(le[i-1][j]=='
                count+=1
            if (j!=W-1):
                if(le[i-1][j+1]=='
                    count+=1

            if (j!=0):
                if(le[i-1][j-1]=='
                    count+=1


        if (j!=W-1):
            if(le[i][j+1]=='
                count+=1

        if (j!=0):
            if(le[i][j-1]=='
                count+=1

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


for i in range(0,H):
    aaa=''
    for j in range(0,W):
        aaa+=answer[i][j]
    print(aaa)