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())

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

for i in range(h):
  s = input()
  for j in range(w):
    if s[j] == '
      dp[i][j] = '
      for p in range(i-1, i+2):
        if p < 0 or h-1 < p:
          continue
        for q in range(j-1, j+2):
          if q < 0 or w-1 < q:
            continue
          elif dp[p][q] == '
            continue
          dp[p][q] += 1
      
for k in dp:
  print(*k, sep='')

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


mine = [['

def counter(i, j):
    if (0 <= i <= h-1) and (0 <= j <=w-1):
        if field[i][j] == '
            return 1
        else:
            return 0
    else:
        return 0

for i in range(h):
    for j in range(w):
        cnt = 0
        if field[i][j] == '
            mine[i][j] = '
        else:
            for k, l in itertools.product([i-1, i, i+1], [j-1, j, j+1]):
                cnt += counter(k, l)
            mine[i][j] = cnt


for m in mine:
    print(''.join(map(str, m)))

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

for i in range(H):
    for j in range(W):
        if S[i][j] == ".":
            cnt = 0
            for di in range(-1, 2):
                for dj in range(-1, 2):
                    tmp_i = i+di
                    tmp_j = j+dj
                    if (0 <= tmp_i <= H-1) and (0 <= tmp_j <= W-1) and (S[tmp_i][tmp_j]=="
                        cnt += 1
            S[i][j] = cnt

for s in S:
    for i in s:
        print(i, end="")
    print()

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

for i in range(H):
    for j in range(W):
        if lst[i][j]==".":
            cnt=0
            for h,w in ([0,1],[0,-1],[1,0],[-1,0],[1,1],[1,-1],[-1,1],[-1,-1]):
                nh,nw=i+h,j+w
                if nh<0 or nh>H-1 or nw<0 or nw>W-1:
                    continue
                if lst[nh][nw]=="
                    cnt+=1
            lst[i][j]=cnt

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

Solution (Python):


import numpy as np
H, W = map(int, input().split())
M = []
for h in range(H):
    col = []
    s = input()
    for w in range(W):
        if s[w] == '.':
            col.append(0)
        else:
            col.append(np.inf)
    M.append(col)



for h in range(H):
    for w in range(W):
        flag_h0= True
        flag_hM = True
        flag_w0 = True
        flag_wM = True
        if M[h][w] == np.inf:
            
            if h==0:
                flag_h0= False
            if h == H - 1:
                flag_hM = False
            if w== 0:
                flag_w0 = False
            if w== W-1:
                flag_wM = False
            
            if flag_h0:
                M[h-1][w] += 1
                if  flag_w0:
                    M[h-1][w-1] += 1
                if flag_wM:
                    M[h-1][w+1] += 1
            
            if flag_w0:
                M[h][w-1] += 1
            if flag_wM:
                M[h][w+1] += 1
            
            if flag_hM:
                M[h+1][w] += 1

                if flag_w0:
                    M[h+1][w-1] += 1
                if flag_wM:
                    M[h+1][w+1] += 1

                    
for h in range(H):
    tmp_ = ''
    for w in range(W):
        if M[h][w] < np.inf:
            tmp_ += str(M[h][w])
        else:
            tmp_  += '
    print(tmp_)

Solution (Python):
hw, *ss = open(0).read().splitlines()

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

ss = list(map(list, ss))

from itertools import product

for h in range(H):
    for w in range(W):
        if ss[h][w] == '
            continue
        ans = 0
        for i,j in product([-1,0,1], [-1,0,1]):
            if 0 <= h+i < H and 0 <= w+j < W:
                ans += (ss[h+i][w+j] == '
        ss[h][w] = ans

for s in ss:
    print(''.join(map(str, s)))

Solution (Python):
from collections import deque
h, w = map(int, input().split())

grid = [list(input()) for _ in range(h)]

dy = (-1, -1, -1, 0, 0, 0, 1, 1, 1)
dx = (-1, 0, 1, -1, 0, 1, -1, 0, 1)

for i in range(h):
    for j, value in enumerate(grid[i]):
        if grid[i][j] == "
            continue
        else:
            bom_cnt = 0
            for k in range(9):
                y = i + dy[k]
                x = j + dx[k]
                if (y < 0 or y >= h or x < 0 or x >= w): continue
                if grid[i][j] == "
                if grid[y][x] == "
                    bom_cnt += 1

            grid[i][j] = bom_cnt

for i in range(h):
    for j in range(w):
        print(grid[i][j], end="")
    print()

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

for i in range(1,h+1):
    temp = ""
    for j in range(1,w+1):
        cnt = 0
        if maze[i][j] == "
            temp += "
        else:
            for y in range(-1,2,1):
                for x in range(-1,2,1):
                    if maze[i+y][j+x] == "
                        cnt += 1
            temp += str(cnt)
    ans += [temp]
for i in range(h):
    print(*ans[i],sep="")

Solution (Python):
import sys






def RD(): return sys.stdin.read()
def II(): return int(input())
def MI(): return map(int,input().split())
def MF(): return map(float,input().split())
def LI(): return list(map(int,input().split()))
def LF(): return list(map(float,input().split()))
def TI(): return tuple(map(int,input().split()))




def main():
	h,w=MI()
	G=[[0]*(w+2)]
	for _ in range(h):
		G.append([0]+list(input())+[0])
	G.append([0]*(w+2))

	for i in range(h+2):
		for j in range(w+2):
			if G[i][j]==".":
				cnt=0
				for ii in range(i-1,i+2):
					for jj in range(j-1,j+2):
						if G[ii][jj]=="
							cnt+=1
				G[i][j]=cnt
	G=G[1:-1]

	for s in G:
		print(*s[1:-1],sep="")


if __name__ == "__main__":
	main()

Solution (Python):


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

S = []
ans_S = []

for i in range(H):
    tmp_s = list(str(input()))
    S.append(tmp_s)
    ans_S.append(tmp_s)

for y in range(H):
    for x in range(W):
        tmp_ans_S = 0
        if S[y][x] == '
            tmp_ans_S = '
        else:
            
            if x > 0:
                if S[y][x - 1] == '
                    tmp_ans_S += 1 
            
            if x > 0 and y > 0:
                if S[y - 1][x - 1] == '
                    tmp_ans_S += 1
            
            if y > 0:
                if S[y - 1][x] == '
                    tmp_ans_S += 1
            
            if x < (W - 1) and y > 0:
                if S[y - 1][x + 1] == '
                    tmp_ans_S += 1
            
            if x < (W - 1):
                if S[y][x + 1] == '
                    tmp_ans_S += 1
            
            if x < (W - 1) and y < (H - 1):
                if S[y + 1][x + 1] == '
                    tmp_ans_S += 1
            
            if y < (H - 1):
                if S[y + 1][x] == '
                    tmp_ans_S += 1
            
            if x > 0 and y < (H - 1):
                if S[y + 1][x - 1] == '
                    tmp_ans_S += 1
        
        ans_S[y][x] = tmp_ans_S

for ans in ans_S:
    print(*ans, sep='')

Solution (Python):
h,w = map(int,input().split())
s =["."*(w+2)]+["."+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] !="
            a=s[i][j-1]+s[i][j+1]+s[i-1][j-1:j+2]+s[i+1][j-1:j+2]
            s[i]=s[i][:j]+str(a.count("

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

Solution (Python):
h,w = map(int,input().split())
s =["."*(w+2)]+["."+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] !="
            a=s[i][j-1]+s[i][j+1]+s[i-1][j-1:j+2]+s[i+1][j-1:j+2]
            s[i]=s[i][:j]+str(a.count("

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

Solution (Python):
if __name__ == '__main__':
    h, w = map(int, input().split())
    maiz = []
    count =[]
    for i in range(h):
        a = input()
        maiz.append(a)
        count.append([0]*w)

    for i in range(h):
        for j in range(w):
            if maiz[i][j] == "
                count[i][j] = "
                if i > 0 and maiz[i-1][j] == ".":
                    count [i-1][j] +=1
                if i < h -1  and maiz[i+1][j] == ".":
                    count [i+1][j] +=1
                if j > 0 and maiz[i][j-1] == ".":
                    count [i][j-1] +=1
                if j < w -1  and maiz[i][j+1] == ".":
                    count [i][j+1] +=1
                if i > 0 and j > 0 and maiz[i-1][j-1] == ".":
                    count [i-1][j-1] +=1
                if i < h -1 and j < w-1  and maiz[i+1][j+1] == ".":
                    count [i+1][j+1] +=1
                if i > 0 and j < w-1 and maiz[i-1][j+1] == ".":
                    count [i-1][j+1] +=1
                if i < h -1 and j >0  and maiz[i+1][j-1] == ".":
                    count [i+1][j-1] +=1


    for i in count:
        ans = ""
        for j in i :
            ans += str(j)
        print(ans)

Solution (Python):
H,W=map(int,input().split())
S=[list(input()+'.') for i in range(H)]
S.append(['.']*(W+1))
a=[[0]*W for i in range(H)]
for y in range(H):
    for x in range(W):
        if S[y][x]=='.':
            a[y][x]=len([1 for i in range(-1,2) for j in range(-1,2) if S[i+y][j+x]=='
        else: a[y][x]='
for i in a:print(''.join(list(map(str,i))))

Solution (Python):



from fractions import gcd
from itertools import combinations,permutations,accumulate 

from collections import deque,defaultdict,Counter
import decimal
import re











import sys
sys.setrecursionlimit(10000000)
mod = 10**9 + 7

def readInts():
  return list(map(int,input().split()))
def I():
  return int(input())


h,w = readInts()
dist = [['0'] * w for _ in range(h)]
maze = [input() for _ in range(h)]
dx = [-1,-1,-1,0,0,1,1,1]
dy = [-1,0,1,-1,1,-1,0,1]

def bfs(y,x):
    dist[y][x] = '
    for i in range(8):
        nx = x + dx[i]
        ny = y + dy[i]
        if not(0 <= nx < w and 0 <= ny < h):
            continue
        if maze[ny][nx] == '
            continue
        dist[ny][nx] = str(int(dist[ny][nx]) + 1)


for y in range(h):
    for x in range(w):
        if maze[y][x] == '
            bfs(y,x)
for i in range(h):
    print("".join(dist[i]),sep = '')

Solution (Python):
H, W = map(int, input().split())
S = [list(input()) for i in range(H)]
A = [['
for i in range(H):
    for j in range(W):
        if S[i][j] == '.':
            a = 0
            if i > 0 and j > 0:
                if S[i - 1][j - 1] == '
                    a += 1
            if i > 0:
                if S[i - 1][j] == '
                    a += 1
            if i > 0 and j < W - 1:
                if S[i - 1][j + 1] == '
                    a += 1
            if j > 0:
                if S[i][j - 1] == '
                    a += 1
            if j < W - 1:
                if S[i][j + 1] == '
                    a += 1
            if i < H - 1 and j > 0:
                if S[i + 1][j - 1] == '
                    a += 1
            if i < H - 1:
                if S[i + 1][j] == '
                    a += 1
            if i < H - 1 and j < W - 1:
                if S[i + 1][j + 1] == '
                    a += 1
            A[i][j] = a
for i in range(H):
    ans = [str(a) for a in A[i]]
    ans = ''.join(ans)
    print(ans)

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

bom = []
for i in range(H):
    for j in range(W):
        if s[i][j] == ".": s[i][j] = 0
        else: bom.append([i,j])

for i in range(len(bom)):
    h,w = bom[i][0], bom[i][1]
    for x,y in ([1,0], [-1,0], [0,1], [0,-1], [1,1], [-1,-1], [1,-1], [-1,1]):
        new_h, new_w =  h+x, w+y
        if new_h<0 or new_h>=H or new_w<0 or new_w>=W: continue
        if s[new_h][new_w] != "
            s[new_h][new_w] += 1
for ans in s:
    print("".join(list(map(str, ans))))

Solution (Python):
H,W = map(int,input().split())
S = ["."*(W+2)]
for i in range(H):
    x=input().strip()
    x = "."+x+"."
    S.append(x)
S.append("."*(W+2))
A = [[0 for _ in range(W+1)] for _ in range(H+1)]
for i in range(1,H+1):
    for j in range(1,W+1):
        if S[i][j]=="
            A[i][j] = "
        else:
            cnt = 0
            if S[i][j+1]=="
                cnt += 1
            if S[i-1][j+1]=="
                cnt += 1
            if S[i-1][j]=="
                cnt += 1
            if S[i-1][j-1]=="
                cnt += 1
            if S[i][j-1]=="
                cnt += 1
            if S[i+1][j-1]=="
                cnt += 1
            if S[i+1][j]=="
                cnt += 1
            if S[i+1][j+1]=="
                cnt += 1
            A[i][j]=str(cnt)
    print("".join(A[i][1:W+1]))

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

di = [1, -1, 0, 0, 1, -1, 1, -1]
dj = [0, 0, 1, -1, 1, -1, -1, 1]

for i in range(h):
    for j in range(w):
        if s[i][j] == '
            continue
        cnt = 0
        for ii, jj in zip(di, dj):
            ni = i + ii
            nj = j + jj
            if 0 <= ni < h and 0 <= nj < w:
                if s[ni][nj] == '
                    cnt += 1
        s[i][j] = chr(ord('0') + cnt)

for x in s:
    print(''.join(x))

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

T = [[0]*(W+2) for _ in range(H+2)]

for i in range(H):
    for j in range(W):
        if S[i][j] == "
            T[i+1][j+1] = 9

for i in range(1,H+1):
    for j in range(1,W+1):
        if T[i][j] >= 9:
            T[i-1][j-1] += 1
            T[i-1][j+0] += 1
            T[i-1][j+1] += 1
            T[i+0][j-1] += 1
            T[i+0][j+0] += 1
            T[i+0][j+1] += 1
            T[i+1][j-1] += 1
            T[i+1][j+0] += 1
            T[i+1][j+1] += 1

for i in range(1,H+1):
    ans = ""
    for j in range(1,W+1):
        if T[i][j] >= 9:
            T[i][j] = "
        ans += str(T[i][j])
    print(ans)

Solution (Python):
import sys
import math
import itertools as it
def I():return int(sys.stdin.readline().replace("\n",""))
def I2():return map(int,sys.stdin.readline().replace("\n","").split())
def S():return str(sys.stdin.readline().replace("\n",""))
def L():return list(sys.stdin.readline().replace("\n",""))
def Intl():return [int(k) for k in sys.stdin.readline().replace("\n","").split()]
def Lx(k):return list(map(lambda x:int(x)*-k,sys.stdin.readline().replace("\n","").split()))

if __name__ == "__main__":
    h,w = I2()
    s = [L() for _ in range(h)]
    dx = [1,0,-1,0,1,-1,-1,1]
    dy = [0,1,0,-1,1,1,-1,-1]
    x,y = 0,0
    for i in range(h):
        for j in range(w):
            if s[i][j] == "
            cnt = 0
            for k in range(8):
                x = j+dx[k]
                y = i+dy[k]
                if (y < 0 or h <= y):continue
                if (x < 0 or w <= x):continue
                if s[y][x] == "
            s[i][j] = cnt
    for i in s:
        for j in i:
            print(j,end="")
        print()

Solution (Python):
Height, Width = map(int, input().split())

minse=[]

for i in range(Height):
    mine = list(input())
    minse.append(mine)

ans_list = [[0 for j in range(Width)] for i in range(Height)]


for h in range(Height):
    for w in range(Width):
        me = minse[h][w]
        if (me == '

            for i in range(3):
                for j in range(3):
                    if (i != 1 or j != 1):
                        index_h = h - 1 + j
                        index_w = w - 1 + i
                        if (index_h < Height and index_h >=0 and index_w>=0 and index_w < Width):
                            ans_list[index_h][index_w] += 1
                            

for i in range(Height):
    for j in range(Width):
        value = minse[i][j]
        if (value == '
            ans_list[i][j]='

for i in range(Height):
    tmp = ans_list[i]
    print(''.join(map(str,tmp)))

Solution (Python):
h, w = map(int, input().split())
field = [[*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 field[i][j] == '
            continue

        cnt = 0
        for d in range(8):
            xi = i + dx[d]
            yj = j + dy[d]

            if xi < 0 or xi > h-1 or yj < 0 or yj > w-1:
                continue

            if field[xi][yj] == '
                cnt += 1
        
        field[i][j] = cnt

for i in range(h):
    print(*field[i], sep='')

Solution (Python):




import sys
from fractions import gcd
import math
from math import ceil,floor
import collections
from collections import Counter
import itertools
import copy














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

for i in range(1,H+1):
    for j in range(1,W+1):
        if S[i][j] == "
            print("
        else:
            Stmp = 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]
            print(Stmp.count("
    print()

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

ans = [[0]*w for _ in range(h)]
for i in range(h):
  for j in range(w):
    if s[i][j] == "
      ans[i][j] = "
      for i2 in range(i-1, i+2):
        if i2 < 0 or h-1 < i2: continue
        for j2 in range(j-1, j+2):
          if (j2 < 0 or w-1 < j2): continue
          elif ans[i2][j2] == "
          ans[i2][j2] += 1
for i in ans:
  print("".join(map(str,i)))

Solution (Python):
import sys

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

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

def ii():
    return int(input())


def main():
    H, W = mi()
    S = ['w'*(W+2)]+['w'+input()+'w' for i in range(H)]+['w'*(W+2)]
    ans = [[0]*W for i in range(H)]

    for j in range(1, W+1):
        for i in range(1, H+1):
            if S[i][j] == '
                ans[i-1][j-1] = '
            else:
                t = 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]
                ans[i-1][j-1] = str(t.count('
    print(*(''.join(ans[i]) for i in range(H)), sep='\n')



if __name__ == '__main__':
    main()

Solution (Python):
def main():
    H,W = map(int,input().split())
    S = list()
    INF = 10**18
    S.append([-INF]*(W+2))
    for i in range(H):
        line = list(input())
        for j,c in enumerate(line):
            if c == '.':
                line[j] = 0
            else:
                line[j] = -INF

        S.append([-INF]+line+[-INF])
    S.append([-INF]*(W+2))
    for i in range(1,H+1):
        for j in range(1,W+1):
            if S[i][j] < 0:
                S[i-1][j-1] += 1
                S[i-1][j] += 1
                S[i-1][j+1] += 1
                S[i][j-1] += 1
                S[i][j+1] += 1
                S[i+1][j-1] += 1
                S[i+1][j] += 1
                S[i+1][j+1] += 1
    
    out = ''
    for i in range(1,H+1):
        for j in range(1,W+1):       
            out += '
        out += '\n'
    print(out)
                
main()

Solution (Python):










import sys

def input(): return sys.stdin.readline()
def iIn(): return int(input())
def iInM(): return map(int, input().split())
def iInM1(): return map(int1, input().split())
def iInLH(): return list(map(int, input().split()))
def iInLH1(): return list(map(int1, input().split()))
def iInLV(n): return [iIn() for _ in range(n)]
def iInLV1(n): return [iIn()-1 for _ in range(n)]
def iInLD(n): return [iInLH() for _ in range(n)]
def iInLD1(n): return [iInLH1() for _ in range(n)]
def sInLH(): return list(input().split())
def sInLV(n): return [input().rstrip('\n') for _ in range(n)]
def sInLD(n): return [sInLH() for _ in range(n)]

def OutH(lst, delimiter=' '): print(delimiter.join(map(str, lst)))
def OutV(lst): print('\n'.join(map(str, lst)))

sys.setrecursionlimit(10 ** 6)

int1 = lambda x: int(x) - 1

INF = int(1e9)
MOD = 1000000007
dx = (-1, 0, 1, 0)
dy = (0, -1, 0, 1)


N_AROUND = 8
ddx = (-1, 0, 1, -1, 1, -1, 0, 1)
ddy = (-1, -1, -1, 0, 0, 1, 1, 1)

H, W = iInM()
S = sInLV(H)


T = [['.'] * (W + 2)]
for i in range(H):
    var = ['.'] + list(S[i]) + ['.']
    T.append(var)
T.append(['.'] * (W + 2))


ans = []
for i in range(1, H+1):
    ans.append([])
    for j in range(1, W+1):
        if T[i][j] == '
            ans[-1].append('
            continue
        cnt = 0
        for k in range(N_AROUND):
            if T[i + ddy[k]][j + ddx[k]] == '
                cnt += 1
        ans[-1].append(str(cnt))
        


for i in range(H):
    OutH(ans[i], delimiter='')

Solution (Python):
H, W = map(int, input().split())
a, b = [], []
ans = 0
for _ in range(H):
    a += [0 if x == '.' else 1 for x in input()]
for i in range(H*W):
    if a[i] == 0:
        if i % W != 0: ans += a[i-1]
        if (i+1) % W != 0: ans += a[i+1]
        if i < (H-1)*W: ans += a[i+W]
        if i < (H-1)*W and i % W != 0: ans += a[i+W-1]
        if i < (H-1)*W and (i+1) % W != 0: ans += a[i+W+1]
        if W <= i: ans += a[i-W]
        if i % W != 0 and W <= i: ans += a[i-W-1]
        if W <= i and (i+1) % W != 0: ans += a[i-W+1]
        b += [str(ans)]
        ans = 0
    else:
        b += ['
        
for i in range(H):
    print(''.join(b[W*i:W*(i+1)]))

Solution (Python):
import sys
H,W = map(int,input().split())
array = []
for I in range(H):
    array.append(list(map(str,input())))

if not (1 <= H <= 50 and 1 <= W <= 50):
    sys.exit()

result = [[0]*(W+2) for i in range((H+2))]

def input_result_array(x,y,result):
    result[x-1][y-1] += 1
    result[x-1][y] += 1
    result[x-1][y+1] += 1
    result[x][y-1] += 1
    result[x][y+1] += 1
    result[x+1][y-1] += 1
    result[x+1][y] += 1
    result[x+1][y+1] += 1
    return result

bomb_point = []
for I in range(H):
    for J in range(W):
        if array[I][J] == '
            bomb_point.append([I,J])
            result = input_result_array(I+1,J+1,result)

for I in range(len(bomb_point)):
    x,y=bomb_point[I]
    result[x+1][y+1] = '

str_result = ''
for I in [I[1:-1] for I in result[1:-1]]:
    for J in I:
        str_result += str(J)
    str_result += '\n'
print(str_result)

Solution (Python):
import sys
import math
import itertools
import bisect
from copy import copy
from collections import deque,Counter
from decimal import Decimal
def s(): return input()
def i(): return int(input())
def S(): return input().split()
def I(): return map(int,input().split())
def L(): return list(input().split())
def l(): return list(map(int,input().split()))
def lcm(a,b): return a*b//math.gcd(a,b)
sys.setrecursionlimit(10 ** 9)
INF = 10**9
mod = 10**9+7

H,W = I()
maze = [list(s()) for _ in range(H)]
ans = [[0]*(W-1) for _ in range(H)]
for i in range(H):
    for j in range(W):
        if maze[i][j] == '.':
            cnt = 0
            Flag1 = 0
            Flag2 = 0
            Flag3 = 0
            Flag4 = 0
            if i != 0:
                if maze[i-1][j] == '
                    cnt += 1
                Flag1 = 1
            if i != H-1:
                if maze[i+1][j] == '
                    cnt += 1
                Flag2 = 1
            if j != 0:
                if maze[i][j-1] == '
                    cnt += 1
                Flag3 = 1
            if j != W-1:
                if maze[i][j+1] == '
                    cnt += 1
                Flag4 = 1
            if Flag1 == Flag3 == 1:
                if maze[i-1][j-1] == '
                    cnt += 1
            if Flag1 == Flag4 == 1:
                if maze[i-1][j+1] == '
                    cnt += 1
            if Flag2 == Flag3 == 1:
                if maze[i+1][j-1] == '
                    cnt += 1
            if Flag2 == Flag4 == 1:
                if maze[i+1][j+1] == '
                    cnt += 1
            maze[i][j] = str(cnt)
for m in maze:
    print(''.join(m))

Solution (Python):
from collections import defaultdict

def readInt():
  return int(input())
def readInts():
  return list(map(int, input().split()))
def readChar():
  return input()
def readChars():
  return input().split()

h,w = readInts()
s = [list(input()) for i in range(h)]

for i in range(h):
  for j in range(w):
    if s[i][j]=="
      if i!=0:
        if j-1>=0:
          if s[i-1][j-1]==".":
            s[i-1][j-1] = 1
          elif s[i-1][j-1]!="
            s[i-1][j-1]+=1
        if s[i-1][j]==".":
          s[i-1][j] = 1
        elif s[i-1][j]!="
          s[i-1][j]+=1
        if j+1<=w-1:
          if s[i-1][j+1]==".":
            s[i-1][j+1] = 1
          elif s[i-1][j+1] != "
            s[i-1][j+1]+=1
      if j-1>=0:
        if s[i][j-1]==".":
          s[i][j-1] = 1
        elif s[i][j-1]!="
          s[i][j-1]+=1
      if j+1<=w-1:
        if s[i][j+1]==".":
          s[i][j+1] = 1
        elif s[i][j+1] != "
          s[i][j+1]+=1
      if i+1<=h-1:
        if j-1>=0:
          if s[i+1][j-1]==".":
            s[i+1][j-1] = 1
          elif s[i+1][j-1]!="
            s[i+1][j-1]+=1
        if s[i+1][j]==".":
          s[i+1][j] = 1
        elif s[i+1][j]!="
          s[i+1][j]+=1
        if j+1<=w-1:
          if s[i+1][j+1]==".":
            s[i+1][j+1] = 1
          elif s[i+1][j+1] != "
            s[i+1][j+1]+=1

for i in s:
  for j in i:
    if j==".":
      print("0",end="")
    else:
      print(j,end="")
  print()

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

for i in range(H):
    for j in range(W):
        if S[i][j] == '.':
            cnt = 0
            for _i in range(max(0, i - 1), min(H, i + 2)):
                for _j in range(max(0, j - 1), min(W, j + 2)):
                    if S[_i][_j] == '
                        cnt += 1
            S[i][j] = str(cnt)
print('\n'.join([''.join(s) for s in S]))

Solution (Python):
def main():
  h,w = list(map(int,input().split()))
  s = [input() for i in range(h)]
  for i in range(0,h):
    for j in range(0,w):
      if s[i][j]=='
        print('
      else:
        count=0
        for k in range(i-1,i+2):
          if k>=0 and k<h:
            for l in range(j-1,j+2):
              if l>=0 and l<w and s[k][l]=='
                count+=1
        print(count,end="")
    print()
    
main()

Solution (Python):
H,W=map(int,input().split())
s=[['.']*(W+2) for i in range(H+2)]
for i in range(H):
  S=input()
  t='.'+S+'.'
  s[i+1]=t
for i in range(H):
  for j in range(W):
    b=0
    if s[i+1][j+1]!='
      for k in range(-1,2,1):
        for l in range(-1,2,1):
          if s[i+1+k][j+1+l]=='
            b+=1
    else:
      b='
    print(b,end='')
  print()

Solution (Python):
import copy
ym,xm=map(int,input().split())
ym-=1
xm-=1
field=[]
for i in range(ym+1):
  field.append(list(input()))
ans=copy.deepcopy(field)
def check(x,y):
  ret=0
  chx=[0]
  chy=[0]
  if x!=0:
    chx.append(-1)
  if x!=xm:
    chx.append(1)
  if y!=0:
    chy.append(-1)
  if y!=ym:
    chy.append(1)
  for px in chx:
    for py in chy:
      if px==0 and py==0:
        continue
      if field[y+py][x+px]=="
        ret+=1
  return ret

for x in range(xm+1):
  for y in range(ym+1):
    if field[y][x]=="
      continue
    ans[y][x]=check(x,y)

for i in ans:
  for j in i:
    print(j,end="")
  print()

Solution (Python):
dy = [-1, 1, 0, 0, -1, 1, -1, 1]
dx = [ 0, 0,-1, 1, -1, 1,  1,-1]
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] == '
        c = 0
        for k in range(8):
            y = i + dy[k]; x = j + dx[k]
            if y<0 or h<=y or x<0 or w<=x: continue
            if S[y][x] == '
        S[i][j] = str(c)
print(*map(''.join, S), sep='\n')

Solution (Python):
dy = [-1, 1, 0, 0, -1, 1, -1, 1]
dx = [ 0, 0,-1, 1, -1, 1,  1,-1]
h, w = map(int, input().split())
S = [input() for _ in range(h)]
ans = [[-1]*w for _ in range(h)]
for i in range(h):
    for j in range(w):
        if S[i][j] == '
        ans[i][j] = 0
        for k in range(8):
            y = i + dy[k]; x = j + dx[k]
            if y<0 or h<=y or x<0 or w<=x: continue
            if S[y][x] == '
for i in range(h):
    for j in range(w): print('
    print('')

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

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

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

for i in range(h):
    for j in range(w):
        if S[i][j] == '
        ans[i][j] = 0
        for k in range(8):
            y = i + dy[k]
            x = j + dx[k]
            if y<0 or h<=y: continue
            if x<0 or w<=x: continue
            if S[y][x] == '
                ans[i][j] += 1

for i in range(h):
    for j in range(w):
        if ans[i][j] == -1:
            print('
        else:
            print(ans[i][j],end='')
    print('')

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

for x in range(h):
    for y in range(w):
        if maze[x][y] == "
            bomb[x][y] = "
            continue

        for dx, dy in [(0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1), (-1, 0), (-1, 1)]:
            nx = x + dx
            ny = y + dy 
            if not 0 <= nx < h or not 0 <= ny < w:
                continue
            if maze[nx][ny] == "
                bomb[x][y] += 1
               



for i in range(h):
    ans = ""
    for j in range(w):     
        ans +=str( bomb[i][j])
    print(ans)