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 = []
for i in range(H):
    a = list(input())
    s.append(a)
for i in range(H):
    for j in range(W):
        count = 0
        if s[i][j] == ".":
            for k in range(i-1,i+2):
                for l in range(j-1,j+2):
                    if (0 <= k <= H-1) and (0 <= l <= W-1) and s[k][l] == "
                        count += 1
            s[i][j] = str(count)
for i in range(H):
    print("".join(s[i]))

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

for x,y in itertools.product(range(W),range(H)):
    if S[y][x]==".":
        res=0
        for i,j in itertools.product(range(-1,2),range(-1,2)):
            if 0<=x+i and x+i<W and 0<=y+j and y+j<H:
                if S[y+j][x+i]=="
                    res+=1
        
        S[y][x]=res

for j in range(H):
    L=[str(a) for a in S[j]]
    print(''.join(L))

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

Solution (Python):
import sys

input = sys.stdin.readline

def main():
    ans = []
    b = "
    h, w = map(int, input().split())
    s = [input().rstrip('\n') for _ in range(h)]
    for y in range(h):
        ans.append([])
        for x in range(w):
            if s[y][x] == b:
                ans[y].append(b)
            else:
                ans[y].append(0)
    for y in range(h):
        for x in range(w):
            if s[y][x] == b:
                if y > 0 and x > 0 and ans[y-1][x-1] != b:
                    ans[y-1][x-1] += 1
                if y > 0 and ans[y-1][x] != b:
                    ans[y-1][x] += 1
                if y > 0 and x < w - 1 and ans[y-1][x+1] != b:
                    ans[y-1][x+1] += 1
                if x > 0 and ans[y][x-1] != b:
                    ans[y][x-1] += 1
                if x < w - 1 and ans[y][x+1] != b:
                    ans[y][x+1] += 1
                if y < h - 1 and x > 0 and ans[y+1][x-1] != b:
                    ans[y+1][x-1] += 1
                if y < h - 1 and ans[y+1][x] != b:
                    ans[y+1][x] += 1
                if y < h - 1 and x < w - 1 and ans[y+1][x+1] != b:
                    ans[y+1][x+1] += 1
    for y in range(h):
        for x in range(w):
            if ans[y][x] == 0 and s[y][x] == b:
                ans[y][x] = "
            else:
                ans[y][x] = str(ans[y][x])
        print(''.join(ans[y]))

if __name__ == '__main__':
    main()

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

ans = [[0]*W for _ in range(H)]
for i in range(H):
    for j in range(W):
        if S[i][j] == "
            ans[i][j] = "
        else:
            c = 0
            for dx in [-1,0,1]:
                for dy in [-1,0,1]:
                    if 0 <= i+dy <= H-1 and 0 <= j+dx <= W-1 and S[i+dy][j+dx] == "
                        c += 1
            ans[i][j] = str(c) 
for i in range(H):
    output = "".join(ans[i])
    print(output)

Solution (Python):
H,W = map(int,input().split())
s = []
for i in range(H):
    a = list(input())
    s.append(a)
for i in range(H):
    for j in range(W):
        count = 0
        if s[i][j] == ".":
            for k in range(i-1,i+2):
                for l in range(j-1,j+2):
                    if (0 <= k <= H-1) and (0 <= l <= W-1) and s[k][l] == "
                        count += 1
            s[i][j] = str(count)
for i in range(H):
    print("".join(s[i]))

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

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

dh = [0,1,-1,0,1,-1,-1,1]
dw = [1,0,0,-1,1,1,-1,-1]

for h in range(H):
    for w in range(W):
        if S[h][w] == "
            continue
        else:
            cnt = 0
            for k in range(8):
                next_h = h+dh[k]
                next_w = w+dw[k]
                if not(0<= next_h <= H-1) or not(0<= next_w <= W-1):
                    continue
                elif S[next_h][next_w] == "
                    cnt += 1
        S[h][w] = str(cnt)
        
for s in S:
    print("".join(s))

Solution (Python):
H, W = (int(x) for x in input().split())
S = [input() for _ in range(H)]
ans = [''] * H  
for i in range(H):
    for j in range(W):
        if S[i][j] == '
            ans[i] += '
            continue
        bom = 0
        for k in range(3):
            for l in range(3):
                if 0 <= i-1+k < H and 0 <= j-1+l < W:
                    if S[i-1+k][j-1+l] == '
                        bom += 1
        ans[i] += str(bom)
for s in ans:
    print(s)

Solution (Python):

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

dh = [0,1,0,-1,1,-1,1,-1]
dw = [1,0,-1,0,1,-1,-1,1]

for h in range(H):
    for w in range(W):
        
        if S[h][w] == '
            continue
        else:
            cnt= 0
            for v in range(8):
                nxt_h = h + dh[v]
                nxt_w = w + dw[v]
                if not(-1<nxt_h<H) or not(-1<nxt_w<W):
                    continue
                elif S[nxt_h][nxt_w] == '
                    cnt += 1
                S[h][w] = str(cnt)
if H==1 and W==1:
    print(0)
else:
    for i in S:
        print(''.join(i))

Solution (Python):
def sarch(i,j):
    cnt=0
    i1=[i-1,i,i+1]
    j1=[j-1,j,j+1]
    for x in range(3):
        for y in range(3):
            if S[i1[x]][j1[y]]=="
                cnt+=1
    return cnt

import numpy as np
H,W=map(int,input().split())
S=np.array([list(str(input())) for i in range(H)])


gyo_0=np.array(["."]*W)
retu_0=np.array([["."]]*(H+2))

S=np.vstack((gyo_0,S))
S=np.vstack((S,gyo_0))
S=np.hstack((retu_0,S))
S=np.hstack((S,retu_0))

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

Solution (Python):
h,w=map(int,input().split())
s=[input() for i in range(h)]
ss=[[0 for _ in range(w+2)] for _ in range(h+2)]
for j in range(w):
    for i in range(h):
        ss[i+1][j+1]=s[i][j]
s=[[0 for _ in range(w)] for _ in range(h)]
for j in range(1,w+1):
    for i in range(1,h+1):
        for a in range(-1,2):
            for b in range(-1,2):
                if a==0 and b ==0:
                    continue
                if ss[i+a][j+b]=='
                    s[i-1][j-1]+=1
for j in range(w):
    for i in range(h):
        if ss[i+1][j+1]=='
            s[i][j]='
for i,d in enumerate(s):
    d=map(str,d)
    print(''.join(d))

Solution (Python):
import bisect,collections,copy,itertools,math,string
import sys
def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())
def main():
    h,w = LI()
    board = [["." for _ in range(w+2)]]
    for _ in range(h):
        s = S()
        s = list(s)
        s = ["."] + s + ["."]
        board.append(s)
    board.append(["." for _ in range(w+2)])

    exp = [[1,0],[0,1],[-1,0],[0,-1],[1,1],[1,-1],[-1,1],[-1,-1]]
    for y in range(1,h+1):
        for x in range(1,w+1):
            if board[y][x] == ".":
                cnt = 0
                for u,r in exp:
                    if board[y-u][x-r] == "
                        cnt += 1
                board[y][x] = str(cnt)

    for line in board[1:-1]:
        line = "".join(line[1:-1])
        print(line)
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):
        if S[i][j]=="
            continue
        else:
            count = 0
            for k in range(-1,2,1):
                for l in range(-1,2,1):
                    if j+k<0 or i+l<0 or j+k>=W or i+l>=H:
                        continue
                    if S[i+l][j+k]=="
                        count+=1
            S[i][j]=str(count)

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

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):
        
        c = 0

        
        if l[i][j]=='
            pass
        
        
        else:
            
            if 0<i and 0<j and l[i-1][j-1]=='
                c+=1
            
            if 0<i and l[i-1][j]=='
                c+=1
            
            if 0<i and j<W-1 and l[i-1][j+1]=='
                c+=1
            
            if 0<j and l[i][j-1]=='
                c+=1
            
            if j<W-1 and l[i][j+1]=='
                c+=1
            
            if i<H-1 and 0<j and l[i+1][j-1]=='
                c+=1
            
            if i<H-1 and j<W-1 and l[i+1][j+1]=='
                c+=1
            
            if i<H-1 and l[i+1][j]=='
                c+=1

            
            l[i][j]=str(c)


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

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

Solution (Python):

import numpy as np
import sys
from collections import deque
from collections import defaultdict
import heapq
import collections
import itertools
import bisect
from scipy.special import comb
import copy
sys.setrecursionlimit(10**6)


def zz():
    return list(map(int, sys.stdin.readline().split()))


def z():
    return int(sys.stdin.readline())


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


def C(line):
    return [sys.stdin.readline() for _ in range(line)]


H, W = zz()
table = []
for i in range(H):
    table.append(input())
ans_table = [[0]*W for _ in range(H)]
dx = [-1, 0, 1, -1, 1, -1, 0, 1]
dy = [-1, -1, -1, 0, 0, 1, 1, 1]
for i in range(H):
    for j in range(W):
        tmp = 0
        if (table[i][j] == '.'):
            for x, y in zip(dx, dy):
                if (i + x >= H or j+y >= W or i + x < 0 or j+y < 0):
                    continue
                if (table[i + x][j + y] == '
                    tmp += 1
            ans_table[i][j] = tmp
        else:
            ans_table[i][j] = '


for _table in ans_table:
    print(*_table, sep='')

Solution (Python):
import sys
import re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import accumulate, permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
INPUT = lambda: sys.stdin.readline().rstrip()
INT = lambda: int(INPUT())
MAP = lambda: map(int, INPUT().split())
S_MAP = lambda: map(str, INPUT().split())
LIST = lambda: list(map(int, INPUT().split()))
S_LIST = lambda: list(map(str, INPUT().split()))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

H, W = MAP()
S = ['.'*(W + 2), *['.' + INPUT() + '.' for _ in range(H)], '.'*(W + 2)]

ans = ["" for _ in range(H)]
dy = [0, -1, -1, -1, 0, 1, 1, 1]
dx = [1, 1, 0, -1, -1, -1, 0, 1]
for i in range(H):
    for j in range(W):
        if S[i+1][j+1] == '
            ans[i] += '
            continue

        cnt = 0
        for y, x in zip(dy, dx):
            if S[i+1+y][j+1+x] == '
                cnt += 1

        ans[i] += str(cnt)

print(*ans, sep='\n')

Solution (Python):
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, gcd
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from functools import reduce
from bisect import bisect_left, insort_left
from heapq import heapify, heappush, heappop

INPUT = lambda: sys.stdin.readline().rstrip()
INT = lambda: int(INPUT())
MAP = lambda: map(int, INPUT().split())
S_MAP = lambda: map(str, INPUT().split())
LIST = lambda: list(map(int, INPUT().split()))
S_LIST = lambda: list(map(str, INPUT().split()))

sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7


def main():
    H, W = MAP()
    S = ['.'*(W + 2), *['.' + INPUT() + '.' for _ in range(H)], '.'*(W + 2)]

    ans = ["" for _ in range(H)]
    dy = [0, -1, -1, -1, 0, 1, 1, 1]
    dx = [1, 1, 0, -1, -1, -1, 0, 1]
    for i in range(H):
        for j in range(W):
            if S[i+1][j+1] == '
                ans[i] += '
                continue

            cnt = 0
            for y, x in zip(dy, dx):
                if S[i+1+y][j+1+x] == '
                    cnt += 1

            ans[i] += str(cnt)

    print(*ans, sep='\n')


if __name__ == '__main__':
    main()

Solution (Python):
H,W=map(int,input().split())
s=[input() for _ in range(H)]
S=[]
S.append(['.' for _ in range(W+2)])
for i in range(H):
    s[i]='.'+s[i]+'.'
    S.append(list(s[i]))
S.append(['.' for _ in range(W+2)])
for i in range(1,H+1):
    for j in range(1,W+1):
        cnt=0
        if S[i][j]=='.':
            for k in range(-1,2):
                for l in range(-1,2):
                    if S[i+k][j+l]=='
                        cnt+=1
            S[i][j]=str(cnt)
for i in range(1,H+1):
    print(''.join(S[i][1:W+1]))

Solution (Python):
import math
import numpy as np
import sys
import os
from operator import mul

sys.setrecursionlimit(10**7)

def _S(): return sys.stdin.readline().rstrip()
def I(): return int(_S())
def LS(): return list(_S().split())
def LI(): return list(map(int,LS()))

if os.getenv("LOCAL"):
    inputFile = basename_without_ext = os.path.splitext(os.path.basename(__file__))[0]+'.txt'
    sys.stdin = open(inputFile, "r")
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7




H,W = LI()
S = [list(_S()) for _ in range (H)]


def replace(x, y):
    
    count = 0
    for dx in range(-1, 2):
        for dy in range(-1, 2):
            
            nx = x + dx
            ny = y + dy

            if 0 <= nx and nx < H and 0 <= ny and ny < W and S[nx][ny] == "
                count += 1
    S[x][y]=str(count)


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

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

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):
        ans = 0
        if S[i][j]=='.':
            for h in range(max(0,i-1),min(H,i+2)):
                for w in range(max(0,j-1),min(W,j+2)):
                    if S[h][w]=='
                        ans += 1
            S[i][j] = str(ans)
        
for i in range(H):
    print(''.join(S[i]))

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

for ch in range(1, h+1):
    for cw in range(1, w+1):
        if f[ch][cw] == '
            continue
        cnt = 0
        for hh in range(ch-1, ch+2,1):
            cnt += f[hh][cw-1:cw+2].count('
        ans[ch-1][cw-1] = str(cnt)
for i in range(h):
    print(''.join(ans[i]))

Solution (Python):
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
from bisect import bisect, bisect_left, bisect_right

def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
 
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

H, W = MAP()
C = [input() for i in range(H)]
C = ["0"*(W+2)] + ["0"+C[i]+"0" for i in range(H)] + ["0"*(W+2)]
Z = [[0] * (W + 2) for i in range(H + 2)]

def surround_plus(i, j, Z):
    dx = [0, 1, 1, 1, 0,-1,-1,-1]
    dy = [1, 1, 0,-1,-1,-1, 0, 1]
    for idx in range(8):
        Z[i + dx[idx]][j + dy[idx]] += 1
    return Z


for i in range(H + 2):
    for j in range(W + 2):
        if C[i][j] == "
            Z = surround_plus(i, j, Z)

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

for i in range(1, H + 1):
    for j in range(1, W + 1):
        if C[i][j] != "
            A[i-1][j-1] = str(Z[i][j])
        else:
            A[i-1][j-1] = "
print(*["".join(a) for a in A], sep="\n")

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

S.append(['x'] * (W+2))
around = [[-1,-1],[-1,0],[-1,1],[0,1],[1,1],[1,0],[1,-1],[0,-1]]
for i in range(H+1) :
    for j in range(W+1) :
        if S[i][j] == '.' :

            count = 0
            for k in range(8) :
                if S[i+around[k][0]][j+around[k][1]] == '


                    count += 1
            S[i][j] = count

ans = ['']* H
for i in range(1,H+1) :
    for j in range(1,W+1) :
        ans[i-1] += str(S[i][j])
    print(ans[i-1])

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

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

            cnt = 0
            for k in range(8):
                x = j + dx[k]
                y = i + dy[k]
                if x >= 0 and x < W and y >= 0 and y < H:
                    if S[y][x] == "
                        cnt += 1

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

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


if __name__ == "__main__":
    main()

Solution (Python):
def main():
    H, W = map(int, input().split())
    S = [["*" for _ in range(W + 2)] for _ in range(H + 2)]
    for i in range(1, H + 1):
        S[i][1: W + 1] = input()
    dx = [-1, 0, 1, -1, 1, -1, 0, 1]
    dy = [-1, -1, -1, 0, 0, 1, 1, 1]

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

            cnt = 0
            for k in range(8):
                if S[i + dx[k]][j + dy[k]] == "
                    cnt += 1

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

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


if __name__ == "__main__":
    main()

Solution (Python):
def main():
    H, W = map(int, input().split())
    S = [["*" for _ in range(W + 2)] for _ in range(H + 2)]
    for i in range(1, H + 1):
        S[i][1: W + 1] = input()

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

            cnt = 0
            if S[i - 1][j - 1] == "
                cnt += 1
            if S[i - 1][j] == "
                cnt += 1
            if S[i - 1][j + 1] == "
                cnt += 1
            if S[i][j - 1] == "
                cnt += 1
            if S[i][j + 1] == "
                cnt += 1
            if S[i + 1][j - 1] == "
                cnt += 1
            if S[i + 1][j] == "
                cnt += 1
            if S[i + 1][j + 1] == "
                cnt += 1

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

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


if __name__ == "__main__":
    main()

Solution (Python):
H,W=map(int,input().split())
S=[list(map(str,input().split())) for i in range(H)]
ans=[[0 for i in range(W+2)]for j in range(H+2)]
inf=10
for i in range(H):
    S[i]=list(S[i][0])
for i in range(W):
    for j in range(H):
        if S[j][i]=='
            ans[j+1][i+1]=inf

for i in range(W):
    for j in range(H):
        if S[j][i]=='
            ans[j][i+1]+=1
            ans[j][i+2]+=1
            ans[j][i]+=1
            ans[j+2][i+1]+=1
            ans[j+1][i]+=1
            ans[j+1][i+2]+=1
            ans[j+2][i+2]+=1
            ans[j+2][i]+=1
del(ans[-1])
del(ans[0])
for i in range(len(ans)):
    del(ans[i][-1])
    del(ans[i][0])
for i in range(W):
    for j in range(H):
        if ans[j][i]>=inf:
            ans[j][i]='
        else:
            ans[j][i]=str(ans[j][i])

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

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

for i in range(1,h+1):
  for j in range(1,w+1):
    if s[i][j]=='.':
      t=s[i-1][j-1:j+2]+s[i][j-1:j+2]+s[i+1][j-1:j+2]
      s[i]=s[i][:j]+str(t.count('
  print(s[i][1:-1])

Solution (Python):
n,m=map(int,input().split())
r=[[*input()] for _ in range(n)]
ans=[[[0] for _ in range(m)] for _ in range(n)]
dir=[[0,0,1,1,1,-1,-1,-1],[-1,1,-1,0,1,-1,0,1]]
for i in range(n):
  for j in range(m):
    count=0
    if r[i][j]=='
      ans[i][j]='
      continue
    for k in range(8):
      dx,dy=i+dir[0][k],j+dir[1][k]
      if 0<=dx<n and 0<=dy<m and r[dx][dy]=='
        count+=1
    ans[i][j]=count
[print(*i,sep='') for i in ans]

Solution (Python):
n,m=map(int,input().split())
r=[[*input()] for _ in range(n)]
ans=[[[0] for _ in range(m)] for _ in range(n)]
dir1=[0,0,1,1,1,-1,-1,-1]
dir2=[-1,1,-1,0,1,-1,0,1]
for i in range(n):
  for j in range(m):
    count=0
    if r[i][j]=='
      ans[i][j]='
      continue
    for k in range(8):
      dx = i + dir1[k]
      dy = j + dir2[k]
      if 0<=dx<n and 0<=dy<m and r[dx][dy]=='
        count+=1
    ans[i][j]=count
[print(*i,sep='') for i in ans]

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

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

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

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

        else:
            a = 0
            if i > 0:
                if S[i-1][j] == "
                    a += 1
                    
                if j > 0:
                    if S[i-1][j-1] == "
                        a += 1

                if j < W-1:
                    if S[i-1][j+1] == "
                        a += 1

            if j > 0:
                if S[i][j-1] == "
                    a += 1

            if j < W-1:
                if S[i][j+1] == "
                    a += 1

            if i < H-1:
                if S[i+1][j] == "
                    a += 1
                    
                if j > 0:
                    if S[i+1][j-1] == "
                        a += 1

                if j < W-1:
                    if S[i+1][j+1] == "
                        a += 1

            ans[i][j] = str(a)

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

Solution (Python):
import numpy as np
from scipy import signal

H, W =map(int, input().split()) 
S = np.array([list(input()) for _ in range(H)])
K = np.array([[1,1,1],[1,0,1],[1,1,1]])
T = signal.convolve2d(S=="
T[S=="
for t in T:
    print("".join(t))

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

s = [input() for _ in range(h)]
ans = ['' for _ in range(h)]

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

for t in ans:
    print(t)

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

ans = []

for h in range(1, H + 1):
    t = ''
    cnt = 0
    for w in range(1, W + 1):
        if S[h - 1][w] == '
            cnt += 1
        if S[h + 1][w] == '
            cnt += 1
        if S[h][w - 1] == '
            cnt += 1
        if S[h][w + 1] == '
            cnt += 1
        if S[h - 1][w - 1] == '
            cnt += 1
        if S[h + 1][w + 1] == '
            cnt += 1
        if S[h + 1][w - 1] == '
            cnt += 1
        if S[h - 1][w + 1] == '
            cnt += 1

        if S[h][w] == '
            t += '
        else:
            t += str(cnt)
        cnt = 0
    ans.append(t)

for i in ans:
    print(i)

Solution (Python):


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

ans = []

for h in range(1, H + 1):
    t = ''
    cnt = 0
    for w in range(1, W + 1):
        if S[h - 1][w] == '
            cnt += 1
        if S[h + 1][w] == '
            cnt += 1
        if S[h][w - 1] == '
            cnt += 1
        if S[h][w + 1] == '
            cnt += 1
        if S[h - 1][w - 1] == '
            cnt += 1
        if S[h + 1][w + 1] == '
            cnt += 1
        if S[h + 1][w - 1] == '
            cnt += 1
        if S[h - 1][w + 1] == '
            cnt += 1

        if S[h][w] == '
            t += '
        else:
            t += str(cnt)
        cnt = 0
    ans.append(t)

for i in ans:
    print(i)

Solution (Python):




import sys
from io import StringIO
import unittest


class TestClass(unittest.TestCase):
    def assertIO(self, input, output):
        stdout, stdin = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(input)
        resolve()
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, stdin
        self.assertEqual(out, output)

    def test_入力例_1(self):
        input = """3 5
.....
.
....."""
        output = """11211
1
11211"""
        self.assertIO(input, output)

    def test_入力例_2(self):
        input = """3 5



        output = """


        self.assertIO(input, output)

    def test_入力例_3(self):
        input = """6 6



.


        output = """


4


        self.assertIO(input, output)


def resolve():
    H, W = map(int, input().split())
    S = [list(input()) for _ in range(H)]
    A = [[0] * (W+2) for _ in range(H+2)]

    for y in range(H):
        for x in range(W):
            if S[y][x] == "
                for i in range(y, y+3):
                    for j in range(x, x+3):
                        A[i][j] += 1

    for y in range(H):
        for x in range(W):
            if S[y][x] == "
                print("
            else:
                print(A[y+1][x+1], end="")
        print()


if __name__ == "__main__":
    
    resolve()

Solution (Python):
H,W=map(int,input().split())
L=[[] for i in range(H)]
for i in range(H):
  L1=list(input())
  L[i]=L1
L=[["$"]+i+["$"] for i in L]
L=[["$" for i in range(W+2)]]+L+[["$" for i in range(W+2)]]
for i in range(1,H+1):
  for j in range(1,W+1):
    if L[i][j]==".":
      L[i][j]=str([L[i][j+1],L[i-1][j+1],L[i+1][j+1],L[i-1][j-1],L[i+1][j],L[i-1][j],L[i][j-1],L[i+1][j-1]].count("
for i in range(1,H+1):
  print("".join(L[i][1:-1]))

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):
    for j in range(w):
        ans[i][j]=str(ans[i][j])
        print(ans[i][j],end="")
    print('')

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

d = [-1, 0, 1]
for h in range(H):
    for w in range(W):
        if S[h][w] == '
            continue
        else:
            count = 0
            for i in range(3):
                for j in range(3):
                    hs = h + d[i]
                    ws = w + d[j]
                    if 0 <= hs < H and 0 <= ws < W and S[hs][ws] == '
                        count += 1
                    else:
                        continue
            S[h][w] = str(count)

ans_row = [''.join(S[h]) for h in range(H)]
print('\n'.join(ans_row))

Solution (Python):
H, W = map(int,input().split(' '))
S = []
dj = [-1, -1, -1, 0, 0, 1, 1, 1]
dk = [-1, 0, 1, -1, 1, -1, 0, 1]

for i in range(H):
  S.append(input())
for j in range(H):
  answer = list(S[j])
  for k in range(W):
    cnt = 0
    if S[j][k] == '
      pass
    if S[j][k] == '.':
      for d in range(8):
        if 0 <= j+dj[d] <= H-1:
          if 0 <= k+dk[d] <= W-1:
            if S[j + dj[d]][k+dk[d]] == '
              cnt += 1
      answer[k] = str(cnt)
  print(''.join(answer))