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):
s = input().split()
h = int(s[0])
w = int(s[1])

mine = [[0 for j in range(w)] for i in range(h)]



for j in range(h):
    p = input()
    q = list(p)
    for i in range(w):
        mine[j][i] = str(q[i])

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

for i in range(h):
    for j in range(w):
        if mine[i][j] == '
            continue

        num = 0
        for d in range(len(dx)):
            ni = i + dy[d]
            nj = j + dx[d]

            if ni < 0 or h <= ni:
                continue
            if nj < 0 or w <= nj:
                continue
            if mine[ni][nj] == '
                num += 1
        
        
        mine[i][j] = str(num)

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

print('')

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


def search(th, tw):
    t = 0
    for i in range(-1, 2):
        for j in range(-1, 2):
            if i == j == 0:
                continue
            if not 0 <= th+i < h:
                continue
            if not 0 <= tw+j < w:
                continue
            if ls[th+i][tw+j] == "
                t += 1
    return t


ans = ""
for i in range(h):
    for j in range(w):
        if ls[i][j] == "
            ans += "
        else:
            ans += str(search(i, j))
    ans += "\n"

print(ans)

Solution (Python):
from collections import deque
H, W = map(int,input().split())
sl = []
for _ in range(H):
    sl.append(list(input()))



for i in range(H):
    for j in range(W):
        if sl[i][j] == '
            continue

        cnt = 0
        directions = [[1, 0], [0, 1], [-1, 0], [0, -1], [1, 1], [-1, -1], [1, -1], [-1, 1]]
        for d in directions:
            y,x = i+d[0],j+d[1]
            if i+d[0] >= H:
                continue
            if i+d[0] < 0:
                continue
            if j+d[1] >= W:
                continue
            if j + d[1] < 0:
                continue
            if sl[y][x] == '
               cnt += 1
        sl[i][j] = str(cnt)

for v in sl:
    print(''.join(v))

Solution (Python):
import sys
sys.setrecursionlimit(4100000)
import math
INF = 10**9

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

    for i in range(h):
        for j in range(w):
            if S[i][j] == '
                for k in [-1, 0, 1]:
                    for l in [-1, 0, 1]:
                        if k == l == 0:
                            ans[i+1][j+1] = '
                        if ans[i+k+1][j+l+1] != '
                            ans[i+k+1][j+l+1] += 1
    for i in range(1,h+1):
        tmp = ''
        for j in range(1,w+1):
            tmp += str(ans[i][j])
        print(tmp)


if __name__ == '__main__':
    main()

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

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

Solution (Python):
H, W = map(int, input().split())
S = [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] += 11


if W == 1:
    for i in range(H-1):
        if ans[i+1][0] > 10:
            ans[i][0] += 1
    for i in range(1, H):
        if ans[i-1][0] > 10:
            ans[i][0] += 1

    for i in range(H):
        if ans[i][0] > 10:
            ans[i][0] = "
        ans[i] = map(str, ans[i])
        ans[i] = "".join(ans[i])

    for a in ans:
        print(a)
    exit()
 

for i in range(H):
    for j in range(W - 1):
        if ans[i][j+1] > 10:
            ans[i][j] += 1
    for j in range(1, W):
        if ans[i][j-1] > 10:
            ans[i][j] += 1
 

for j in range(1, W-1):
    for i in range(H-1):
        if ans[i+1][j-1] > 10:
            ans[i][j] += 1
        if ans[i+1][j] > 10:
            ans[i][j] += 1
        if ans[i+1][j+1] > 10:
            ans[i][j] += 1
 
    for i in range(1, H):
        if ans[i-1][j-1] > 10:
            ans[i][j] += 1
        if ans[i-1][j] > 10:
            ans[i][j] += 1
        if ans[i-1][j+1] > 10:
            ans[i][j] += 1
 

for i in range(H-1):
    if ans[i+1][0] > 10:
        ans[i][0] += 1
    if ans[i+1][1] > 10:
        ans[i][0] += 1
for i in range(1, H):
    if ans[i-1][0] > 10:
        ans[i][0] += 1
    if ans[i-1][1] > 10:
        ans[i][0] += 1
 

for i in range(H-1):
    if ans[i+1][W-1] > 10:
        ans[i][W-1] += 1
    if ans[i+1][W-2] > 10:
        ans[i][W-1] += 1
for i in range(1, H):
    if ans[i-1][W-1] > 10:
        ans[i][W-1] += 1
    if ans[i-1][W-2] > 10:
        ans[i][W-1] += 1
 
for i in range(H):
    for j in range(W):
        if ans[i][j] > 10:
            ans[i][j] = "
    ans[i] = map(str, ans[i])
    ans[i] = "".join(ans[i])
 
for a in ans:
    print(a)

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

around=[[0,1],[0,-1],[1,0],[-1,0],[1,1],[1,-1],[-1,1],[-1,-1]]

for i in range(h):
  for j in range(w):
    x,y=i,j
    if s[x][y]==".":
      cnt=0
      for a in around:
        if (x+a[0]<0 or x+a[0]>h-1) or (y+a[1]<0 or y+a[1]>w-1):
          continue
        if s[x+a[0]][y+a[1]]=="
          cnt+=1
      ans[x][y]=str(cnt)
    

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

Solution (Python):

import sys
h, w = map(int, input().split())
s = [[c for c in l.strip()] for l in sys.stdin]



for i in range(h):
    line = ""
    for j in range(w):
        
        cnt = 0

        if s[i][j] == "
            line += "
        else:
            
            if i != 0:
                if j != 0:
                    if s[i - 1][j - 1] == "
                        cnt += 1
                if s[i - 1][j] == "
                    cnt += 1
                if j != w - 1:
                    if s[i - 1][j + 1] == "
                        cnt += 1

            
            if j != 0:
                if s[i][j - 1] == "
                    cnt += 1
            if j != w - 1:
                if s[i][j + 1] == "
                    cnt += 1

            
            if i != h - 1:
                if j != 0:
                    if s[i + 1][j - 1] == "
                        cnt += 1
                if s[i + 1][j] == "
                    cnt += 1
                if j != w - 1:
                    if s[i + 1][j + 1] == "
                        cnt += 1
            line += str(cnt)
    print(line)

Solution (Python):
import sys

def I(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def main():
    h, w = MI()
    s = [input() for _ in range(h)]
    table = [[0]*(w+2) for _ in range(h+2)]
    for i in range(1, h+1):
        for j in range(1, w+1):
            table[i][j] = s[i-1][j-1]
    for i in range(1, h+1):
        for j in range(1, w+1):
            if table[i][j] == '
                print('
                continue
            cnt = 0
            for k in range(i-1, i+2):
                for l in range(j-1, j+2):
                    if table[k][l] == '
                        cnt += 1
            print(cnt, end='')
        print()

if __name__ == '__main__':
    main()

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

T=[]

for y in range(H+2):
    H=""
    for x in range(W+2):
        if S[y][x]==".":
            K=0
            for b in range(-1,2):
                for a in range(-1,2):
                    K+=(S[y+b][x+a]=="
            H+=str(K)
        else:
            H+=S[y][x]
    T.append(H)

print("\n".join(map(lambda x:x[1:-1],T[1:-1])))

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

M = [[0] * (W + 2) for _ in range(H + 2)]

for i in range(H):
    for j in range(W):
        if S[i][j] == '
            for k in range(3):
                for l in range(3):
                    if k == l == 1:
                        M[i + k][j + l] = '
                    else:
                        if M[i + k][j + l] != '
                            M[i + k][j + l] += 1

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

Solution (Python):
import sys, os, math, bisect, itertools, collections, heapq, queue

from decimal import Decimal
from collections import defaultdict, deque

sys.setrecursionlimit(10000000)

ii = lambda: int(sys.stdin.buffer.readline().rstrip())
il = lambda: list(map(int, sys.stdin.buffer.readline().split()))
fl = lambda: list(map(float, sys.stdin.buffer.readline().split()))
iln = lambda n: [int(sys.stdin.buffer.readline().rstrip()) for _ in range(n)]

iss = lambda: sys.stdin.buffer.readline().decode().rstrip()
sl = lambda: list(map(str, sys.stdin.buffer.readline().decode().split()))
isn = lambda n: [sys.stdin.buffer.readline().decode().rstrip() for _ in range(n)]

lcm = lambda x, y: (x * y) // math.gcd(x, y)

MOD = 10 ** 9 + 7
MAX = float('inf')


def main():
    if os.getenv("LOCAL"):
        sys.stdin = open("input.txt", "r")

    H, W = il()
    S = [iss() for _ in range(H)]

    move = [[-1, 1], [0, 1], [1, 1], [-1, 0], [1, 0], [-1, -1], [0, -1], [1, -1]]
    ret = []
    for _ in range(H):
        ret.append([0] * W)

    for h in range(H):
        for w in range(W):
            if S[h][w] == '.':
                continue
            else:
                ret[h][w] = '
                for mw, mh in move:
                    if w + mw >= W or h + mh >= H: continue
                    if w + mw < 0 or h + mh < 0: continue
                    if ret[h + mh][w + mw] != '

    for r in ret:
        print(*r, sep='')


if __name__ == '__main__':
    main()

Solution (Python):


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

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


def eight_math_index(i, j, H, W):
    result = [(i-1, j-1), (i-1, j), (i-1, j+1),
              (i, j - 1), (i, j), (i, j + 1),
              (i + 1, j - 1), (i + 1, j), (i + 1, j + 1)]
    return list(filter(lambda x: (0 <= x[0] <= (H-1)) and (0 <= x[1] <= (W-1)), result))


for i in range(H):
    for j in range(W):
        if S[i][j] == ".":
            eight = eight_math_index(i, j, H, W)
            S[i][j] = 0
            for (k, l) in eight:
                if S[k][l] == "
                    S[i][j] += 1

for l in S:
    print("".join(list(map(str, l))))

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

S = [list(input()) for _ in range(H)]
dydx = [(-1, -1), (-1, 0), (-1, 1), (0, -1), (0, 1), (1, -1), (1, 0), (1, 1)]


def check(h, w):
  count = 0
  for dy, dx in dydx:
    if 0 <= h + dy < H and 0 <= w + dx < W and S[h+dy][w+dx] == '
      count += 1
  return count

for h in range(H):
  for w in range(W):
    if S[h][w] == '.':
      S[h][w] = str(check(h, w))
  res = "".join(S[h])
  print(res)

Solution (Python):
h,w=map(int, input().split())
mat = []
mat.append(["."]*(w+2))
for i in range(h):
  mat.append(["."]+list(input())+["."])
mat.append(["."]*(w+2))
for y in range(1, h+1):
  for x in range(1, w+1):
    if mat[y][x] != "
      c = 0
      if mat[y-1][x-1] == "
        c += 1
      if mat[y-1][x] == "
        c += 1       
      if mat[y-1][x+1] == "
        c += 1      
      if mat[y][x-1] == "
        c += 1
      if mat[y][x+1] == "
        c += 1
      if mat[y+1][x-1] == "
        c += 1        
      if mat[y+1][x] == "
        c += 1        
      if mat[y+1][x+1] == "
        c += 1
      mat[y][x] = str(c)

for y in range(1, h+1):
  print("".join(mat[y][1:w+1]))

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



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



for i in range(h):
    si = ""
    for j in range(w):
        c = 0
        if s[i][j] == ".":
            for di in d:
                if i+di[0] >= 0 and i+di[0] < h and j+di[1] >= 0 and j+di[1] < w:
                    if s[i+di[0]][j+di[1]] == "
                        c += 1
            si += str(c)
        else:
            si += s[i][j]
    s[i] = si

for si in s:
    print(si)

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))
for i in range(1,h+1):
    ans=[]
    for j in range(1,w+1):
        cnt=0
        if s[i][j]==".":
            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
            ans.append(str(cnt))
        else:
            ans.append("
    print("".join(ans))

Solution (Python):
r,c = map(int,input().split())

map = []
for i in range(r):
    map.append(input())

def kinbou(y,x):
    sum = 0
    if y-1 >= 0:
        if (map[y-1][x] == '
            sum += 1

    if x-1 >= 0:
        if (map[y][x-1] == '
            sum += 1

    if x+1 < c:
        if (map[y][x+1] == '
            sum += 1

    if y+1 < r:
        if (map[y+1][x] == '
            sum += 1

    if y-1 >= 0 and x-1 >= 0:
        if (map[y-1][x-1] == '
            sum += 1

    if y-1 >= 0 and x+1 < c:
        if (map[y-1][x+1] == '
            sum += 1

    if y+1 < r and x-1 >= 0:
        if (map[y+1][x-1] == '
            sum += 1

    if y+1 < r and x+1 < c:
        if (map[y+1][x+1] == '
            sum += 1
    return sum

map2 = [['0']*c for i in range(r)]

for i in range(r):
    for j in range(c):
        if map[i][j] == '.':
            map2[i][j] = str(kinbou(i,j))
        else:
            map2[i][j] = '
    print(''.join(map2[i]))

Solution (Python):
H, W = list(map(int,input().split()))
S = []
c = []
for h in range(H):
    S.append(input())
    c.append([0 for _ in range(W)])
for y in range(H):
    for x in range(W):
        if S[y][x]=="
            continue
        for cy in range(max(0,y-1),min(H,y+2)):
            for cx in range(max(0,x-1),min(W,x+2)):
                c[y][x] += 1 if ( (cy!=y or cx!=x) and S[cy][cx]=="
for y in range(H):
    for x in range(W):
        print("
    print("")

Solution (Python):
a = []
dx = [1, 0, -1, 0, 1, -1, -1, 1]
dy = [0, 1, 0, -1, 1, 1, -1, -1]
h, w = map(int, input().split())
ans = [["

for _ in range(h):
    a.append(input())

for k in range(h):
    for j in range(w):
        if a[k][j] == ".":
            count = 0
            for i in range(8):
                nx = j + dx[i]
                ny = k + dy[i]

                if nx < 0 or ny < 0 or nx >= w or ny >= h:
                    continue
                if a[ny][nx] == "
                    count += 1
            ans[k][j] = count

for s in ans:
    print(*s, sep="")

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

sd = {}
for i in range(0, h + 2):
    for j in range(0, w + 2):
        if (i == 0) or (i == h + 1) or (j == 0) or (j == w + 1):
            sd[(i, j)] = 0
        elif s[i - 1][j - 1] == '.':
            sd[(i, j)] = 0
        else:
            sd[(i, j)] = 1

for i in range(1, h + 1):
    line = ""
    for j in range(1, w + 1):
        if s[i - 1][j - 1] == '.':
            num = 0
            num += sd[(i - 1, j - 1)]
            num += sd[(i - 1, j)]
            num += sd[(i - 1, j + 1)]
            num += sd[(i, j - 1)]
            num += sd[(i, j)]
            num += sd[(i, j + 1)]
            num += sd[(i + 1, j - 1)]
            num += sd[(i + 1, j)]
            num += sd[(i + 1, j + 1)]
            line += str(num)
        else:
            line += s[i - 1][j - 1]
    print(line)

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

for h in range(H):
    for w in range(W):
        if s_l[h][w] == ".":
            cnt = 0
            if h != 0:
                if w != 0:
                    if s_l[h - 1][w - 1] == "
                        cnt += 1
                if w != W - 1:
                    if s_l[h - 1][w + 1] == "
                        cnt += 1
                if s_l[h - 1][w] == "
                    cnt += 1
            if h != H - 1:
                if w != 0:
                    if s_l[h + 1][w - 1] == "
                        cnt += 1
                if w != W - 1:
                    if s_l[h + 1][w + 1] == "
                        cnt += 1
                if s_l[h + 1][w] == "
                    cnt += 1
            if w != 0:
                if s_l[h][w - 1] == "
                    cnt += 1
            if w != W - 1:
                if s_l[h][w + 1] == "
                    cnt += 1
            s_l[h][w] = str(cnt)

for h in range(H):
    print("".join(s_l[h]))

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] == '
            continue
        cnt = 0
        for k in range(-1, 2):
            for l in range(-1, 2):
                if k == 0 and l == 0:
                    continue
                if 0 <= i + k < h and 0 <= j + l < w and s[i+k][j+l] == '
                    cnt += 1
        s[i][j] = cnt

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

Solution (Python):
h,w = map(int, input().split())
grid = [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 i in range(h):
    for j in range(w):
        if grid[i][j] == ".":
            cnt = 0
            for k in range(8):
                nx = j + dx[k]
                ny = i + dy[k]            
                if (nx < 0) or (w-1 < nx) or (ny < 0) or (h-1 < ny):
                    continue
                elif grid[ny][nx] == "
                    cnt += 1
                else:
                    pass
            grid[i][j] = cnt
for i in range(h):
  ans = ''.join(map(str,grid[i]))
  print(ans)

Solution (Python):

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

lst = [str(input()) for _ in range(H)]



def check(y,x):
    if x < 0 or x >= W or y < 0 or y >= H:
        return 0
    elif lst[y][x] == '
        return 1
    else:
        return 0


fin = [[] for _ in range(H)]
dxy = [(-1,-1),(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1)]
for j in range(H):
    for i in range(W):
    
        sol = 0
        if lst[j][i] == '
            sol = '
        else:
            for dy,dx in dxy:
                sol += check(j+dy,i+dx)
            
        fin[j].append(str(sol))    
    print(*fin[j],sep = '')

Solution (Python):

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

lst = [str(input()) for _ in range(H)]



def check(y,x):
    if x < 0 or x >= W or y < 0 or y >= H:
        return 0
    elif lst[y][x] == '
        return 1
    else:
        return 0


fin = [[] for _ in range(H)]
for j in range(H):
    for i in range(W):
    
        sol = 0
        if lst[j][i] == '
            sol = '
        else:
            
            sol += check(j-1,i)
            sol += check(j+1,i)
            sol += check(j,i-1)
            sol += check(j,i+1)
            sol += check(j-1,i-1)
            sol += check(j+1,i+1)
            sol += check(j+1,i-1)
            sol += check(j-1,i+1)
        fin[j].append(str(sol))    
    print(*fin[j],sep = '')

Solution (Python):
h,w=map(int,input().split())
s=[list(input()) for _ in range(h)]
a=[-1,0,1]
b=[-1,0,1]
for i in range(h):
    for j in range(w):
        if s[i][j]==".":
            s[i][j]=0
            for c in a:
                for d in b:
                    if i+c<0 or j+d<0 or i+c>h-1 or j+d>w-1 or (a==0 and b==0) or s[i+c][j+d]!="
                        continue
                    else:
                        s[i][j]+=1
for i in range(h):
    print(*s[i][:],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):
        if S[i][j] == '
            continue
        tmp = 0
        for di in range(max(i-1, 0), min(i+2, H)):
            for dj in range(max(j-1, 0), min(j+2, W)):
                tmp += int(S[di][dj] == '
        S[i][j] = str(tmp)

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

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

H,W = list(map(int,input().split()))
cell = []
for i in range(H):
    s = list(input().rstrip())
    cell.append(s)
for ih in range(H):
    for iw in range(W):
        if cell[ih][iw] == '.':
            ans =0
            for iih in range(max(0,ih-1),min(ih+2,H)):
                for iiw in range(max(0,iw-1),min(iw+2,W)):
                    if cell[iih][iiw]=='
                        ans+=1
            cell[ih][iw] = ans
for i in range(H):
    for j in range(W):
        print(cell[i][j],end='')
    print('')

Solution (Python):

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

S_tgt = copy.deepcopy(S_src)
for i in range(H):
    for j in range(W):
        if S_src[i][j] == '
            continue

        count = 0
        for m in range(-1, 2):
            if not (0 <= i + m < H):
                continue
            for n in range(-1, 2):
                if not (0 <= j + n < W):
                    continue
                if S_src[i+m][j+n] == '
                    count += 1
        S_tgt[i][j] = str(count)

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

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

for h in range(1,1+H):
  for w in range(1,1+W):
    if S[h][w]!="
      s = S[h-1][w-1:w+2]+S[h][w-1]+S[h][w+1]+S[h+1][w-1:w+2]
      S[h]=S[h][:w]+str(s.count("

for h in range(1,1+H):
  print(S[h][1:1+W])

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


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

ans = S

for i,row in enumerate(S):
    for j,mas in enumerate(row):
        if mas == '
            pass
        else:
            count = 0
            for i2 in [-1,0,1]:
                for j2 in [-1,0,1]:
                    if (i+i2<0) | (j+j2<0) | (i+i2>=H) |(j+j2>=W):
                        pass
                    else:
                        if S[i+i2][j+j2]=='
                            count +=1
            ans[i] = ans[i][:j] + str(count) +ans[i][j+1:]
for i in ans:
    print(i)

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

S.insert(0,(["."]*W))
S.append(["."]*W)

for h in range(H+2):
  S[h].insert(0,".")
  S[h].append(".")

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

for h in range(1,1+H):
  print(*S[h][1:-1],sep="")

Solution (Python):
h,w = map(int,input().split())
s = [list(input()) for i in range(h)]
if h == 1 or w == 1:
    if h == 1 and w == 1:
        if s[0][0] == '.':
            print(0)
            exit(0)
        else:
            print('
            exit(0)
    elif h == 1:
        for i in range(w):
            if s[0][i] == '.':
                if i == 0:
                    t = 0
                    if s[0][i+1] == '
                        t += 1
                    s[0][i] = str(t)
                elif i == w - 1:
                    t = 0
                    if s[0][i-1] == '
                        t += 1
                    s[0][i] = str(t)
                else:
                    t = 0
                    if s[0][i+1] == '
                        t += 1
                    if s[0][i-1] == '
                        t += 1
                    s[0][i] = str(t)
    else:
        for i in range(h):
            if s[i][0] == '.':
                if i == 0:
                    t = 0
                    if s[i+1][0] == '
                        t += 1
                    s[i][0] = str(t)
                elif i == h - 1:
                    t = 0
                    if s[i-1][0] == '
                        t += 1
                    s[i][0] = str(t)
                else:
                    t = 0
                    if s[i+1][0] == '
                        t += 1
                    if s[i-1][0] == '
                        t += 1
                    s[i][0] = str(t)
    for i in range(h):
        print(''.join(s[i]))
    exit(0)

for i in range(h):
    for j in range(w):
        if s[i][j] == '.':
            if i == 0 and j == 0:
                t = 0
                if s[i][j+1] == '
                    t += 1
                if s[i+1][j] == '
                    t += 1
                if s[i+1][j+1] == '
                    t += 1
                s[i][j] = str(t)
            elif i == 0 and j == w - 1:
                t = 0
                if s[i][j-1] == '
                    t += 1
                if s[i+1][j-1] == '
                    t += 1
                if s[i+1][j] == '
                    t += 1
                s[i][j] = str(t)
            elif i == h - 1 and j == 0:
                t = 0
                if s[i-1][j] == '
                    t += 1
                if s[i-1][j+1] == '
                    t += 1
                if s[i][j+1] == '
                    t += 1
                s[i][j] = str(t)
            elif i == h - 1 and j == w - 1:
                t = 0
                if s[i-1][j-1] == '
                    t += 1
                if s[i-1][j] == '
                    t += 1
                if s[i][j-1] == '
                    t += 1
                s[i][j] = str(t)
            elif i == 0:
                t = 0
                if s[i][j-1] == '
                    t += 1
                if s[i][j+1] == '
                    t += 1
                if s[i+1][j-1] == '
                    t += 1
                if s[i+1][j] == '
                    t += 1
                if s[i+1][j+1] == '
                    t += 1
                s[i][j] = str(t)
            elif j == 0:
                t = 0
                if s[i-1][j] == '
                    t += 1
                if s[i-1][j+1] == '
                    t += 1
                if s[i][j+1] == '
                    t += 1
                if s[i+1][j] == '
                    t += 1
                if s[i+1][j+1] == '
                    t += 1
                s[i][j] = str(t)
            elif i == h - 1:
                t = 0
                if s[i-1][j-1] == '
                    t += 1
                if s[i-1][j] == '
                    t += 1
                if s[i-1][j+1] == '
                    t += 1
                if s[i][j-1] == '
                    t += 1
                if s[i][j+1] == '
                    t += 1
                s[i][j] = str(t)
            elif j == w - 1:
                t = 0
                if s[i-1][j-1] == '
                    t += 1
                if s[i-1][j] == '
                    t += 1
                if s[i][j-1] == '
                    t += 1
                if s[i+1][j-1] == '
                    t += 1
                if s[i+1][j] == '
                    t += 1
                s[i][j] = str(t)
            else:
                t = 0
                if s[i-1][j-1] == '
                    t += 1
                if s[i-1][j] == '
                    t += 1
                if s[i-1][j+1] == '
                    t += 1
                if s[i][j-1] == '
                    t += 1
                if s[i][j+1] == '
                    t += 1
                if s[i+1][j-1] == '
                    t += 1
                if s[i+1][j] == '
                    t += 1
                if s[i+1][j+1] == '
                    t += 1
                s[i][j] = str(t)

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

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

lis = []
tmp =["."]*(w+2)
lis.append(tmp)

for i in range(h):
    a = ["."]+list(input())+["."]
    lis.append(a)

lis.append(tmp)

for i in range(1,h+1):
    for j in range(1,w+1):
        if lis[i][j] == "
            continue
        
        else:
            n = 0
            if lis[i-1][j-1] == "
                n += 1
            if lis[i-1][j] == "
                n += 1
            if lis[i][j-1] == "
                n += 1
            if lis[i][j+1] == "
                n += 1
            if lis[i+1][j-1] == "
                n += 1
            if lis[i+1][j] == "
                n += 1
            if lis[i+1][j+1] == "
                n += 1
            if lis[i-1][j+1] == "
                n += 1
            
            lis[i][j] = n

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

Solution (Python):
def CountBomb(box, line, row):
    count = 0
    for i in range(-1, 2):
        for j in range(-1, 2):
            if line+i < 0 or line+i >= len(box) or row+j < 0 or row+j >= len(box[0]):
                continue
            else:
                cell = box[line+i][row+j]
                if cell == "
                    count += 1
    return count

def resolve():
    h, w = map(int, input().split())
    box = list()
    for i in range(h):
        a = input()
        b = list()
        for i in range(len(a)):
            b.append(a[i])
        box.append(b)
    
    for line in range(h):
        for row in range(w):
            if box[line][row] == ".":
                box[line][row] = CountBomb(box, line, row)
    
    for line in range(h):
        for row in range(w):
            a = box[line][row]
            print(a, end="")
        print()
resolve()