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())
L = [[x if x=='

for i in range(H):
  l = L[i]
  l_before=None
  l_after=None
  if i>0:
    l_before=L[i-1]
  if i<H-1:
    l_after=L[i+1]
    
  for j in range(W):
    if l[j] != '
      try:
        if j>=1 and l_before[j-1]=='
          l[j]+=1
      except:
        pass
      try:
        if l_before[j]=='
          l[j]+=1
      except:
        pass
      try:
        if l_before[j+1]=='
          l[j]+=1
      except:
        pass
      try:
        if j>=1 and l[j-1]=='
          l[j]+=1
      except:
        pass
      try:
        if l[j+1]=='
          l[j]+=1
      except:
        pass
      
      try:
        if j>=1 and l_after[j-1]=='
          l[j]+=1
      except:
        pass
      try:
        if l_after[j]=='
          l[j]+=1
      except:
        pass
      try:
        if l_after[j+1]=='
          l[j]+=1
      except:
        pass


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

Solution (Python):
import itertools
H,W = map(int,input().split())
S = [input() for h in range(H)]
a = [[0 if cell=="." else "

for h in range(H):
  for w in range(W):
    if S[h][w]!="
      continue
    for dx,dy in itertools.product([-1,0,1],repeat=2):
      x = h+dx
      y = w+dy
      if x<0 or H<=x:
        continue
      if y<0 or W<=y:
        continue
      if S[x][y]=="
        continue
      a[x][y]+=1

for row in a:
  print(*row,sep="")

Solution (Python):









import os
import sys




def main():
    h, w = map(int, input().split())
    s = []
    s.append((w+2)*'.')
    for _ in range(h):
        s.append('.'+input()+'.')
    s.append((w+2)*'.')

    cnt = [[0]*(w+2) for i in range(h+2)]
    for i in range(1, h+1):
        for j in range(1, w+1):
            if s[i][j] == '
                continue
            if s[i-1][j-1] == '
                cnt[i][j] += 1
            if s[i-1][j] == '
                cnt[i][j] += 1
            if s[i-1][j+1] == '
                cnt[i][j] += 1
            if s[i][j-1] == '
                cnt[i][j] += 1
            if s[i][j+1] == '
                cnt[i][j] += 1
            if s[i+1][j-1] == '
                cnt[i][j] += 1
            if s[i+1][j] == "
                cnt[i][j] += 1
            if s[i+1][j+1] == '
                cnt[i][j] += 1

    for i in range(1, h+1):
        for j in range(1, w+1):
            if s[i][j] == '
                print('
                continue
            print(cnt[i][j], end='')
        print()






if __name__ == "__main__":
    main()

Solution (Python):
h,w=map(int,input().split())
s = [["."for i in range(w+2)]]
for i in range(h):s.append(["."]+list(input())+["."])
s.append(["."]*(w+2))
l=[]
for i in range(1,h+1):
 l2=[]
 for j in range(1,w+1):
  if s[i][j]=="
  else:l2.append(str([s[i+1][j],s[i+1][j+1],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("
 l.append("".join(l2))
print(*l,sep="\n")

Solution (Python):
def bakudanda (y,x):
  if y < 0 or y >= len(s):
    return 0
  if x < 0 or x >= len(s[y]):
    return 0
  if s[y][x] == '
    return 1
  else:
    return 0
H,W = map(int,input().split())
s=[]
for i in range(H):
  s.append(input())
  
for i in range(len(s)):
  a = s[i]
  fp = ''
  for j in range(len(a)):
    if a[j] == '.':
      c = 0
      ds = [(-1, -1), (-1, 0), (-1, 1), (0, -1), (0,1), (1, -1), (1, 0), (1, 1)]
      for (y, x) in ds:
        c+= bakudanda(i+y,j+x)
      fp += str(c)
    else:
      fp += '
  print(fp)

Solution (Python):
def bakudanda (S,y,x):
  if y < 0 or y >= len(S):
    return 0
  if x < 0 or x >= len(S[y]):
    return 0
  if S[y][x] == '
    return 1
  else:
    return 0
H,W = map(int,input().split())
s=[]
for i in range(H):
  s.append(input())
  
for i in range(len(s)):
  a = s[i]
  fp = ''
  for j in range(len(a)):
    if a[j] == '.':
      c = 0
      ds = [(-1, -1), (-1, 0), (-1, 1), (0, -1), (0,1), (1, -1), (1, 0), (1, 1)]
      for (y, x) in ds:
        c+= bakudanda(s,i+y,j+x)
      fp += str(c)
    else:
      fp += '
  print(fp)

Solution (Python):
h, w = map(int, input().split())
l = []
for i in range(h):
    l.append(list(input()))
for i in range(h):
    for j in range(w):
        num_mine = 0
        if l[i][j] != "
            if 0<i and 0<j and l[i-1][j-1] == "
                num_mine += 1
            if 0<i and l[i-1][j] == "
                num_mine += 1
            if 0<i and j<w-1 and l[i-1][j+1] == "
                num_mine += 1
            if 0<j and l[i][j-1] == "
                num_mine += 1
            if j<w-1 and l[i][j+1] == "
                num_mine += 1
            if i<h-1 and 0<j and l[i+1][j-1]== "
                num_mine += 1
            if i<h-1 and l[i+1][j] == "
                num_mine += 1
            if i<h-1 and j<w-1 and l[i+1][j+1] == "
                num_mine += 1
            l[i][j] = str(num_mine) 
for i in range(h):
    print(''.join(l[i]))

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

def count_bomb_num(i, j):
    bomb_num = 0
    for k in range(-1, 2):
        if j == 0 and k == -1:
            continue
        if j == w - 1 and k == 1:
            continue
        if s_list[i][j + k] == "
            bomb_num += 1
    return bomb_num

count_list = [[0] * w for _ in range(h)]
for i in range(h):
    for j in range(w):
        if i == 0:
            count_list[i][j] = count_bomb_num(i, j)
        if i < h - 1:
            count_list[i + 1][j] = count_bomb_num(i + 1, j)
        
        if s_list[i][j] == "
            print("
        else:
            bomb_num = count_list[i][j]
            if i > 0:
                bomb_num += count_list[i - 1][j]
            if i < h - 1:
                bomb_num += count_list[i + 1][j]
            print(bomb_num, end="")
    print()

Solution (Python):
import numpy as np

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


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

a = np.reshape(a, (H, W))
for raw in a:
    ls = ""
    for item in raw:
        ls += item
    print(ls)

Solution (Python):
import numpy as np
from scipy import signal
H, W = list(map(int, input().split()))
com = np.full((1,W),'
Mat = np.zeros((H,W))
MAP = np.empty((H,W),int)
mask = [[1,1,1],
        [1,0,1],
        [1,1,1]]
for i in range(H):
    Mat[i] = list(input())==com

MAP = signal.convolve2d(Mat,mask,'same')
outMAP = np.where(Mat,'

for j in outMAP:print(*j,sep='')

Solution (Python):
H, W = map(int, input().split())
zlst = [0] * (W + 2)
b1 = []
b1.append(zlst)
for h in range(H):
  lst1 = [0]
  str1 = input()
  for s in str1:
    if s == '.':
      lst1.append(0)
    else:
      lst1.append(1)
  lst1.append(0)
  b1.append(lst1)
b1.append(zlst)


for i in range(H):
  nstr = ''
  for j in range(W):
    if b1[i + 1][j + 1] == 1:
      nstr = nstr + '
    else:
      nstr = nstr + str(
        b1[i][j] + b1[i][j+1] + b1[i][j+2] + b1[i+1][j] + 
        b1[i+1][j+2] + b1[i+2][j] + b1[i+2][j+1] + b1[i+2][j+2])
  print(nstr)

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

for i in range(h):
    result = []
    for j in range(w):
        if s[i][j] == '
            result.append('
        elif s[i][j] == '.':
            cnt = 0
            for y in range(-1, 2):
                for z in range(-1, 2):
                    if 0 <= i+y <= h-1:
                        if 0 <= j+z <= w-1:
                            if s[i+y][j+z] == '
                                cnt += 1
            result.append(str(cnt))
    print(*result, sep='')

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

def dfs(x,y):
    if 0 <= x < h and 0 <= y < w:
        if ans[x][y] != "
            ans[x][y] += 1
        

for i in range(h):
    for j in range(w):
        if s[i][j] == '
            ans[i][j] = '
            dfs(i+1, j)
            dfs(i+1, j+1)
            dfs(i+1, j-1)
            dfs(i-1, j+1)
            dfs(i-1, j)
            dfs(i-1, j-1)
            dfs(i, j+1)
            dfs(i, j-1)

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

Solution (Python):
h, w = map(int, input().split())
mine = []
mine.append(["."] * (w + 2))
for i in range(h):
    mine.append(["."] + list(input()) + ["."])
mine.append(["."] * (w + 2))
ans = [[0] * w for i in range(h)]
for i in range(1, h + 1):
    for j in range(1, w + 1):
        if mine[i][j] == "
            ans[i - 1][j - 1] = "
        else:
            if mine[i - 1][j - 1] == "
                ans[i - 1][j - 1] += 1
            if mine[i - 1][j] == "
                ans[i - 1][j - 1] += 1
            if mine[i - 1][j + 1] == "
                ans[i - 1][j - 1] += 1
            if mine[i][j - 1] == "
                ans[i - 1][j - 1] += 1
            if mine[i][j + 1] == "
                ans[i - 1][j - 1] += 1
            if mine[i + 1][j - 1] == "
                ans[i - 1][j - 1] += 1
            if mine[i + 1][j] == "
                ans[i - 1][j - 1] += 1
            if mine[i + 1][j + 1] == "
                ans[i - 1][j - 1] += 1
for i in ans:
    print(*i, sep="")

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

Rmin = 0
Rmax = 0
Cmin = 0
Cmax = 0


for r in range(H):
    
    for c in range(W):
        
        if S[r][c] == ".":
            S[r][c] = 0
        elif S[r][c] == "
            if r == 0 and H == 1:
                Rmin = r
                Rmax = 1
            elif r == 0:    
                Rmin = r
                Rmax = r+2
            elif r == H-1:
                Rmin = r-1
                Rmax = r+1
            else:
                Rmin = r-1
                Rmax = r+2

            if c == 0 and W ==1 :
                Cmin = c
                Cmax = 1
            elif c == 0:
                Cmin = c
                Cmax = c+2
            elif c == W-1:
                Cmin = c-1
                Cmax = c+1
            else:
                Cmin = c-1
                Cmax = c+2
            for i in range(Rmin,Rmax):
                for t in range(Cmin,Cmax):
                    if S[i][t] == "." or S[i][t] == 0:
                        S[i][t] = 1
                    elif not S[i][t] == "
                        S[i][t] += 1

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

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

for i in range(H):
    for j in range(W):
        if S[i][j] != "
            num = 0
            
            if i > 0:
                if j > 0:
                    if S[i-1][j-1] == "
                        num += 1
                if S[i-1][j] == "
                    num += 1
                if j < W-1:
                    if S[i-1][j+1] == "
                        num += 1
            if j > 0:
                if S[i][j-1] == "
                    num += 1
            if j < W-1:
                if S[i][j+1] == "
                    num += 1
            if i < H-1 :
                if j > 0:
                    if S[i+1][j-1] == "
                        num += 1
                if S[i+1][j] == "
                    num += 1
                if j < W-1:
                    if S[i+1][j+1] == "
                        num += 1
            
            S[i][j] = str(num)

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

Solution (Python):

import math
 
 
def inp():
    return(int(input()))
 
 

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

def instr():
    s = input()
    return(list(s[:len(s)]))
 
 

def invr():
    return(map(int, input().split()))
 
 
def valid(x, y): return True if x >= 0 and x < row and y >= 0 and y < col else False
 
 
fx = [+0, +0, +1, -1, -1, +1, -1, +1]
fy = [-1, +1, +0, +0, +1, +1, -1, -1]
row, col = invr()
mat = []
for _ in range(row):
    mat.append(instr())
 
for i in range(row):
    for j in range(col):
        if mat[i][j] == ".":
            count = 0
            for k in range(len(fx)):
                nx = i + fx[k]
                ny = j + fy[k]
                if valid(nx, ny):
                    if mat[nx][ny] == "
                        count += 1
            mat[i][j] = str(count)
 
for r in mat:
    for i in r:
        print(i, end="")
    print()

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

dx = [1,1,1,0,-1,-1,-1,0]
dy = [0,1,-1,1,0,1,-1,-1]
ans = [[0] * (w + 2) for _ in range(h + 2)]

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

for i in range(1, h+1):
    for j in range(1, w+1):
        if s[i][j] == '.':
            for A in range(8):
                if(s[i + dy[A]][j + dx[A]]=='
                    ans[i][j] += 1
            

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

Solution (Python):
def mine(n,m):
    try:
        if n >= 0 and n<=H and m>=0 and m<=W:
            s[n][m] += 1
    except:
        None


H,W = map(int,input().split())
s = [[0 if i == "." else "
      
for i in range(H):
    for j in range(W):
        if s[i][j] == "
            mine(i-1,j-1)
            mine(i-1,j)
            mine(i-1,j+1)
            mine(i,j+1)
            mine(i,j-1)
            mine(i+1,j-1)
            mine(i+1,j)
            mine(i+1,j+1)
        else:
            None
      
for k in s:
    a = list(map(str,k))
    print("".join(a))

Solution (Python):
def p(h,w):
    try:
        if h >= 0 and h<= H and w >= 0 and w <= W:
            S[h][w] += 1
    except:
        None

H,W = map(int,input().split())
S = [[0 if i == "." else "

for i in range(H):
    for j in range(W):
        if S[i][j] == "
            p(i+1, j)
            p(i+1, j+1)
            p(i+1, j-1)
            p(i-1, j)
            p(i-1, j+1)
            p(i-1, j-1)
            p(i, j+1)
            p(i, j-1)
            '''
            for k in S:
                a = list(map(str, k))
                print("".join(a))
            print()
            '''
        else:
            None
for k in S:
    a = list(map(str,k))
    print("".join(a))

Solution (Python):
h, w = map(int, input().split())
s = []
s.append("."*(w+2))
for _ in range(h):
    s.append("."+input()+".")
s.append("."*(w+2))
for i in range(1, h+1):
    ans = ""
    for j in range(1, w+1):
        if s[i][j] == ".":
            ans += 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("
        else:
            ans += "
    print(ans)

Solution (Python):
h,w = map(int,input().split())
gaze = ["W"+input()+"W" for _ in range(h)]
gaze = ["W"*(w+2)]+gaze+["W"*(w+2)]

for i in range(1,h+1):
    for j in range(1,w+1):
        count = 0
        if gaze[i][j] == ".":
            if gaze[i-1][j-1] == "
                count += 1
            if gaze[i-1][j] == "
                count += 1
            if gaze[i-1][j+1] == "
                count += 1
            if gaze[i+1][j-1] == "
                count += 1
            if gaze[i+1][j] == "
                count += 1
            if gaze[i+1][j+1] == "
                count += 1
            if gaze[i][j-1] == "
                count += 1
            if gaze[i][j+1] == "
                count += 1
            gaze[i] = gaze[i][:j]+str(count)+gaze[i][j+1:]

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

Solution (Python):
h,w=map(int,input().split())
s=[]
t=[(-1,1),(-1,0),(-1,-1),(0,1),(0,-1),(1,1),(1,0),(1,-1)]
for i in range(h):
    s.append(list(input()))
for i in range(h):
    for j in range(w):
        if s[i][j]==".":
            tmp=0
            for x,y in t:
                if 0<=i+x<=h-1 and 0<=j+y<=w-1:
                    if s[i+x][j+y]=="
                        tmp+=1
            s[i][j]=str(tmp)
for i in range(h):
    print("".join(s[i]))

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


k=[]
for i in range(h):
    p=[]
    for j in range(w):
        cnt=0
        if s[i+1][j+1]==".":
            if s[i][j]=="
                cnt+=1
            if s[i][j+1]=="
                cnt+=1
            if s[i][j+2]=="
                cnt+=1
            if s[i+1][j]=="
                cnt+=1
            if s[i+1][j+2]=="
                cnt+=1
            if s[i+2][j]=="
                cnt+=1
            if s[i+2][j+1]=="
                cnt+=1
            if s[i+2][j+2]=="
                cnt+=1
            p.append(cnt)
        else:
            p.append("
    k.append(p)
    
for i in range(h):
    for j in range(w):
        print(k[i][j],end="")
    print()

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

x = [1,-1,1,-1,0,0,1,-1]
y = [1,-1,0,0,1,-1,-1,1]
n_s = []
for i in range(h):
    n_l = []
    for j in range(w):
        cnt=0
        for k in range(8):
            if l[i][j]=="
                cnt = "
                break
            elif 0<=i+x[k]<h and 0<=j+y[k]<w:
                if l[i+x[k]][j+y[k]]=="
                    cnt+=1
            else:
                continue
        n_l.append(str(cnt))
    n_s.append(n_l)

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

Solution (Python):
import sys

def solve():
    input = sys.stdin.readline
    H, W = map(int, input().split())
    S = [list(input().strip("\n")) for _ in range(H)]
    round = ((-1, -1), (-1, 0), (-1, 1), (0, -1), (0, 1), (1, -1), (1, 0), (1, 1))
    for h in range(H):
        for w in range(W):
            if S[h][w] == ".":
                count = 0
                for dh, dw in round:
                    if 0<=h+dh<H and 0<=w+dw<W and S[h+dh][w+dw] == "
                        count += 1
                S[h][w] = str(count)
    for h in range(H): print("".join(S[h]))

    return 0

if __name__ == "__main__":
    solve()

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

for h in range(H):
    for w in range(W):
        if S[h][w] == ".":
            cnt = 0
            for i in range(max(h-1, 0), min(h+2, H)):
                for j in range(max(w-1, 0), min(w+2, W)):
                    if S[i][j] == "
                        cnt += 1
            S[h][w] = str(cnt)
    print("".join(S[h]))

Solution (Python):
h,  w =  map(int, input().split())
a = []
a.append("."*(w+2))
for _ in  range(h):
    a.append("."+input()+".")
a.append("."*(w+2))

for i in range(1, h+1):
    ans = ""
    for j in range(1, w+1):
        if a[i][j] == ".":
            ans += str([a[i-1][j-1], a[i-1][j], a[i-1][j+1], a[i][j-1], a[i][j+1], a[i+1][j-1], a[i+1][j], a[i+1][j+1]].count("
        else:
            ans += "
    print(ans)

Solution (Python):
h,w=map(int,input().split())
c=["."+input()+"." for i in range(h)]
s=["."*(w+2)]
s.extend(c)
b=["."*(w+2)]
s.extend(b)
sc=[[0for _ in range(w+2)] for i in range(h+2)]


for i in range(1,h+1):
    for j in range(1,w+1):
        if s[i][j]=="
            if sc[i-1][j-1]!="
                sc[i-1][j-1]+=1
            if sc[i][j-1]!="
                sc[i][j-1]+=1
            if sc[i+1][j-1]!="
                sc[i+1][j-1]+=1
            if sc[i-1][j]!="
                sc[i-1][j]+=1
            if sc[i+1][j]!="
                sc[i+1][j]+=1
            if sc[i-1][j+1]!="
                sc[i-1][j+1]+=1
            if sc[i][j+1]!="
                sc[i][j+1]+=1
            if sc[i+1][j+1]!="
                sc[i+1][j+1]+=1
            sc[i][j]="
for i in range(1,h+1):
    ne="".join([str(_) for _ in sc[i]])
    ne=ne[1:w+1]
    print(ne)

Solution (Python):
def print2(A,S): 
    for i in range(len(A)):
        b = [str(n) for n in A[i][:]]
        print(S.join(b))

h,w = map(int,input().split())
s = [0]*(h+2)
s[0] = ["."]*(w+2)
s[-1] = ["."]*(w+2)
for i in range(h):
    s[i+1] = ["."] + list(input()) + ["."]
ans = [[0 for j in range(w)] for i in range(h)]
for i in range(1,h+1):
    for j in range(1,w+1):
        if s[i][j] == "
            ans[i-1][j-1] = "
        else:
            for y in range(-1,2):
                for x in range(-1,2):
                    if s[i+y][j+x] == "
                        ans[i-1][j-1] += 1
print2(ans,"")

Solution (Python):
import numpy as np
h,w = (map(int,input().split(' ')))
x = []
for i in range(h):
  x.append(input())
  
y = np.zeros((h,w),dtype=int)

for n,i in enumerate(x):
  for j in range(w):
    if i[j] == '

      if j >= 1:
        if x[n][j-1] != '
          y[n][j-1]  +=1  
      if j < w-1:
        if x[n][j+1] != '
   	        y[n][j+1]  +=1  

      if n > 0:
        if x[n-1][j] !='
          y[n-1][j] +=1
        if j >= 1:
          if x[n-1][j-1] !='
            y[n-1][j-1] +=1
        if j < w-1:
          if x[n-1][j+1] !='
            y[n-1][j+1] +=1

      if n < h-1:
        if x[n+1][j] != '
          y[n+1][j] +=1
        if j >= 1:
          if x[n+1][j-1] != '
            y[n+1][j-1] +=1
        if j < w-1:
          if x[n+1][j+1] != '
            y[n+1][j+1] +=1

y2 = y.astype(np.unicode)
            
for n,i in enumerate(x):
  for n2,j in enumerate(i):
    if j == '
      y2[n][n2] ='
  print(''.join(list(y2[n])))

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

for y in range(h):
  for x in range(w):
    if s[y][x]=='.':
      cnt=0
      for dy,dx in [[0,1],[0,-1],[1,0],[-1,0],[1,1],[-1,1],[1,-1],[-1,-1]]:
        ny=y+dy
        nx=x+dx
        if 0<=ny<h and 0<=nx<w and s[ny][nx]=='
          cnt+=1
      s[y][x]=str(cnt)

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

Solution (Python):



import os
import sys
from itertools import repeat







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

    S = list()
    ans = list()

    for i in range(H):
        S.append(input())
        ans.append(list(repeat(0, W)))

    for i in range(H):
        for j in range(W):
            if S[i][j] == "
                ans[i][j] = "

                
                if i > 0 and j > 0 and ans[i - 1][j - 1] != "
                    ans[i - 1][j - 1] += 1
                
                if i > 0 and ans[i - 1][j] != "
                    ans[i - 1][j] += 1
                
                if i > 0 and j < W - 1 and ans[i - 1][j + 1] != "
                    ans[i - 1][j + 1] += 1

                
                if j > 0 and ans[i][j - 1] != "
                    ans[i][j - 1] += 1
                
                if j < (W - 1) and ans[i][j + 1] != "
                    ans[i][j + 1] += 1

                
                if i < (H - 1) and j > 0 and ans[i + 1][j - 1] != "
                    ans[i + 1][j - 1] += 1
                
                if i < (H - 1) and ans[i + 1][j] != "
                    ans[i + 1][j] += 1
                
                if i < (H - 1) and j < W - 1 and ans[i + 1][j] != "
                    ans[i + 1][j + 1] += 1

    for i in range(H):
        print(*ans[i], sep="")
    return


if __name__ == '__main__':
    main()

Solution (Python):
def resolve():
    H, W = [int(i) for i in input().split()]
    S = [list(input()) for _ in range(H)]
    for y in range(H):
        for x in range(W):
            if S[y][x] == ".":
                S[y][x] = 0
            else:
                S[y][x] = 9
    for y in range(H):
        for x in range(W):
            if S[y][x] < 9:
                continue
            if x - 1 >= 0:
                if y - 1 >= 0:
                    S[y - 1][x - 1] += 1
                S[y][x - 1] += 1
                if y + 1 < H:
                    S[y + 1][x - 1] += 1
            if y - 1 >= 0:
                S[y - 1][x] += 1
            S[y][x] += 1
            if y + 1 < H:
                S[y + 1][x] += 1
            if x + 1 < W:
                if y - 1 >= 0:
                    S[y - 1][x + 1] += 1
                S[y][x + 1] += 1
                if y + 1 < H:
                    S[y + 1][x + 1] += 1
    for y in range(H):
        for x in range(W):
            if S[y][x] < 9:
                S[y][x] = str(S[y][x])
            else:
                S[y][x] = "
        print("".join(S[y]))


resolve()

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

GRID = [["."] * (w + 2)]
for _ in range(h):
    GRID.append(["."] + list(input()) + ["."])
GRID.append(["."] * (w + 2))

for i in range(1, h + 1):
    for j in range(1, w + 1):
        if GRID[i][j] == "
            continue
        cnt = 0
        for a in range(-1, 2):
            for b in range(-1, 2):
                if GRID[i + a][j + b] == "
                    cnt += 1
        GRID[i][j] = str(cnt)

for row in GRID[1:h + 1]:
    print("".join(row[1:w + 1]))

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

for y in range(h):
  for x in range(w):
    if s[y][x]=='.':
      cnt=0
      for dy,dx in [[0,1],[0,-1],[1,0],[-1,0],[1,1],[-1,1],[1,-1],[-1,-1]]:
        ny=y+dy
        nx=x+dx
        if 0<=ny<h and 0<=nx<w and s[ny][nx]=='
          cnt+=1
      s[y][x]=str(cnt)

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

Solution (Python):
H,W=map(int,input().split())
S = [list(input()) for _ in range(H)]
result = [[0 for _ in range(W)] for _ in range(H)]
for i in range(H):
  for j in range(W):
    if S[i][j]=='
      result[i][j] = '
      if i+1 < H and result[i+1][j]!='
      if i-1 >= 0 and result[i-1][j]!='
      if j+1 < W and result[i][j+1]!='
      if j-1 >= 0 and result[i][j-1]!='
      if i+1 < H and j+1 < W and result[i+1][j+1]!='
      if i+1 < H and j-1 >= 0 and result[i+1][j-1]!='
      if i-1 >= 0 and j+1 < W and result[i-1][j+1]!='
      if i-1 >= 0 and j-1 >= 0 and result[i-1][j-1]!='
[[print(result[i][j]) if j==W-1 else print(result[i][j],end='') for j in range(W)] for i in range(H)]

Solution (Python):
import sys

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


board = [
  list(input())
  for _ in range(H)]

def calc(board, x, y):
    num = 0
    if board[x][y] == '
        return '
    for i in (x-1, x, x+1):
        for j in (y-1, y, y+1):
            if i == x and j == y:
                continue
            elif 0 <= i <= H-1 and 0 <= j <= W-1:
                if board[i][j] == '
                    num += 1
    return str(num)

for i in range(H):
    for j in range(W):
        sys.stdout.write(calc(board, i, j))
    sys.stdout.write('\n')

Solution (Python):

def canGo(h,w,y,x):
    if h==y and w==x:
        return False
    if h<0 or h>=H or w<0 or w>=W:
        return False
    if glid[h][w] == "
        return False
    return True

def search(h,w):
    dy=[-1,0,1]
    dx=[-1,0,1]
    if glid[h][w]=="
        mine[h][w] = -1
        for y in dy:
            for x in dx:
                if canGo(h+y,w+x,h,w):
                    mine[h+y][w+x]+=1

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

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


for i in range(H):
    for j in range(W):
        search(i,j)
for i in mine:
    for j in i:
        print(j if j!=-1 else "
    print()

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

for i in range(h):
    for j in range(w):
        if s[i][j] == ".":
            cnt = 0
            for y, x in d:
                if 0 <= i + y < h and 0 <= j + x < w:
                    if s[i+y][j+x] == "
                        cnt += 1
            s[i][j] = str(cnt)
        else:
            continue

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

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

Solution (Python):
H, W = map(int, input().split())
L = [str(input()) for _ in range(H)]
ans = [[0] * W for _ in range(H)]
dh = [1, 1, 1, 0, 0, -1, -1, -1]
dw = [-1, 0, 1, -1, 1, -1, 0, 1]
for h in range(H):
    for w in range(W):
        if L[h][w] == '
            ans[h][w] = '
        else:
            for i in range(8):
                nh = h + dh[i]
                nw = w + dw[i]
                if 0 <= nh < H and 0 <= nw < W and L[nh][nw] == '
                    ans[h][w] += 1
for row in ans:
    print(*row, sep='')

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

t=[[0]*w for _ in range(h)]
for i in range(h):
    for j in range(w):
        if s[i][j]=='.':
            for k in [(i-1,j-1),(i-1,j),(i-1,j+1),(i,j-1),(i,j+1),(i+1,j-1),(i+1,j),(i+1,j+1)]:
                if k[1]>=0 and k[1]<w and k[0]>=0 and k[0]<h:
                    if s[k[0]][k[1]]=='
                        t[i][j]+=1
        else:
            t[i][j]='

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