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())
data=[list(input()) for i in range(H)]
for w in range(W):
    for h in range(H):
        if data[h][w]==".":
            xlist=[0]
            if w!=0:xlist+=[-1]
            if w!=W-1:xlist+=[1]
            ylist=[0]
            if h!=0:ylist+=[-1]
            if h!=H-1:ylist+=[1]
            data[h][w]=str([data[h+y][w+x] for x in xlist for y in ylist].count("
for i in data:
    print("".join(i))

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

def count(T,h,w):
    re=0
    re+=[T[h+1][w],T[h+1][w+1],T[h+1][w-1],T[h][w+1],T[h][w-1],\
         T[h-1][w],T[h-1][w+1],T[h-1][w-1]].count('
    return re

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

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

mines=['' for _ in range(h)]



for row in range(h):
    for col in range(w):
        if board[row][col] != '
            counts = 0
            dx = [x for x in range(-1, 2) if (col+x>=0 and col+x<w)]
            dy = [y for y in range(-1, 2) if (row+y>=0 and row+y<h)]
            
            for x in dx:
                for y in dy:
                    
                    if board[row+y][col+x] == '
                        counts += 1
            mines[row] += str(counts)
        else:
            mines[row] += '
for mine in mines:
    print(mine)

Solution (Python):
from collections import Counter,defaultdict,deque
from heapq import heapify,heappop,heappush
from bisect import bisect_left,bisect_right
import sys,math,itertools,string,queue
sys.setrecursionlimit(10**8)
mod = 10**9+7
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpl_str(): return list(sys.stdin.readline().split())
def inpln(n): return list(int(sys.stdin.readline()) for i in range(n))

def check(x,y):
    res = 0
    for j in dy:
        for i in dx:
            if s[y][x] == '
                return '
            if x + i >= w or y + j >= h or x + i < 0 or y + j < 0:
                continue
            if s[y+j][x+i] == '
                res += 1
    return res
dx = [-1,0,1]
dy = [-1,0,1]
h,w = inpl()
s = [''] * h
for i in range(h):
    s[i] = input()
res = []
for y in range(h):
    L = ''
    for x in range(w):
        L += str(check(x,y))
    res.append(L)
for i in range(h):
    print(res[i])

Solution (Python):
h,w=[int(i) for i in input().split()]
s=[input() for i in range(h)]
ans=""
for i in range(h):
    for j in range(w):
        cnt=0
        if s[i][j]=="
            ans+="
        else:
            for y,x in [(i+1,j-1),(i+1,j),(i+1,j+1),(i-1,j-1),(i-1,j),(i-1,j+1),(i,j+1),(i,j-1)]:
                if x>=0 and y>=0 and y<h and x<w:
                    if s[y][x]=="
                        cnt+=1
            ans+=str(cnt)
    ans+="\n"
print(ans)

Solution (Python):
H, W = map(int, input().split())
Slist = [["."] * (W + 2), ]
for _ in range(H):
    line = list(input())
    line.insert(0, ".")
    line.append(".")
    Slist.append(line)
Slist.append(["."] * (W + 2))

point_tuple = ((-1, -1), (-1, 0), (-1, 1),
               (0, -1), (0, 1),
               (1, -1), (1, 0), (1, 1))
for i in range(1, H + 1):
    for j in range(1, W + 1):
        
        if Slist[i][j] == ".":
            counter = 0
            for a, b in point_tuple:
                if Slist[i - a][j - b] == "
                    counter += 1
            Slist[i][j] = str(counter)

for i in range(1, H + 1):
    print("".join(Slist[i][1:-1]))

Solution (Python):
import sys
from collections import deque

h, w = (int(x) for x in sys.stdin.readline().split())
strings = deque(sys.stdin.read().split())

for i in range(h):
    strings[i] = ''.join(['-', strings[i], '-'])
strings.append('-' * (w + 2)); strings.appendleft('-' * (w + 2))

ans = []
for i in range(1, h + 1):
    s = ''
    for j in range(1, w + 1):
        current = strings[i][j]
        if current == '
        elif current == '.':
            count = 0
            for t in range(i-1, i+2):
                for u in range(j-1, j+2):
                    if strings[t][u] == '
            s += str(count)
    ans.append(s)

for s in ans: print(s)

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

s_list = []
for i in range(H):
    s_list.append(input())
    
out_list = []

eight_dir = ((1, 0), (1, 1), (0, 1), (-1, 1), (-1, 0), (-1, -1), (0, -1), (1, -1))

for i in range(H):
    cur_str = ''
    for j in range(W):
        if s_list[i][j] == '
            cur_str += "
        else:
            count = 0
            for k in range(8):
                search_x = j + eight_dir[k][1]
                search_y = i + eight_dir[k][0]
                if 0 <= search_x < W and 0 <= search_y < H:
                    if s_list[search_y][search_x] == '
                        count += 1
            cur_str += str(count)

    print(cur_str)

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


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

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

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

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

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

Solution (Python):
H,W=map(int,input().split())
S=[input() for _ in range(H)]
A=[]
for i in range(H):
    tmp=''
    for j in range(W):
        if S[i][j]=='
            tmp+='
        else:
            cnt=0
            for m in range(max(i-1,0),min(i+1,H-1)+1):
                for n in range(max(j-1,0),min(j+1,W-1)+1):
                    if S[m][n]=='
                        cnt+=1
            tmp+=str(cnt)
    A.append(tmp)
for a in A:
    print(a)

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


def check_num(i, j):
    a = i - 1
    b = i + 1
    c = j - 1
    d = j + 1
    if i == 0:
        a = i
    if i == h-1:
        b = i
    if j == 0:
        c = j
    if j == w-1:
        d = j
    count = 0
    for p in range(a, b+1):
        for q in range(c, d+1):
            if p == i and q == j:
                continue
            if mine_area[p][q] == '
                count += 1
    return count


for _ in range(h):
    mine_line = list(input())
    mine_area.append(mine_line)

for i in range(h):
    for j in range(w):
        if mine_area[i][j] == '
            mine_area[i][j] = '
        else:
            mine_area[i][j] = check_num(i, j)

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

Solution (Python):

import numpy as np
h, w = map(int, input().rstrip("\n").split())

array = np.array([[" "] * (w + 2)] + [list(" " + input().rstrip("\n") + " ") for _ in range(h)] + [[" "] * (w + 2)])

list_ = []

for i in range(1, h + 1):
    for j in range(1, w + 1):
        if array[i][j] == "
            list_.append("
        else:
            list_.append(str(sum(sum(array[i - 1:i + 2, j - 1:j + 2] == "
    list_.append("\n")
list_.pop()

print("".join(list_))

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

temp = ['.'*(w+2)]+['.'+input()+'.' for _ in range(h)]+['.'*(w+2)]



ans_list = []

for h_i in range(1, h+1):
  w_list = []
  for w_i in range(1, w+1):
    if temp[h_i][w_i] == '.':
      top_1 = temp[h_i-1][w_i-1]
      top_2 = temp[h_i-1][w_i]
      top_3 = temp[h_i-1][w_i+1]
      left = temp[h_i][w_i-1]
      right = temp[h_i][w_i+1]
      bottom_1 = temp[h_i+1][w_i-1]
      bottom_2 = temp[h_i+1][w_i]
      bottom_3 = temp[h_i+1][w_i+1]

      count = [top_1,top_2,top_3,left,right,bottom_1,bottom_2,bottom_3].count('
      w_list.append(count)
    else:
      w_list.append('
   
  ans_list.append(w_list)

for ans_i in ans_list:print(*ans_i, sep='')

Solution (Python):
H, W = map(int, input().split())
g = [list("." * (W + 2))]
for _ in range(H):
    g += [list("." + input() + ".")]
g += [list("." * (W + 2))]

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

for i in range(1, H + 1):
    for j in range(1, W + 1):
        if g[i][j] == ".":
            c = 0
            for (dx, dy) in d:
                if g[i + dx][j + dy] == "
                    c += 1
            g[i][j] = c
for i in range(1, H + 1):
    print(*g[i][1:(W + 1)], sep="")

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

for y in range(h):
	for x in range(w):
		if fd[y][x] == '
			continue
		c = 0
		
		for k in range(8):
			ny = y + dy[k]
			nx = x + dx[k]
			
			if ny < 0 or h <= ny or nx < 0 or w <= nx:
				continue
				
			if fd[ny][nx] == '
				c += 1
		fd[y][x] = str(c)
		
for y in range(h):
	print(''.join(fd[y]))

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] == '.':
            c = 0
            for k in range(max(i-1, 0), min(i+1, h-1)+1):
                for l in range(max(j-1, 0), min(j+1, w-1)+1):
                    if s[k][l] == '
                        c += 1
            s[i][j] = str(c)

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

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

import itertools

for i in range(h):
  ans=""
  for j in range(w):
    if s[i][j]=="
      ans+="
      continue
    temp=0
    for dx,dy in itertools.product([-1,0,1],repeat=2):
      if i+dx<0 or i+dx>=h:
        continue
      if j+dy<0 or j+dy>=w:
        continue
      if s[i+dx][j+dy]=="
        temp+=1
    ans+=str(temp)
  print(ans)

Solution (Python):
h, w = map(int, input().split())
S=[]
S.append(["."] * (w + 2))
for i 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):
    counter = 0
    if S[i][j] == ".":
      if S[i-1][j-1] == "
        counter += 1
      if S[i-1][j] == "
        counter += 1
      if S[i-1][j+1] == "
        counter += 1
      if S[i][j-1] == "
        counter += 1
      if S[i][j+1] == "
        counter += 1
      if S[i+1][j-1] == "
        counter += 1
      if S[i+1][j] == "
        counter += 1
      if S[i+1][j+1] == "
        counter += 1
      S[i][j] = counter

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

Solution (Python):

import sys
from collections import defaultdict

H, W = (int(i) for i in sys.stdin.readline().rstrip().split())
S = (sys.stdin.readline().rstrip() for _ in range(H))

dd = {}
for i, x in enumerate(S):
  for j, y in enumerate(x):
    dd[(i, j)] = 1 if y == '

all_pos = {}
for pos in sorted(dd.keys()):
  x, y = pos
  state = 0
  state += dd.get((x + 1, y), 0)
  state += dd.get((x - 1, y), 0)
  state += dd.get((x, y + 1), 0)
  state += dd.get((x, y - 1), 0)
  state += dd.get((x + 1, y + 1), 0)
  state += dd.get((x - 1, y - 1), 0)
  state += dd.get((x + 1, y - 1), 0)
  state += dd.get((x - 1, y + 1), 0)
  all_pos[(x, y)] = state
  
for i in range(H):
  for j in range(W):
    if dd[(i, j)]:
      print('
    else:
      print(all_pos[(i, j)], end='')
  print()

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

def check_bomb(x, y):
    cnt = 0
    if S[y][x] == '
        return '
    if x-1 >= 0:
        if S[y][x-1] == '
            cnt += 1
    if x+1 < W:
        if S[y][x+1] == '
            cnt += 1
    if y-1 >= 0:
        if x-1 >= 0:
            if S[y-1][x-1] == '
                cnt += 1
        if x+1 < W:
            if S[y-1][x+1] == '
                cnt += 1
        if S[y-1][x] == '
                cnt += 1
    if y+1 < H:
        if x-1 >= 0:
            if S[y+1][x-1] == '
                cnt += 1
        if x+1 < W:
            if S[y+1][x+1] == '
                cnt += 1
        if S[y+1][x] == '
                cnt += 1
    return str(cnt)

for y in range(H):
    for x in range(W):
        print(check_bomb(x, y), end='')
    print('')

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

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

result = []
for temp_h in range(h):
  temp_s = ""
  for temp_w in range(w):
    if list_s[temp_h][temp_w] == "
      temp_s += "
      continue
    temp_num = 0
    for one_pair in list_pair:
      check_h = temp_h + one_pair[0]
      check_w = temp_w + one_pair[1]
      if 0 <= check_h < h and 0 <= check_w < w:
        if list_s[check_h][check_w] == "
          temp_num += 1
    temp_s += str(temp_num)
  result.append(temp_s)

for one_line in result:
  print(one_line)

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 i in range(H):
    for j in range(W):
        cnt = 0
        if S[i][j] == "
            continue
        
        for k in range(len(dx)):
            if i+dx[k] < 0 or i+dx[k] >= H:
                continue
            if j+dy[k] < 0 or j+dy[k] >= W:
                continue
            if S[i+dx[k]][j+dy[k]] == "
                cnt += 1
        S[i][j] = str(cnt)
for i in range(H):
    print("".join(S[i]))

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

cnt=[[0 for i in range(w+2)] for j in range(h+2)]

for i in range(h):
    for j in range(w):
        if s[i][j]=='
            cnt[i][j]+=1
            cnt[i][j+1]+=1
            cnt[i][j+2]+=1
            cnt[i+1][j]+=1
            cnt[i+1][j+2]+=1
            cnt[i+2][j]+=1
            cnt[i+2][j+1]+=1
            cnt[i+2][j+2]+=1

ans=[['' for i in range(w)] for j in range(h)]

for i in range(h):
    for j in range(w):
        if s[i][j]=='
            ans[i][j]='
        else:
            ans[i][j]=str(cnt[i+1][j+1])

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

Solution (Python):
h, w = map(int, input().split())
s = []
bombs= []
for _h in range(h):
    _s = input()
    for _w in range(w):
        if _s[_w] == "
            bombs.append((_h, _w))
    s.append(_s)

a = [[0]*(w+1) for _ in range(h+1)]

for _h, _w in bombs:
        a[_h-1][_w-1] += 1
        a[_h-1][_w] += 1
        a[_h-1][_w+1] += 1

        a[_h][_w-1] += 1
        a[_h][_w+1] += 1

        a[_h+1][_w-1] += 1
        a[_h+1][_w] += 1
        a[_h+1][_w+1] += 1

for _h, _w in bombs:
    a[_h][_w] = "
a.pop(-1)
for _a in a:
    _a.pop(-1)
    print("".join(map(str, _a)))

Solution (Python):
def count(i, j):
  count = 0
  for k in range(-1, 2):
    for l in range(-1, 2):
      if (k, l) != (0, 0) \
        and 0 <= i+k < H \
        and 0 <= j+l < W \
        and S[i+k][j+l] == '
        count += 1
  return count

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

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

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

Solution (Python):
h,w=map(int,input().split());s=[list(input()) for _ in range(h)]
for i in range(h):
    moji = ''
    for j in range(w):
        cnt = 0
        if s[i][j] == '
            moji += '
        else:
            for ih in [-1, 0, 1]:
                for jw in [-1, 0, 1]:
                    if 0 <= i + ih < h and 0 <= j + jw < w:
                        if ih == 0 and jw == 0:
                            pass
                        elif s[i + ih][j + jw] == '
                            cnt += 1
                        else:
                            pass
            moji += str(cnt)
    print(moji)

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

def check_bomb(i,j):
    if i<0 or j<0:
        return 0
    try:
        if H_lis[i][j]==".":
            return 0
        elif H_lis[i][j]=="
            return 1
        else:
            return 0
    except:
        return 0

for i in range(H):
    ans = ""
    for j in range(W):
        if check_bomb(i,j)==1:
            ans += "
        else:
            ans+=str(check_bomb(i-1,j-1)+check_bomb(i-1,j)+check_bomb(i-1,j+1)+check_bomb(i,j-1)+check_bomb(i,j+1)+check_bomb(i+1,j-1)+check_bomb(i+1,j)+check_bomb(i+1,j+1))
    print(ans)

Solution (Python):
H,W=map(int,input().split())
s=[list(input()) for i in range(H)]
for i in range(H):
  ans=["0"]*W
  for j in range(W):
    if s[i][j]=="
      ans[j]="
      continue
    count=0
    if i > 0:
      if s[i-1][j]=="
        count+=1
      if j > 0:
        if s[i-1][j-1]=="
          count+=1
      if j < W-1:
        if s[i-1][j+1]=="
          count+=1
    if i<H-1:
      if s[i+1][j]=="
        count+=1
      if j >0:
        if s[i+1][j-1]=="
          count+=1
      if j<W-1:
        if s[i+1][j+1]=="
          count+=1
    if j>0:
      if s[i][j-1]=="
        count+=1
    if j<W-1:
      if s[i][j+1]=="
        count+=1
    ans[j]=str(count)
  print("".join(ans))

Solution (Python):
def is_in(min, i, max):
    if i < min:
        ret = False
    elif max <= i:
        ret = False
    else:
        ret = True
    return ret


def main():
    height, width = map(int, input().split())

    matrix = []
    for i in range(height):
        temp_s = input()
        matrix.append(list(temp_s))

    ans_matrix = []
    for i in range(height):
        temp_row = []
        for j in range(width):
            if matrix[i][j] == '
                string = '
            else:
                count = 0
                for x in range(-1, 2, 1):
                    for y in range(-1, 2, 1):
                        if x == 0 and y == 0:
                            continue

                        if is_in(0, i+x, height) and is_in(0, j+y, width):
                            temp = matrix[i+x][j+y]
                            if temp == '
                                count += 1
                string = str(count)
            temp_row.append(string)
        ans_matrix.append(temp_row[:])
    for i in range(height):
        print(''.join(ans_matrix[i]))
    print()


if __name__ == '__main__':
    main()

Solution (Python):
H = 0
W = 0
Data = []
Ans  = []

def check(h,w):
    if Data[h][w] == '
        return '

    num = 0
    dx = [-1,-1,-1,0 ,0, 1,1,1]
    dy = [-1, 0, 1,-1,1,-1,0,1]
    for i in range(8):
        if Data[h+dx[i]][w+dy[i]] == '
            num += 1
    return str(num)

H,W = map(int,input().split())
Data.append([0]*(W+2))
for _ in range(H):
    s = list(input())
    s.insert(0,0)
    s.append(0)
    Data.append(s)
Data.append([0]*(W+2))

for h in range(1,H+1):
    s = ''
    for w in range(1,W+1):
        s += check(h,w)
    Ans.append(s)

for a in Ans:
    print(a)

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

for i in range(H):
    A = ""
    for j in range(W):
        if S[i][j] == "
            A += "
        else:
            C = 0
            for SS in S[max(0,i-1):min(H,i+2)]:
                C += SS[max(0,j-1):min(W,j+2)].count("
            A += str(C)
    print(A)

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

Solution (Python):
H,W=map(int,input().split())
S=[[0 for i in range(W)] for j in range(H)]
for i in range(H):
  a=list(map(str,input()))
  for j in range(W):
    S[i][j]=a[j]
  a=[]
V=[[0 for i in range(W)] for j in range(H)]
for m in range(H):
  for n in range(W):
    if S[m][n]=="
      V[m][n]="
    if m>0 and n>0 and S[m-1][n-1]=="
      if S[m][n]=="
        V[m][n]="
      else:
        V[m][n] += 1
    if m>0 and S[m-1][n]=="
      if S[m][n]=="
        V[m][n]="
      else:
        V[m][n] += 1
    if W-1>n and m>0 and S[m-1][n+1]=="
      if S[m][n]=="
        V[m][n]="
      else:
        V[m][n] += 1
    if n>0 and S[m][n-1]=="
      if S[m][n]=="
        V[m][n]="
      else:
        V[m][n] += 1
    if W-1>n and S[m][n+1]=="
      if S[m][n]=="
        V[m][n]="
      else:
        V[m][n] += 1
    if H-1>m and n>0 and S[m+1][n-1]=="
      if S[m][n]=="
        V[m][n]="
      else:
        V[m][n] += 1
    if H-1>m and S[m+1][n]=="
      if S[m][n]=="
        V[m][n]="
      else:
        V[m][n] += 1
    if W-1>n and H-1>m and S[m+1][n+1]=="
      if S[m][n]=="
        V[m][n]="
      else:
        V[m][n] += 1
a=[]
for k in range(H):
  for l in range(W):
    a.append(V[k][l])
  print("".join(map(str,a)))
  a=[]

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

l = [[-1, -1], [-1, 0], [-1, 1], [0, -1], [0, 1], [1, -1], [1, 0], [1, 1]]

for i in range(H):
    for j in range(W):
        c = 0
        if S[i][j] == "
            continue
        for k in range(8):
            x = j + l[k][0]
            y = i + l[k][1]
            if x < 0 or x >= W or y < 0 or y >= H:
                continue
            
            if S[y][x] == '
                c += 1
        S[i][j] = str(c)
        
for i in range(H):
    print(''.join(S[i]))

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

V=[[0 for i in range(W)] for j in range(H)]
for m in range(H):
  for n in range(W):
    if S[m][n]=="
      V[m][n]="
      continue
      
    if m > 0 and n > 0 and S[m-1][n-1]=="
      V[m][n] += 1
    if m > 0 and S[m-1][n]=="
      V[m][n] += 1
    if m > 0 and n < W - 1 and S[m-1][n+1]=="
      V[m][n] += 1
    if n > 0 and S[m][n-1]=="
      V[m][n] += 1
    if n < W - 1 and S[m][n+1]=="
      V[m][n] += 1
    if m < H - 1 and n > 0 and S[m+1][n-1]=="
      V[m][n] += 1
    if m < H - 1 and S[m+1][n]=="
      V[m][n] += 1
    if m < H - 1 and n < W - 1 and S[m+1][n+1]=="
      V[m][n] += 1

a=[]

for k in range(H):
  for l in range(W):
    a.append(V[k][l])
  print("".join(map(str,a)))
  a=[]

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

s = []

for _ in range(h):
    temp = []
    for i in input():
        if i == '
            temp.append(-100)
        else:
            temp.append(0)
    temp = [0] + temp + [0]
    s.append(temp)

s = [[0] * (w+2)] + s + [[0] * (w+2)]

for i in range(h+2):
    for j in range(w+2):
        if s[i][j] < 0:
            s[i-1][j-1] += 1
            s[i][j-1] += 1
            s[i+1][j-1] += 1
            s[i-1][j] += 1
            s[i+1][j] += 1
            s[i-1][j+1] += 1
            s[i][j+1] += 1
            s[i+1][j+1] += 1

s = s[1:-1]

ans = []

for i in s:
    temp = ''
    for j in i[1:-1]:
        if j < 0:
            temp += "
        else:
            temp += str(j)
    ans.append([temp])

for i in ans:
    print(*i)

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

for i in range(1,h+1):
  v=[]
  for j in range(1,w+1):
    x=[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]]
    if s[i][j]=='
      v.append('
    else:
      v.append(x.count('
  
  print(*v, sep='')

Solution (Python):
import sys
import os

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

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

            if 0 <= i-1 and i-1 <= H-1 and\
               0 <= j-1 and j-1 <= W-1 and S[i-1][j-1] == "
                    cnt +=1
            if 0 <= i+1 and i+1 <= H-1 and\
               0 <= j-1 and j-1 <= W-1 and S[i+1][j-1] == "
                    cnt +=1
            if 0 <= i-1 and i-1 <= H-1 and\
               0 <= j+1 and j+1 <= W-1 and S[i-1][j+1] == "
                    cnt +=1
            if 0 <= i+1 and i+1 <= H-1 and\
               0 <= j+1 and j+1 <= W-1 and S[i+1][j+1] == "
                    cnt +=1
            print(cnt,end="")
            cnt = 0
        else:
            print("
        if j == W-1:
            print()

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

h,w=(int(x) for x in input().split())

sl=[]

for _ in range(h):
    s = input()
    sl.append(s[:-1])

for i in range(h):
    for j in range(w):
        mc=0
        
        if sl[i][j]!="
            for k in range(-1,2):
                
                for l in range(-1,2):
                    if (i==0 and k==-1) or (j==0 and l==-1) or (i==h-1 and k==1) or (j==w-1 and l==1):
                        continue
                    
                    if sl[i+k][j+l]=="
                        mc+=1
                        
                if (i==0 and k==-1) or (j==0 and l==-1) or (i==h and k==1) or (i==w and l==1):
                        continue
        if j == 0:
            sl[i]=sl[i][j].replace('.',str(mc))+sl[i][j+1:]
        elif j == w-1:
            sl[i]=sl[i][:j]+sl[i][j].replace('.',str(mc))
        else:
            
            sl[i]=sl[i][:j]+sl[i][j].replace('.',str(mc))+sl[i][j+1:]
        



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

Solution (Python):
h,w=map(int,input().split())
m=[]
m.append(['x']*(w+2))
for i in range(h):
    s=list(input())
    m.append(['x']+s+['x'])
m.append(['x']*(w+2))

for i in range(1,h+1):
    for j in range(1,w+1):
        if m[i][j]=='.':
            c=0
            for a in [[-1,-1],[-1,0],[-1,1],[0,-1],[0,1],[1,-1],[1,0],[1,1]]:
                if m[i+a[0]][j+a[1]]=='
                    c+=1
            m[i][j]=str(c)
for i in range(1,h+1):
    ans=''
    for j in range(1,w+1):
        ans+=m[i][j]
    print(ans)

Solution (Python):
H,W = map(int, input().split())
m = [input() for _ in range(H)]
ans_m=[[0]*W for _ in range(H)]
dhw = [(1,0),(-1,0),(0,1),(0,-1),(1,1),(-1,-1),(-1,1),(1,-1)]
def main():
  global ans_m
  for h in range(H):
    for w in range(W):
      if m[h][w]=="
        ans_m[h][w]="
      else:
        n_mines=0
        for dh, dw in dhw:
          nh, nw = h+dh, w+dw
          if 0<=nh<H and 0<=nw<W:
            if m[nh][nw]=="
              n_mines+=1
          ans_m[h][w]=str(n_mines)
  for _ in range(H):
    print("".join(ans_m[_]))
  return

if __name__ == "__main__":
  main()

Solution (Python):

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

G = [['' for _ in range(W+2)] for _ in range(H+2)]
Ans = [[0 for _ in range(W+2)] for _ in range(H+2)]

for i in range(1,H+1):
    S = input()
    for j,s in enumerate(S):
        G[i][j+1]= s

for i in range(1,H+1):
    for j in range(W):
        
        if i == 1:
            if G[i][j+2] == '
                Ans[i][j+1] += 1
            
            
            if G[i][j] == '
                Ans[i][j+1] += 1
            if G[i+1][j+2] == '
                Ans[i][j+1] += 1
            if G[i+1][j+1] == '
                Ans[i][j+1] += 1
            if G[i+1][j] == '
                Ans[i][j+1] += 1
            continue
        
        if i == H:
            if G[i][j+2] == '
                Ans[i][j+1] += 1
            
            
            if G[i][j] == '
                Ans[i][j+1] += 1
            if G[i-1][j+2] == '
                Ans[i][j+1] += 1
            if G[i-1][j+1] == '
                Ans[i][j+1] += 1
            if G[i-1][j] == '
                Ans[i][j+1] += 1
            continue

        if G[i][j+2] == '
            Ans[i][j+1] += 1
        
        
        if G[i][j] == '
            Ans[i][j+1] += 1
        if G[i+1][j+2] == '
            Ans[i][j+1] += 1
        if G[i+1][j+1] == '
            Ans[i][j+1] += 1
        if G[i+1][j] == '
            Ans[i][j+1] += 1 
        if G[i-1][j+2] == '
            Ans[i][j+1] += 1
        if G[i-1][j+1] == '
            Ans[i][j+1] += 1
        if G[i-1][j] == '
            Ans[i][j+1] += 1

for i in range(1,H+1):
    for j in range(1,W+1):
        Ans[i][j] = str(Ans[i][j])
        if G[i][j] == '
            Ans[i][j] = '

for i in range(1,H+1):
    s = ''
    for j in range(1,W+1):
        s += Ans[i][j]
    print(s)