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):
def count_bomb(y,x):
    ans=0
    for i in range(y-1,y+2):
        for j in range(x-1,x+2):
            if i>=0 and i<h and j>=0 and j<w:
                if s[i][j]=="
                    ans+=1
    return ans
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]=count_bomb(i,j)
for i in range(h):
    print(*s[i],sep="")

Solution (Python):
def main():
	H,W = list(map(int, input().split()))
	grid = []

	for _ in range(H):
		grid.append(list(input()))

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

	for i in range(H):
		for j in range(W):
			if(grid[i][j] == "
				continue
			
			count = 0
			for k in shift:
				ii = i + k[0]
				jj = j + k[1]

				if(-1 < ii < H and -1 < jj < W and grid[ii][jj] == "
					count += 1
			
			grid[i][j] = count

	for row in grid:
		print("".join(map(str, row)))


if __name__ == "__main__":
	main()

Solution (Python):
from collections import deque
H,W=map(int,input().split())
b=deque()
ans=[]
for i in range(H):
    s=input()
    t=[]
    for j in range(W):
        if s[j]=='
            b.append((i,j))
            t.append('
        else:
            t.append(0)
    ans.append(t)
while b:
    y,x=b.popleft()
    for i in range(-1,2):
        for j in range(-1,2):
            if i==0 and j==0:
                continue
            if 0<=y+i<H and 0<=x+j<W and ans[y+i][x+j]!='
                ans[y+i][x+j]+=1
for s in ans:
    print(*s,sep='')

Solution (Python):
import numpy as np


def main():
    h, w = map(int, input().split())
    ss = np.array([list(input()) for _ in range(h)])
    ans = np.zeros((h, w))
    xs, ys = np.where(ss == '
    for x, y in zip(xs, ys):
        coord = np.array([[x, y]])
        surround = np.array([[-1, -1], [-1, 0], [-1, +1], [0, -1], [0, +1], [+1, -1], [+1, 0], [+1, +1]])
        mines = np.tile(coord, (8, 1)) - surround
        for mx, my in mines:
            if mx < 0 or my < 0 or mx >= h or my >= w:
                continue
            ans[mx, my] += 1
    ans = ans.astype(np.int16).tolist()
    for x, y in zip(xs, ys):
        ans[x][y] = '

    for line in ans:
        print(''.join(map(str, line)))


if __name__ == "__main__":
    main()

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

def main():
    H, W = map(int, readline().rstrip().split())
    mat = [list(readline().rstrip()) for _ in range(H)]
    for h in range(H):
        for w in range(W):
            if mat[h][w] == '.':
                cnt = 0
                for dh in [-1, 0, 1]:
                    for dw in [-1, 0, 1]:
                        if 0 <= h+dh < H and 0 <= w+dw < W:
                            if mat[h+dh][w+dw] == '
                                cnt += 1
                mat[h][w] = cnt
    
    for l in mat:
        print(*l, sep='')

if __name__ == '__main__':
    main()

Solution (Python):
def count_bomb(y,x):
    ans=0
    for i in range(y-1,y+2):
        for j in range(x-1,x+2):
            if i>=0 and i<h and j>=0 and j<w:
                if s[i][j]=="
                    ans+=1
    return ans
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]=count_bomb(i,j)
for i in range(h):
    print(*s[i],sep="")

Solution (Python):

h, w = map(int, input().split())
sumgrid = [[0] * w for i in range(h)]   

for i in range(h):
    
    line = list(map(int, input().replace('

    for j in range(w):
        
        bom_cnt = line[j-1] if 0 < j else 0
        bom_cnt += line[j]
        bom_cnt += line[j+1] if j < (w - 1) else 0

        
        if line[j] == 0:
            sumgrid[i][j] += bom_cnt
        else:
            sumgrid[i][j] = '

        
        if 0 < i and sumgrid[i-1][j] != '
            sumgrid[i-1][j] += bom_cnt

        
        if i + 1 < h:
            sumgrid[i+1][j] += bom_cnt

for i in range(h):
    print(''.join([str(n) for n in sumgrid[i]]))

Solution (Python):
h,w = map(int, input().split())
grid = []
ddxy = [[0,1],[1,0],[0,-1],[-1,0],[1,1],[1,-1],[-1,1],[-1,-1]]
ans=[[0]*w for _ in range(h)]

def ok(x, y):
    if x>-1 and y>-1 and x<w and y<h and ans[y][x]!=-1:
        return 1
    else:
        return 0
for i in range(h):
    s = list(input().strip())
    grid.append(s)
for i in range(h):
    for j in range(w):
        if grid[i][j]=="
            ans[i][j]=-1
            for dxdy in ddxy:
                if ok(j+dxdy[1],i+dxdy[0]):
                    ans[i+dxdy[0]][j+dxdy[1]]+=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):
import sys

readline = sys.stdin.readline

def check(i, j):
    global grid
    if grid[i][j] == '
        return '
    cnt = 0
    for k in range(-1, 2):
        for l in range(-1, 2):
            cnt += grid[i+k][j+l] == '
    return str(cnt)

def main():
    global grid
    H, W = map(int, readline().split())
    grid = []
    grid.append(['*']*(W+2))
    for _ in range(H):
        grid.append(['*'] + list(readline()[:-1]) + ['*'])
    grid.append(['*']*(W+2))

    for i in range(1, H+1):
        for j in range(1, W+1):
            grid[i][j] = check(i, j)
    
    for i in range(1, H+1):
        print(''.join(grid[i][1:W+1]))
    
    
if __name__ == "__main__":
    main()

Solution (Python):


h,w = map(int,input().split())
s = [list(input()) for _ 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 s[y][x] == '
            continue
        
        cnt = 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 s[ny][nx] == "
                cnt += 1
                
        s[y][x] = str(cnt)
for y in range(h):
    print(''.join(s[y]))

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

for i in range(h):
    for j in range(w):
        if s[i][j] == '.':
            for x in range(-1, 2):
                for y in range(-1, 2):
                    if 0 <= i + x < h and 0 <= j + y < w:
                        if s[i+x][j+y] == '
                            data[i][j] += 1
        else:
            data[i][j] = '

for i in range(h):
    for j in range(w):
        if data[i][j] != '
            data[i][j] = str(data[i][j])

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

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

direc = [-1, 0, 1]

for i in range(h):
    for j in range(w):
        if s[i][j] == '.':
            for x in direc:
                for y in direc:
                    if 0 <= i + x < h and 0 <= j + y < w:
                        if s[i+x][j+y] == '
                            data[i][j] += 1
        else:
            data[i][j] = '

for i in range(h):
    for j in range(w):
        if data[i][j] != '
            data[i][j] = str(data[i][j])

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

Solution (Python):
from sys import stdin


if __name__ == "__main__":
    _in = [_.rstrip() for _ in stdin.readlines()]
    H,W = list(map(int,_in[0].split(' ')))  
    S_arr = [[None]*(W+2) for i in range(H+2)]
    for i in range(H):
        _ = list(_in[i+1])   
        S_arr[i+1][1:-1] = _
    
    num_arr = [[None]*W for i in range(H)]
    for i in range(1,H+1):
        for j in range(1,W+1):
            if S_arr[i][j] == '
                num_arr[i-1][j-1] = '
            else:
                cnt = 0
                cnt += 1 if S_arr[i-1][j-1]=='
                cnt += 1 if S_arr[i-1][j]=='
                cnt += 1 if S_arr[i-1][j+1]=='
                cnt += 1 if S_arr[i][j-1]=='
                cnt += 1 if S_arr[i][j+1]=='
                cnt += 1 if S_arr[i+1][j-1]=='
                cnt += 1 if S_arr[i+1][j]=='
                cnt += 1 if S_arr[i+1][j+1]=='
                num_arr[i-1][j-1] = str(cnt)
    
    for num in num_arr:
        print(''.join(num))

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

for i in range(h):
  for j in range(w):
    if ar[i][j] == '
      continue
    
    bomb = 0
    if i > 0:
      if j > 0 and ar[i-1][j-1] == '
        bomb += 1
      if ar[i-1][j] == '
        bomb += 1
      if j+1 < w and ar[i-1][j+1] == '
        bomb += 1
    if j > 0 and ar[i][j-1] == '
      bomb += 1
    if j+1 < w and ar[i][j+1] == '
      bomb += 1
    if i+1 < h:
      if j > 0 and ar[i+1][j-1] == '
        bomb += 1
      if ar[i+1][j] == '
        bomb += 1
      if j+1 < w and ar[i+1][j+1] == '
        bomb += 1
    ar[i][j] = str(bomb)

for i in range(h):
  print("".join(ar[i]))

Solution (Python):

import sys
import math
from bisect import bisect_left, bisect_right, insort_left, insort_right
from collections import defaultdict, deque, Counter
from copy import deepcopy
from fractions import gcd
from functools import lru_cache, reduce
from heapq import heappop, heappush
from itertools import accumulate, groupby, product, permutations, combinations, combinations_with_replacement
from math import ceil, floor, factorial, log, sqrt, sin, cos
from operator import itemgetter
from string import ascii_lowercase, ascii_uppercase, digits
sys.setrecursionlimit(10**7)
rs = lambda: sys.stdin.readline().rstrip()
ri = lambda: int(rs())
rf = lambda: float(rs())
rs_ = lambda: [_ for _ in rs().split()]
ri_ = lambda: [int(_) for _ in rs().split()]
rf_ = lambda: [float(_) for _ in rs().split()]
INF = float('inf')
MOD = 10 ** 9 + 7
PI = math.pi

H, W = ri_()
S = [list(rs()) for _ in range(H)]
for i in range(H):
    for j in range(W):
        cnt = 0
        if S[i][j] == '
            continue
        for dx in [-1, 0, 1]:
            for dy in [-1, 0, 1]:
                if not 0 <= i + dy < H or not 0 <= j + dx < W:
                    continue
                if S[i + dy][j + dx] == '
                    cnt += 1
        S[i][j] = str(cnt)
for s in S:
    print(''.join(s))

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

def checker(s_input,point):
    tmp=0
    x,y = map(int,point)
    def next_check(y,x):
        if 0<=y<=(h-1) and 0<=x<=(w-1):
            if s_input[y][x]=='
                return 1
        return 0
    tmp+=next_check(y-1,x-1)
    tmp+=next_check(y-1,x)
    tmp+=next_check(y-1,x+1)
    tmp+=next_check(y,x-1)
    tmp+=next_check(y,x+1)
    tmp+=next_check(y+1,x-1)
    tmp+=next_check(y+1,x)
    tmp+=next_check(y+1,x+1)
    return tmp

for i in range(h):
    for j in range(w):
        if s_input[i][j]=='
            ans_map[i][j]='
        else:
            ans_map[i][j]=checker(s_input,[j,i])

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

Solution (Python):
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] == '.':
            count = 0
            for dx, dy in (
                (-1,1), (0,1), (1,1),
                (-1,0), (0,0), (1,0),
                (-1,-1),(0,-1),(1,-1)
            ):
                now_x, now_y = i+dx, j+dy
                if 0 <= now_x and 0 <= now_y and now_x <= H-1 and now_y <= W-1 and s[now_x][now_y] == "
                    count += 1
            s[i][j] = str(count)

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

Solution (Python):
import itertools,sys
def S(): return sys.stdin.readline().rstrip()
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
H,W = LI()
S = [S() for _ in range(H)]
ans = []
for h,w in itertools.product(range(H),range(W)):
    if S[h][w]=='
        ans.append('
        continue
    count = 0
    for dh,dw in itertools.product([-1,0,1],repeat=2):
        nh,nw = h+dh,w+dw
        if not(0<=nh<=H-1) or not (0<=nw<=W-1):
            continue
        if S[nh][nw]=='
            count += 1
    ans.append(count)
for i,x in enumerate(ans,1):
    print(x,sep='',end='')
    if i%W==0:
        print()

Solution (Python):
import itertools,sys
def S(): return sys.stdin.readline().rstrip()
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
H,W = LI()
S = [S() for _ in range(H)]
ans = []
dhdw = [[-1,-1],[-1,0],[-1,1],[0,-1],[0,1],[1,-1],[1,0],[1,1]]
for h,w in itertools.product(range(H),range(W)):
    if S[h][w]=='
        ans.append('
        continue
    count = 0
    for dh,dw in dhdw:
        nh,nw = h+dh,w+dw
        if not(0<=nh<=H-1) or not (0<=nw<=W-1):
            continue
        if S[nh][nw]=='
            count += 1
    ans.append(count)
for i,x in enumerate(ans,1):
    print(x,sep='',end='')
    if i%W==0:
        print()

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

ans = [[0 for _ in range(W)] for _ in range(H)]
dx = [-1, 0, 1]
dy = [-1, 0, 1]
for h in range(H):
    for w in range(W):
        if S[h][w] == '.':
            tmp = 0
            for i in range(3):
                for j in range(3):
                    if h == 0:
                        if dx[i] == -1:
                            continue
                    if h == H-1:
                        if dx[i] == 1:
                            continue
                    if w == 0:
                        if dy[j] == -1:
                            continue
                    if w == W-1:
                        if dy[j] == 1:
                            continue
                    new_x, new_y = h+dx[i], w+dy[j]
                    if S[new_x][new_y] == '
                        tmp += 1
            ans[h][w] = str(tmp)
        elif S[h][w] == '
            ans[h][w] = '

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

Solution (Python):
h,w=map(int,input().split())
S= [[j for j in input()] for i in range(h)]
for i in range(h):
  for j in range(w):
    a=0
    if S[i][j]==".":
      for p in range(-1,2):
        for k in range(-1,2):
          if 0<=i+p<h and 0<=j+k<w:
            if S[i+p][j+k]=="
              a+=1
      S[i][j]=str(a)

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

Solution (Python):
h,w=map(int,input().split())
li=[input() for i in range(h)]
ans=[[0]*w for i in range(h)]
for i in range(h):
  for j in range(w):
    if li[i][j]=='
      ans[i][j]='
      for k in range(-1,2):
        for l in range(-1,2):
          if 0<=i+k<h and 0<=j+l<w:
            if ans[i+k][j+l]!='
              ans[i+k][j+l]+=1
anss=[map(str,ans[i]) for i in range(h)]
for i in range(h):
  print(''.join(anss[i]))

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

B = [[0]*w for i in range(h)]

d = [-1, 0, 1]

for i in range(h):
    for j in range(w):
        if A[i][j] == "
            B[i][j] = "
            for k in d:
                for l in d:
                    if 0 <= i+k < h and 0 <= j+l < w:
                        if B[i+k][j+l] != "
                            B[i+k][j+l] += 1


B_str = [map(str, B[i]) for i in range(h)]
for i in range(h):
    print("".join(B_str[i]))

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

a = [0, 0, 1, -1, 1, -1, 1, -1]
b = [1, -1, 1, -1, -1, 1, 0, 0]
for i in range(h):
    for j in range(w):
        if s[i][j] == '.':
            sub = 0
            for k in range(8):
                if i + a[k] >= 0 and j + b[k] >= 0 and i + a[k] < h and j + b[k] < w:
                    if s[i + a[k]][j + b[k]] == '
                        sub += 1
            s[i][j] = sub

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

Solution (Python):
H,W = map(int,input().split())
src = [input() for i in range(H)]
ans = []
for row in src:
    ans.append(list(row))
dxy = [(-1,-1),(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1)]
for y in range(H):
    for x in range(W):
        if src[y][x] == '
        c = 0
        for dx,dy in dxy:
            if x+dx < 0 or x+dx > W-1 or y+dy < 0 or y+dy > H-1: continue
            if src[y+dy][x+dx] == '
        ans[y][x] = c
for row in ans:
    print(''.join(list(map(str,row))))

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

src = [input() for i in range(h)]

ans = []
for row in src:
    ans.append(list(row))
    
for i in range(h):
    for j in range(w):
        if src[i][j] == '
            continue
        c = 0
        if i > 0:
            if j > 0 and src[i-1][j-1] == '
            if src[i-1][j] == '
            if j < w-1 and src[i-1][j+1] == '
        if j > 0 and src[i][j-1] =='
        if j < w-1 and src[i][j+1] == '
        if i < h-1:
            if j > 0 and src[i+1][j-1] == "
            if src[i+1][j] == '
            if j<w-1 and src[i+1][j+1] == '
        ans[i][j] = c
        
for row in ans:
    print (''.join(list(map(str,row))))

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

src = [input() for i in range(h)]

ans = []
for row in src:
    ans.append(list(row))
    
for i in range(h):
    for j in range(w):
        if src[i][j] == '
            continue
        c = 0
        if i > 0:
            if j > 0 and src[i-1][j-1] == '
            if src[i-1][j] == '
            if j < w-1 and src[i-1][j+1] == '
        if j > 0 and src[i][j-1] =='
        if j < w-1 and src[i][j+1] == '
        if i < h-1:
            if j > 0 and src[i+1][j-1] == "
            if src[i+1][j] == '
            if j<w-1 and src[i+1][j+1] == '
        ans[i][j] = c
        
for row in ans:
    print (''.join(list(map(str,row))))

Solution (Python):

import sys

def count(h, w, S: "List[str]") -> int:
    return (S[h-1][w] + S[h][w-1] + S[h-1][w-1] + S[h+1][w-1] + S[h-1][w+1] + S[h+1][w+1] + S[h+1][w] + S[h][w+1]).count("

def solve(H: int, W: int, S: "List[str]"):
    S = ["." * W] + S + ["." * W]
    for h in range(H+2):
        S[h] = "." + S[h] + "."
    ans = ["" for _ in range(H)]
    for h in range(H):
        for w in range(W):
            if S[h+1][w+1] != "
                ans[h] += str(count(h+1, w+1, S))
            else:
                ans[h] += "
    for a in ans:
        print("".join(a))
    return



def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    H = int(next(tokens))  
    W = int(next(tokens))  
    S = [next(tokens) for _ in range(H)]  
    solve(H, W, S)

if __name__ == '__main__':
    main()

Solution (Python):

import re
import math
from collections import defaultdict
import itertools
from copy import deepcopy
import random
from heapq import heappop,heappush
import time
import os
import queue
import sys
import datetime
from functools import lru_cache

readline=sys.stdin.readline
sys.setrecursionlimit(2000000)

alphabet="abcdefghijklmnopqrstuvwxyz"
mod=int(10**9+7)
inf=int(10**20)
def yn(b):
    if b:
        print("yes")
    else:
        print("no")
def Yn(b):
    if b:
        print("Yes")
    else:
        print("No")
def YN(b):
    if b:
        print("YES")
    else:
        print("NO")
class union_find():
    def __init__(self,n):
        self.n=n
        self.P=[a for a in range(N)]
        self.rank=[0]*n
 
    def find(self,x):
        if(x!=self.P[x]):self.P[x]=self.find(self.P[x])
        return self.P[x]
 
    def same(self,x,y):
        return self.find(x)==self.find(y)
 
    def link(self,x,y):
        if self.rank[x]<self.rank[y]:
            self.P[x]=y
        elif self.rank[y]<self.rank[x]:
            self.P[y]=x
        else:
            self.P[x]=y
            self.rank[y]+=1
 
    def unite(self,x,y):
        self.link(self.find(x),self.find(y))
 
    def size(self):
        S=set()
        for a in range(self.n):
            S.add(self.find(a))
        return len(S)
def is_power(a,b):
    now=b
    while now<a:
        now*=b
    if now==a:return True
    else:return False
def bin_(num,size):
    A=[0]*size
    for a in range(size):
        if (num>>(size-a-1))&1==1:
            A[a]=1
        else:
            A[a]=0
    return A
def get_facs(n,mod_=0):
    A=[1]*(n+1)
    for a in range(2,len(A)):
        A[a]=A[a-1]*a
        if(mod>0):A[a]%=mod_
    return A
def comb(n,r,mod,fac):
    if(n-r<0):return 0
    return (fac[n]*pow(fac[n-r],mod-2,mod)*pow(fac[r],mod-2,mod))%mod
def next_comb(num,size):
    x=num&(-num)
    y=num+x
    z=num&(~y)
    z//=x
    z=z>>1
    num=(y|z)
    if(num>=(1<<size)):return False
    else:
        return num
def get_primes(n,type="int"):
    A=[True]*(n+1)
    A[0]=False
    A[1]=False
    for a in range(2,n+1):
        if A[a]:
            for b in range(a*2,n+1,a):
                A[b]=False
    if(type=="bool"):return A
    B=[]
    for a in range(n+1):
        if(A[a]):B.append(a)
    return B
def is_prime(num):
    if(num<=2):return False
    i=2
    while i*i<=num:
       if(num%i==0):return False
       i+=1
    return True
def ifelse(a,b,c):
    if a:return b
    else:return  c
def join(A,c=" "):
    n=len(A)
    A=list(map(str,A))
    s=""
    for a in range(n):
        s+=A[a]
        if(a<n-1):s+=c
    return s
def factorize(n,type_="dict"):
    b = 2
    list_ = []
    while b * b <= n:
        while n % b == 0:
            n //= b
            list_.append(b)
        b+=1
    if n > 1:list_.append(n)
    if type_=="dict":
        dic={}
        for a in list_:
            if a in dic:
                dic[a]+=1
            else:
                dic[a]=1
        return dic
    elif type_=="list":
        return list_
    else:
        return None
def floor_(n,x=1):
    return x*(n//x)
def ceil_(n,x=1):
    return x*((n+x-1)//x)
def hani(x,min_,max_):
    ret=x
    if x<min_:ret=min_
    if x>max_:ret=max_
    return ret
def seifu(x):
    return x//abs(x)


def main():
    H,W=map(int,input().split())
    S=[]
    for a in range(H):
        S.append(input())
    ans=[["a" for a in range(W)] for b in range(H)]
    for h in range(H):
        for w in range(W):
            if S[h][w]=="
                ans[h][w]="
            else:
                count=0
                for a in range(-1,2):
                    for b in range(-1,2):
                        x=w+b
                        y=h+a
                        
                        if 0<=y and y<H and 0<=x and x<W:
                            if S[y][x]=="
                                count+=1
                ans[h][w]=str(count)
    for a in range(H):
        print("".join(ans[a]))
main()

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

s = [[""]*w for _ in range(h)]



for i in range(h):
    s[i] = list(temp[i])

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






  
  
for i in range(h):
    for j in range(w):
        if s[i][j] == "
            ns[i][j] = "

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

            if i != h-1:
                if ns[i+1][j] != "
                if j != 0:
                    if ns[i+1][j-1] != "
                if j != w-1:
                    if ns[i+1][j+1] != "

for i in range(h):
    for j in range(w):
        ns[i][j] = str(ns[i][j])

for i in range(h):
    print("".join(ns[i]))

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

Solution (Python):
N, M = map(int,input().split()) 
P= [["." for _ in range(M+2)] for i in range(N+2)]

for i in range(1,N+1):
    P[i][1:M+1] = list(input())


def rep(a,b):
    ans = 0
    for i in range(-1,2,1):
        for j in range(-1,2,1):
            if P[a+i][b+j]=="
                ans += 1
    P[a][b] = ans


for i in range(1,M+1):
    for j in range(1,N+1):
        if P[j][i] == ".":
          rep(j,i)

for i in range(1,N+1):
    print(''.join([str(i) for i in P[i][1:M+1]]))

Solution (Python):
import numpy as np

H, W = map(int, input().split())
S = [list(input()) for _ in range(H)]
  
dx = [1, 1,  1, 0,  0, -1, -1, -1]
dy = [1, 0, -1, 1, -1,  1,  0, -1]

for h, row in enumerate(S):
  for w, i in enumerate(row):
    if i == '.':
      S[h][w] = 0
    if i == '
      for x, y in zip(dx, dy):
        if 0 <= x+w < W and 0 <= y+h < H:
          if S[y+h][x+w] == '
            continue
          elif S[y+h][x+w] == '.':
            S[y+h][x+w] = 1
          else:
            S[y+h][x+w] += 1
            
for s in S:
  print(''.join(map(str, s)))

Solution (Python):
dy = [1, 0, -1, 1, -1, 1, 0, -1]
dx = [1, 1, 1, 0, 0, -1, -1, -1]

def resolve():
	h, w = map(int, input().split())
	m = [[0]*w for _ in range(h)]
	s = [list(input()) for _ in range(h)]
	for i in range(h):
		for j in range(w):
			if s[i][j] == '
				m[i][j] = '
				continue
			for k in range(8):
				ny = i + dy[k]
				nx = j + dx[k]
				if 0 <= ny < h and 0 <= nx < w and s[ny][nx] == '
					m[i][j] += 1
	for seq in m:
		print("".join([str(i) for i in seq]))
resolve()

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

ans = []
for row in src:
    ans.append(list(row))

dxy = [(-1,-1),(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1)]
for y in range(H):
    for x in range(W):
        if ans[y][x] == '
        c = 0
        for dx,dy in dxy:
            if x+dx < 0 or x+dx > W-1 or y+dy < 0 or y+dy > H-1: continue
            if ans[y+dy][x+dx] == '
        ans[y][x] = c

for row in ans:
    print("".join(list(map(str,row))))

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

m = []

for i in range(H):
    s_t = input()
    s_w =[]
    for j in range(W):           
        s_w.append(s_t[j])
    s.append(s_w)



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

for i in range(H):
    for j in range(W):
        if s[i][j] == "
            continue
        
        count = 0
        for k in range(8):
            
            ni = i + dy[k]
            nj = j + dx[k]
            
            if (ni<0 or H<=ni) or (nj<0 or W<=nj):
                continue
            if s[ni][nj] == "
                count += 1
        s[i][j] = str(count)

for i in range(H):
    for j in range(W):
        print(s[i][j], end="")
    print()

Solution (Python):
import numpy as np
H,W = map(int,input().split())
table = [["."]*(W+2),]
for _ in range(H):
    table.append([".",]+list(input())+[".",])
table.append(["."]*(W+2))
table = np.array(table)
ans = []

for h in range(H):
    for w in range(W):
        mawari = table[h:h+3,w:w+3]
        
        if mawari[1,1] == ".":
            ans.append(np.count_nonzero(mawari=="
        else:
            ans.append("
ans = np.array(ans).reshape(H,W).astype(np.unicode)
for i in ans:
    print("".join(i))

Solution (Python):
h, w=map(int, input().split())
s=[[0]*(w+2)]+[[0]+list(input())+[0] for i in range(h)]+[[0]*(w+2)]
for i in range(1, h+1):
  for j in range(1, w+1):
    if s[i][j]=='.':
      s[i][j]=0
for i in range(1, h+1):
  for j in range(1, w+1):
    if s[i][j]=='
      for k in range(i-1, i+2):
        for l in range(j-1, j+2):
          if s[k][l]!='
            s[k][l]+=1
for i in range(1, h+1):
  print(''.join(map(str, s[i][1:w+1])))

Solution (Python):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
from collections import Counter, deque
from collections import defaultdict
from itertools import combinations, permutations, accumulate, groupby, product
from bisect import bisect_left,bisect_right
from heapq import heapify, heappop, heappush
from math import floor, ceil,sqrt
from operator import itemgetter
def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
def LI2(): return [int(input()) for i in range(n)]
def MXI(): return [[LI()]for i in range(n)]
def printns(x): print('\n'.join(x))
def printni(x): print('\n'.join(list(map(str,x))))
inf = 10**17
mod = 10**9 + 7


h,w=MI()
mines=[[0 for i in range(w+2)]for i in range(h+2)]

for i in range(w+2):
    mines[0][i]="."
    mines[h+1][i]="."
for i in range(h+2):
    mines[i][0]="."
    mines[i][w+1]="."
for i in range(h):
    s=list(input().rstrip())
    for j in range(w):
        mines[i+1][j+1]=s[j]

minum=[[0 for i 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 mines[i][j]=="
            minum[i-1][j]+=1
            minum[i+1][j]+=1
            minum[i][j+1]+=1
            minum[i][j-1]+=1
            minum[i+1][j+1]+=1
            minum[i+1][j-1]+=1
            minum[i-1][j+1]+=1
            minum[i-1][j-1]+=1

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

Solution (Python):
h,w = map(int,input().split())
s = [[j for j in input()] for i in range(h)]
for i in range(h):
  for p in range(w):
    count = 0
    if s[i][p] =='.':
      for j in range(-1,2):
        for k in range(-1,2):
          if 0 <= i+j < h and 0<= p+k < w:
            if s[i+j][p+k] == '
              count += 1
      s[i][p] = str(count)
for i in s:
  print("".join(i))