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())
S = ["." + input() + "." for _ in range(H)]
S = ["." * (W + 2)] + S + ["." * (W + 2)]
ans = [["0" for _ in range(W)] for _ 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:
            ans[i - 1][j - 1] = str((S[i - 1][j - 1], S[i - 1][j], S[i - 1][j + 1], S[i][j - 1], S[i][j], S[i][j + 1], S[i + 1][j - 1], S[i + 1][j], S[i + 1][j + 1],).count("
for i in ans:
    print("".join(i))

Solution (Python):
import sys
import heapq
import re
from itertools import permutations
from bisect import bisect_left, bisect_right
from collections import Counter, deque
from fractions import gcd
from math import factorial, sqrt, ceil
from functools import lru_cache, reduce
INF = 1 << 60
MOD = 1000000007
sys.setrecursionlimit(10 ** 7)


class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def size(self, x):
        return -self.parents[self.find(x)]

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

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        return len(self.roots())

    def all_group_members(self):
        return {r: self.members(r) for r in self.roots()}

    def __str__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())


def dijkstra_heap(s, edge, n):
    
    d = [10**20] * n
    used = [True] * n 
    d[s] = 0
    used[s] = False
    edgelist = []
    for a,b in edge[s]:
        heapq.heappush(edgelist,a*(10**6)+b)
    while len(edgelist):
        minedge = heapq.heappop(edgelist)
        
        if not used[minedge%(10**6)]:
            continue
        v = minedge%(10**6)
        d[v] = minedge//(10**6)
        used[v] = False
        for e in edge[v]:
            if used[e[1]]:
                heapq.heappush(edgelist,(e[0]+d[v])*(10**6)+e[1])
    return d


def factorization(n):
    arr = []
    temp = n
    for i in range(2, int(-(-n**0.5//1))+1):
        if temp%i==0:
            cnt=0
            while temp%i==0:
                cnt+=1
                temp //= i
            arr.append([i, cnt])

    if temp!=1:
        arr.append([temp, 1])

    if arr==[]:
        arr.append([n, 1])

    return arr


def lcm(x, y):
    return (x * y) // gcd(x, y)


def lcm_list(numbers):
    return reduce(lcm, numbers, 1)


def gcd_list(numbers):
    return reduce(gcd, numbers)


def is_prime(n):
    if n <= 1:
        return False
    p = 2
    while True:
        if p ** 2 > n:
            break
        if n % p == 0:
            return False
        p += 1
    return True



def eratosthenes(limit):
    A = [i for i in range(2, limit+1)]
    P = []

    while True:
        prime = min(A)
        
        if prime > sqrt(limit):
            break
            
        P.append(prime)
            
        i = 0
        while i < len(A):
            if A[i] % prime == 0:
                A.pop(i)
                continue
            i += 1
            
    for a in A:
        P.append(a)
            
    return P


def permutation_with_duplicates(L):

    if L == []:
        return [[]]

    else:
        ret = []

        
        S = sorted(set(L))

        for i in S:

            data = L[:]
            data.remove(i)

            for j in permutation_with_duplicates(data):
                ret.append([i] + j)

        return ret



h, w = map(int, input().split())
ans = [[] for i in range(h)]
for i in range(h):
    ans[i] = [j if j == "

x = [-1, 0, 1]  
for i in range(h):
    for j in range(w):
        if ans[i][j] == "
            continue
        for k in x:
            for l in x:
                if 0 <= i + k < h and 0 <= j + l < w:
                    if ans[i + k][j + l] == "
                        
                        ans[i][j] = str(int(ans[i][j]) + 1)

for i in ans:
    print("".join(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))


for h in range(1,H+1):
    ans = ''
    for w in range(1,W+1):
        c = 0
        if S[h][w] == '
            ans += '
        else:
            c += S[h-1][w-1:w+2].count('
            c += S[h][w-1:w+2].count('
            c += S[h+1][w-1:w+2].count('
            ans += str(c)
    print(ans)

Solution (Python):
import sys

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

def minesweeper(x, y):
    count = 0
    if S[x][y] == ".":
        for i in dx:
            for j in dy:
                nx = x + i
                ny = y + j
                if 0 <= nx < H and 0 <= ny < W and S[nx][ny] == "
                    count += 1
        S[x][y] = count
    if y + 1 != W:
        print(S[x][y], end="")
    else:
        print(S[x][y], end="\n")
        

H, W = map(int, sys.stdin.readline().split())
S = [list(sys.stdin.readline().rstrip()) for i in range(H)]

for i in range(H):
    for j in range(W):
        minesweeper(i, j)

Solution (Python):


import numpy as np

h, w = map(int, input().split())
board = np.reshape(np.array(np.repeat("@", (h+2)*(w+2))), (h+2, w+2))
for i in range(h):
    s = input()
    board[i+1, 1:w+1] = np.array(list(s))
for i in range(1, h+1):
    count = np.zeros(w, dtype=str)
    for j in range(1, w+1):
        if board[i,j] == '.':
            count[j-1] = str(\
                (board[i-1, j-1] == "
                (board[i  , j-1] == "
                (board[i+1, j-1] == "
        elif board[i, j] == "
            count[j-1] = "
    print(''.join(count))

Solution (Python):
h, w = map(int, input().split())
s=[input() for _ in range(h)]
for i in range(h):
    ss = ''
    for j in range(w):
        if s[i][j] == '
            ss += '
        else:
            counter = 0
            for k in [-1, 0, 1]:
                for l in [-1, 0, 1]:
                    if 0 <= i+k <= h-1 and 0 <= j+l <= w-1 and s[i+k][j+l] == '
                        counter += 1
            ss += str(counter)
    print(ss)

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

Solution (Python):
H,W=map(int, input().split())
one = ""
for i in range(W+2):
  one = one + "."
S_map = [one]
for i in range(H):
  s = str(input())
  S_map.append("."+s+".")
S_map.append(one)

for i in range(1,1+H):
  count = ""
  for k in range(1,1+W):
    if S_map[i][k] == "
      count = count + "
    else:
      check_map=S_map[i-1][k-1:k+2]+S_map[i+1][k-1:k+2]
      check_map=check_map+S_map[i][k-1]+S_map[i][k+1]
      count = count + str(len([1 for check in check_map if check=="
  print(count)

Solution (Python):
h,w=map(int,input().split())
s=[list("."+input()+".") for _ in range(h)]
s= [["."]*(w+2)] + s + [["."]*(w+2)] 
for i in range(1,h+1):
    l=[]
    for k in range(1,w+1):
        if s[i][k]=="
            p="
        else:
            p=0
            p+=s[i-1][k-1:k+2].count("
            p+=s[i][k-1:k+2].count("
            p+=s[i+1][k-1:k+2].count("
        l.append(str(p))
    print("".join(l))

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

for i in range(h):
    for j in range(w):
        if grid[i][j] != "
            min_i = 0 if i == 0 else i - 1
            max_i = h if i == h - 1 else i + 2
            min_j = 0 if j == 0 else j - 1
            max_j = w if j == w - 1 else j + 2
            filter = sum(list(g[min_j: max_j] for g in grid[min_i: max_i]), [])

            grid[i][j] = str(filter.count("

for g in grid:
    print("".join(g))

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

import sys
import math
import heapq
mod=10**9+7
inf=float("inf")
from math import sqrt
from collections import deque
from collections import Counter
from math import ceil
input=lambda: sys.stdin.readline().strip()
sys.setrecursionlimit(11451419)
from functools import lru_cache






def a(x,y,a,b):
    if A[x][y]=="
ans=[[0 for i in range(w)] for i in range(h)]
for i in range(h):
    for j in range(w):
        
        if A[i][j]=="
            ans[i][j]="
            continue
        else:
          
            if i!=0 and j!=0 : a(i-1,j-1,i,j)
            if i!=0 : a(i-1,j,i,j)
            if i!=0 and j!=w-1 : a(i-1,j+1,i,j)
            if j!=0 : a(i,j-1,i,j)
            if j!=w-1 : a(i,j+1,i,j)
            if i!=h-1 and j!=0 : a(i+1,j-1,i,j)
            if i!=h-1 : a(i+1,j,i,j)
            if i!=h-1 and j!=w-1 : a(i+1,j+1,i,j)

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

Solution (Python):
H, W = map(int, input().split())
S = [input() for _ in range(H)]
ad = ['.' for _ in range(W)]
S.append(ad)
S.insert(0, ad)
newS = [['
for i in range(1, H+1):
    for j in range(W):
        if S[i][j] == '.':
            c = max(0, j-1)
            d = min(W-1, j+1)
            cnt = S[i][c:d+1].count('
            cnt += S[i-1][c:d+1].count('
            cnt += S[i+1][c:d+1].count('
            newS[i-1][j] = str(cnt)
for i in newS:
    print(''.join(i))

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

Solution (Python):
h,w=map(int,input().split())
s=[list("."+input()+".") for _ in range(h)]
s= [["."]*(w+2)] + s + [["."]*(w+2)] 
for i in range(1,h+1):
    l=[]
    for k in range(1,w+1):
        if s[i][k]=="
            p="
        else:
            p=0
            p+=s[i-1][k-1:k+2].count("
            p+=s[i][k-1:k+2].count("
            p+=s[i+1][k-1:k+2].count("
        l.append(str(p))
    print("".join(l))

Solution (Python):
h,w=map(int,input().split())
s=[]
s.append("."*w)
s=[input() for _ in range(h)]
s.append("."*w)
for i in range(h):
    l=[]
    for k in range(w):
        if s[i][k]=="
            p="
        elif k==0:
            p=0
            p+=(s[i-1]+"..")[:k+2].count("
            p+=(s[i]+"..")[:k+2].count("
            p+=(s[i+1]+"..")[:k+2].count("
        else:
            p=0
            p+=(s[i-1]+"..")[k-1:k+2].count("
            p+=(s[i]+"..")[k-1:k+2].count("
            p+=(s[i+1]+"..")[k-1:k+2].count("
        l.append(str(p))
    print("".join(l))

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

for hi in range(h):
    for wi in range(w):
        if sl[hi][wi]=="
            continue
        c = 0
        for dh in range(-1,2):
            for dw in range(-1,2):
                if h>hi+dh>=0 and w>wi+dw>=0:
                    if sl[hi+dh][wi+dw]=="
                        c += 1
        sl[hi][wi] = str(c)

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

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

for h in range(H):
    for w in range(W):
        if a[h][w] == ".":
            count = 0
            if h > 0:
                if w > 0:
                    if a[h - 1][w - 1] == "
                        count += 1
                if a[h - 1][w] == "
                    count += 1
                if w < W - 1:
                    if a[h - 1][w + 1] == "
                        count += 1
            if w > 0:
                if a[h][w - 1] == "
                    count += 1
            if w < W - 1:
                if a[h][w + 1] == "
                    count += 1
            if h < H - 1:
                if w > 0:
                    if a[h + 1][w - 1] == "
                        count += 1
                if a[h + 1][w] == "
                    count += 1
                if w < W - 1:
                    if a[h + 1][w + 1] == "
                        count += 1
            a[h][w] = str(count)
for h2 in range(H):
    print("".join(a[h2]))

Solution (Python):
import sys

input = sys.stdin.readline


def main():
    H, W = [int(x) for x in input().split()]
    S = [input().strip() for _ in range(H)]

    A = [[""] * W for j in range(H)]

    for j in range(H):
        for i in range(W):
            if S[j][i] == "
                A[j][i] = '
            else:
                cnt = 0
                for dj, di in ((-1, -1), (-1, 0), (-1, 1), (0, -1), (0, 1), (1, -1), (1, 0), (1, 1)):
                    if 0 <= j + dj <= H - 1 and 0 <= i + di <= W - 1:
                        if S[j + dj][i + di] == '
                            cnt += 1

                A[j][i] = str(cnt)

    for a in A:
        print("".join(a))








if __name__ == '__main__':
    main()

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

for i in range(h):
    for j in range(w):
        if S[i][j] == '.':
            count = 0
            for x in X:
                for y in Y:
                    if (i == 0 and x == -1) or (i == h-1 and x == 1):
                        continue
                    if (j == 0 and y == -1) or (j == w-1 and y == 1):
                        continue
                    if S[i+x][j+y] == '
                        count += 1
        else:
            continue
        S[i][j] = count

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

Solution (Python):
from itertools import product

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

G=[[0] * W for h in range(H)]
P=[(y,x) for y,x in product(range(-1,2),range(-1,2)) if not (y==0 and x==0)]

for x,y in product(range(W),range(H)):
    if S[y][x]=="
        G[y][x]="
        continue

    for p in P:
        c_y=y+p[0]
        c_x=x+p[1]

        if c_x <0 or c_x>=W or c_y<0 or c_y>=H: 
            continue

        if S[c_y][c_x]=="
            G[y][x]+=1

_=[print("".join(map(str,r))) for r in G]

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

if h == 1:
    if w==1:
        if g[0][0]=="
            print("
        else:
            print("0")
    else:
        wn = ""
        for u in range(w):
            if g[0][u] == ".":
                if u==0:
                    stri = g[0][u+1]
                elif u==w-1:
                    stri = g[0][u-1]
                else:
                    stri = g[0][u-1:u+2]

                wn += str(stri.count("
            else:
                wn += "
        print(wn)

elif w == 1:
    for v in range(h):
        wn = ""
        if g[v][0]== ".":
            if v==0:
                stri = g[v+1][0]
            elif v==h-1:
                stri = g[v-1][0]
            else:
                stri = g[v-1][0]+g[v+1][0]
            wn += str(stri.count("
        else:
            wn += "
        print(wn)
        
else:
    for s in range(h):
        wn = ""
        for t in range(w):

            if g[s][t] == ".":
                if s==0:
                    if t==0:
                        stri =g[s][t+1]+g[s+1][t:t+2]
                    elif t==w-1:
                        stri = g[s][t-1]+g[s+1][t-1:t+1]
                    else:
                        stri = g[s][t-1:t+2]+g[s+1][t-1:t+2]
                        
                elif s==h-1:
                    if t==0:
                        stri = g[s-1][t:t+2]+g[s][t+1]
                    elif t==w-1:
                        stri = g[s-1][t-1:t+1]+g[s][t-1]
                    else:
                        stri = g[s-1][t-1:t+2]+g[s][t-1:t+2]
                else:
                    if t==0:
                        stri = g[s-1][t:t+2]+g[s][t+1]+g[s+1][t:t+2]
                    elif t==w-1:
                        stri = g[s-1][t-1:t+1]+g[s][t-1]+g[s+1][t-1:t+1]
                    else:
                        stri = g[s-1][t-1:t+2]+g[s][t-1]+g[s][t+1]+g[s+1][t-1:t+2]

                wn += str(stri.count("
            else:
                wn += "

        print(wn)

Solution (Python):

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

def mine(x,y,grid):
    if grid[x][y] == ".":
        cnt = 0
        for dx,dy in ((1,0),(1,1),(0,1),(-1,1),(-1,0),(-1,-1),(0,-1),(1,-1)):
            nx,ny = x + dx,y + dy
            if 0 <= nx and 0 <= ny and nx <= h-1 and ny <= w-1 and grid[nx][ny] == "
                cnt += 1
        grid[x][y] = str(cnt)    
        
for i in range(h):
    for j in range(w):
        mine(i,j,s)
        
for i in range(h):
    print("".join(s[i]))

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


new_maze = [['' for _ in range(W)] for _ in range(H)]
for i in range(1, H+1):
    for j in range(1, W+1):
        if maze[i][j] == '.':
            count = 0
            for k in [-1, 0, 1]:
                for l in [-1, 0, 1]:
                    if k == 0 and l == 0:
                        continue
                    if maze[i+k][j+l] == '
                        count += 1
            new_maze[i-1][j-1] = str(count)
        else:
            new_maze[i-1][j-1] = '
    print(*new_maze[i-1], sep='')

Solution (Python):
mine = []
h, w = map(int, input().split())
for i in range(h):
    mine.append(list(input()[:]))
ans = [[0 for i in range(w)] for j in range(h)]
for i in range(h):
    for j in range(w):
        if mine[i][j] == '
            if i != 0:
                ans[i-1][j] += 1
            if j != 0:
                ans[i][j-1] += 1
            if i != h-1:
                ans[i+1][j] += 1
            if j != w-1:
                ans[i][j+1] += 1
            if i != 0 and j != 0:
                ans[i-1][j-1] += 1
            if i != 0 and j != w-1:
                ans[i-1][j+1] += 1
            if i != h-1 and j != 0:
                ans[i+1][j-1] += 1
            if i != h-1 and j != w-1:
                ans[i+1][j+1] += 1

for i in range(h):
    for j in range(w):
        if mine[i][j] == "
            print("
        else:
            print(ans[i][j], end="")
    print()

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

S = [list(input()) for i in range(H)]

for line in S:
    line.insert(0, ".")
    line.append(".")
S.insert(0, ["."]*(W+2))
S.append(["."]*(W+2))

for i in range(1, H+1):
    for j in range(1, W+1):
        if S[i][j] == ".":
            count = S[i-1][j-1:j+2].count("
            S[i][j] = count

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

Solution (Python):
import numpy as np
h, w = map(int, input().split())
s = np.array([list(input()) for i in range(h)])
a=s
c = 0

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

for ai in a:
	print("".join(ai))

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

def check(x,y,S):
    cnt = 0
    for dx in range(-1,2):
        for dy in range(-1,2):
            sx = x + dx
            sy = y + dy
            if sx < 0 or sx >= H or sy < 0 or sy >= W:
                continue
            if S[sx][sy] == "
                cnt += 1
    S[x][y] = str(cnt)
    return S

for y in range(W):
    for x in range(H):
        if S[x][y] == "
            continue
        S = check(x,y,S)
for i in S:
    print("".join(i))

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

Step = [
    [-1, -1],
    [-1,  0],
    [-1,  1],
    [0,   1],
    [1,   1],
    [1,   0],
    [1,  -1],
    [0,  -1],
]

for i in range(H):
    for j in range(W):
        if S[i][j] == "
            C[i] += "
        else:
            ans = 0
            for one in Step:
                y = i + one[0]
                x = j + one[1]
                if 0 <= y and y < H and 0 <= x and x < W:
                    if S[y][x] == "
                        ans += 1
            C[i] += str(ans)
        
for i in range(H):
    print(C[i])

Solution (Python):
def ii():return int(input())
def iim():return map(int,input().split())
def iil():return list(map(int,input().split()))
def ism():return map(str,input().split())
def isl():return list(map(str,input().split()))

h,w = iim()
l = [['.']*(w+2)]
for i in range(h):
    l.append(list('.'+input()+'.'))
l.append(['.']*(w+2))
'''
for i in l:
    print(i)
'''
for row in range(1,h+1):
    for col in range(1,w+1):
        if l[row][col] != '
            li = [l[row-1][col-1],l[row-1][col],l[row-1][col+1],l[row][col-1],l[row][col+1],l[row+1][col-1],l[row+1][col],l[row+1][col+1]]
            l[row][col] = li.count('

for i in range(1,h+1):
    print(''.join(map(str,l[i][1:w+1])))

Solution (Python):

def myAnswer(H:int,W:int,S:list) -> None:
   ans = [["
   index = []
   for i,s in enumerate(S):
      tmp = ["
      for j,b in enumerate(s):
         if(b == "."):
            tmp.append(0)
         else:
            index.append([i+1,j+1])
            tmp.append(b)
      tmp.append("
      ans.append(tmp)
   ans.append(["
   for i,j in index:
      for n in range(i-1,i+2,1):
         for m in range(j-1,j+2,1):
            if(ans[n][m]!="
               ans[n][m] +=1
   ans.pop(0)
   ans.pop()
   for a in ans:
      a.pop(0)
      a.pop(-1)
      string = ""
      for s in a:
         string += str(s)
      print(string)
      
def main():
   H,W = map(int,input().split())
   S = [list(input()) for _ in range(H)]
   myAnswer(H,W,S[:])
if __name__ == '__main__':
   main()

Solution (Python):
H, W = map(int, input().split())
s = [list(input()) for i in range(H)]
for i in range(H):
    for j in range(W):
        tmp = 0
        if s[i][j] == '.':
            if i >= 1 and j >= 1:
                if s[i-1][j-1] == '
            if i >= 1:
                if s[i-1][j] == '
            if i >= 1 and j < W-1:
                if s[i-1][j+1] == '
            if j >= 1:
                if s[i][j-1] == '
            if j < W-1:
                if s[i][j+1] == '
            if i < H-1 and j >= 1:
                if s[i+1][j-1] == '
            if i < H-1:
                if s[i+1][j] == '
            if i < H-1 and j < W-1:
                if s[i+1][j+1] == '

            s[i][j] = str(tmp)

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

Solution (Python):
import sys
import numpy as np

h, w = map(int, input().split())
s = np.array([list(input()) for i in range(h)])

bomb = np.array(np.where(s == '
n = np.zeros(s.shape, dtype=np.int64)
for k in bomb:
    i, j = k
    n[max(0,i-1), max(0,j-1)] += 1
    try: n[i+2, max(0,j-1)] -= 1
    except IndexError: pass
    try: n[max(0,i-1), j+2] -= 1
    except IndexError: pass
    try: n[i+2, j+2] += 1
    except IndexError: pass
n = np.cumsum(np.cumsum(n,axis=0), axis=1)
ans = np.where(s == '
for row in ans:
    print(*row, sep='')

Solution (Python):
from collections import Counter
import numpy as np
h, w = list(map(int, input().split()))
s = np.array([list(input()) for i in range(h)])

cnt = 0
ans = [[] for i in range(h)]
for i in range(h):
  for j in range(w):
    if s[i][j] == ".":
      xm = max(0, j-1)
      ym = max(0, i-1)
      xp = min(w-1, j+1)
      yp = min(h-1, i+1)
      temp = s[ym:yp+1, xm:xp+1].reshape(-1).tolist()
      ans[i].append(str(Counter(temp)["
     
    else:
      ans[i].append("
      
for ai in ans:
  print("".join(ai))

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

C = [[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] == "
            C[i][j] ="
        else:
            bottomi = i - 1 if i > 0 else 0
            bottomj = j - 1 if j > 0 else 0
            topi = i + 1 if i < H-1 else H-1
            topj = j + 1 if j < W-1 else W-1
            cnt = 0
            for k in range(bottomi,topi+1):
                for l in range(bottomj,topj+1):
                    if S[k][l] == "
                        C[i][j] += 1

for i in range(H):
    ST = ""
    for j in range(W):
        ST +=str(C[i][j])
    else:
        print(ST)

Solution (Python):
def resolve():
    H, W = list(map(int, input().split()))
    S = [input() for i in range(H)]
    A = []
    for i in range(H):
        line = ""
        for j in range(W):
            if S[i][j] == "
                line += "
                continue
            cnt = 0
            if i-1 >= 0:
                cnt += 1 if j-1 >= 0 and S[i-1][j-1] == "
                cnt += 1 if S[i-1][j] == "
                cnt += 1 if j+1 < W and S[i-1][j+1] == "

            cnt += 1 if j-1 >= 0 and S[i][j-1] == "
            cnt += 1 if S[i][j] == "
            cnt += 1 if j+1 < W and S[i][j+1] == "

            if i+1 < H:
                cnt += 1 if j-1 >= 0 and S[i+1][j-1] == "
                cnt += 1 if S[i+1][j] == "
                cnt += 1 if j+1 < W and S[i+1][j+1] == "
            
            line += str(cnt)
        A.append(line)
    
    [print(l) for l in A]

            

if '__main__' == __name__:
    resolve()

Solution (Python):
def resolve():
    '''
    code here
    '''
    H, W = [int(item) for item in input().split()]
    grid = [input() for _ in range(H)]

    res = ['' for _ in range(H)]



    def cnt_bomb(i, j):
        cnt = 0        

        for n in [-1, 0, 1]:
            for m in [-1, 0, 1]:
                cnt += chk_bomb(i+n, j+m)

        return str(cnt)


    def chk_bomb(i, j):

        if i < 0 or  H <= i or j < 0 or W <= j:
            return 0
        elif grid[i][j] == '
            return 1
        else:
            return 0



    for i in range(H):
        for j in range(W):
            if grid[i][j] == '
                res[i] += '
            else:
                res[i] += cnt_bomb(i, j)

    for line in res:
        print(line)

if __name__ == "__main__":
    resolve()

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


for i in range(h):
    for j in range(w):
        if s[i][j]=="
            print("
        else:
            cnt=0
            for x in range(-1,2):
                for y in range(-1,2):
                    if 0<=i+x<h and 0<=j+y<w and s[i+x][j+y]=="
                        cnt+=1
            print(cnt,end="")
    print()

Solution (Python):
H,W = map(int,input().split())
S = [input() for i in range(H)]
T = [[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]=="
            T[i][j]="
            for il in range(max(0,i-1),min(i+1,H-1)+1):
                for jl in range(max(0, j-1), min(j+1, W-1)+1):
                    
                    if S[il][jl] is not "
                        T[il][jl]+=1


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

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

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

Solution (Python):
H,W=map(int,input().split())
S=[input() for _ in range(H)]
for i in range(H):
  T=''
  for j in range(W):
    if S[i][j] == '
      T+='
    else:
      T+=str(sum([x[max(0,j-1):min(j+2,W)].count('
    
  print(T)

Solution (Python):
H,W = map(int,input().split())
S = [input() for _ in range(H)]
answer = [[0 if cell == '.' else '

import itertools
for i in range(H):
  for j in range(W):
    if S[i][j] != '
      continue
    for dx,dy in itertools.product([-1,0,1],repeat=2):
      x = i+dx
      y = j+dy
      if x < 0 or x >= H:
        continue
      if y < 0 or y >= W:
        continue
      if S[x][y] == '
        continue
      answer[x][y] += 1

      
for row in answer:
  print(*row,sep='')