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 = input().split()
H = int(H)
W = int(W)
S = [input() for i in range(H)]

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

    


for w in range(1,W-1):
    s = S[0][w]
    if s == '.':
        count = 0
        if S[0][w-1] == '
        if S[0][w+1] == '
        if S[1][w-1] == '
        if S[1][w] == '
        if S[1][w+1] == '
        S[0] = S[0][:w] + str(count) + S[0][w+1:]

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

for h in range(1,H-1):
    w = 0
    s = S[h][w]
    if s == '.':
        count = 0
        
        if S[h][w+1] == '
        
        if S[h-1][w] == '
        if S[h-1][w+1] == '
        
        if S[h+1][w] == '
        if S[h+1][w+1] == '
        S[h] = str(count) + S[h][w+1:]

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

w = 0
h = 0
s = S[h][w]
if s == '.':
        count = 0
        
        if S[h][w+1] == '
        
        
        
        
        if S[h+1][w] == '
        if S[h+1][w+1] == '
        S[h] = str(count) + S[h][w+1:]
        
w = 0
h = H-1
s = S[h][w]
if s == '.':
        count = 0
        
        if S[h][w+1] == '
        
        if S[h-1][w] == '
        if S[h-1][w+1] == '
        
        
        
        S[h] = str(count) + S[h][w+1:]

w = W-1
h = 0
s = S[h][w]
if s == '.':
        count = 0
        if S[h][w-1] == '
        
        
        
        
        if S[h+1][w-1] == '
        if S[h+1][w] == '
        
        S[h] = S[h][:w] + str(count)

w = W-1
h = H-1
s = S[h][w]
if s == '.':
        count = 0
        if S[h][w-1] == '
        
        if S[h-1][w-1] == '
        if S[h-1][w] == '
        
        
        
        
        S[h] = S[h][:w] + str(count)
        

        

for h in range(H):
    print(S[h])

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

result = []
for j in range(h):
    r = ""
    for i in range(w):
        if s[j][i] == "
            r += "
            continue

        si = 0 if i == 0 else i-1
        sj = 0 if j == 0 else j-1
        ei = w if i == w-1 else i+2
        ej = h if j == h-1 else j+2
        count = 0
        for j_ in range(sj,ej):
            for i_ in range(si,ei):
                if s[j_][i_] == "
                    count += 1
        r += str(count)
    result.append(r)

for j in range(h):
    print(result[j])

Solution (Python):
h,w=map(int,input().split())
S=[]
S.append(['0']*(w+2))
for i in range(h):
    X=['0']
    X.extend(list(input()))
    X.extend(['0'])
    S.append(X)
S.append(['0']*(w+2))    
    
for i in range(1,h+1):
    for j in range(1,w+1):
        cnt=0
        if S[i][j]=='.':
            for a in range(-1,2):
                for b in range(-1,2):
                    if a==0 and b==0: 
                        continue
                    if S[i+a][j+b]=='
                        cnt+=1
            S[i][j]=str(cnt)
    Y=''.join(S[i][1:w+1])
    print(Y)

Solution (Python):
args = input().split()

h, w = int(args[0]), int(args[1])

array = []

hoge = ""

for i in range(w + 2):
    hoge += "."

array.append(hoge)
for y in range(h):
    args = "." + input() + "."

    array.append(args)

array.append(hoge)

result = []
for iy in range(h):
    result = ""
    for ix in range(w):
        y = iy + 1
        x = ix + 1
        count = 0
        if array[y][x] == "
            result += "
        else:
            for fx in [-1, 0, 1]:
                for fy in [-1, 0, 1]:
                    if array[y + fy][x + fx] == "
                        count += 1
            result += str(count)
    print(result)

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

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

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

        count = 0
        for x in dx:
            for y in dy:
                if 0<=i+y<h and 0<=j+x<w and S[i+y][j+x]=='
                    count += 1
        S[i][j] = str(count)

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

Solution (Python):
h, w = map(int, input().split())
s = [list(input()) for i in range(h)]
for i in range(h):
    T = ""
    for j in range(w):
        if s[i][j] == ".":
            count = 0
            if i < h - 1:
                if s[i + 1][j] == "
                    count += 1
                if j < w - 1:
                    if s[i + 1][j + 1] == "
                        count += 1
                if j > 0:
                    if s[i + 1][j - 1] == "
                        count += 1
            if i > 0:
                if s[i - 1][j] == "
                    count += 1
                if j < w - 1:
                    if s[i - 1][j + 1] == "
                        count += 1
                if j > 0:
                    if s[i - 1][j - 1] == "
                        count += 1
            if j < w - 1:
                if s[i][j + 1] == "
                    count += 1
            if j > 0:
                if s[i][j - 1] == "
                    count += 1
            T += str(count)
        else:
            T += "
    print(T)

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

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 field[i][j] == '.':
            num = 0
            for k in range(8):
                ni = i + dx[k]
                nj = j + dy[k]
                if ni < 0 or ni >= H or nj < 0 or nj >= W:
                    continue
                if field[ni][nj] == '
                     num += 1
            field[i][j] = str(num)

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

Solution (Python):
def CountMine(x, y):
    count = 0
    for k in range(3):
        for l in range(3):
            if S[x-1+k][y-1+l] == '
                count += 1
    return count
    
    

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

for i in range(H):
    for j in range(W):
        if S[i+1][j+1] == '.':
            print(CountMine(i+1, j+1), end="")
        else:
            print('
    print('\n')

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

S_out = [[0] * W for _ in range(H)]

for i in range(H):
    for j in range(W):
        edge0 = (i == 0)
        edge1 = (i == H - 1)
        edge2 = (j == 0)
        edge3 = (j == W -1)

        if S[i][j] == "
            S_out[i][j] = "
        else:
            S_out[i][j] = str(((S[i - 1][j - 1 + edge2:j + 2 - edge3] if not edge0 else "")
                           + S[i][j - 1 + edge2:j + 2 - edge3]
                           + (S[i + 1][j - 1 + edge2:j + 2 - edge3] if not edge1 else "")).count("

print("\n".join(list(map(lambda x: "".join(x), S_out))))

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

for i in range(H):
    for j in range(W):
        if(S[i][j] == '
            continue
        count = 0
        for x in dx:
            for y in dy:
                if(not 0<=i+y<H or not 0<=j+x<W):
                    continue
                if(S[i+y][j+x] == '
                    count += 1
        S[i][j] = str(count)
     
for s in S:
    print("".join(s))

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


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


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

        count = 0
        for x in dx:
            for y in dy:
                if not 0<=i+y<h or not 0<=j+x<w:
                    continue
                
                if S[i+y][j+x] == '
                    count += 1
        S[i][j] = str(count)

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

Solution (Python):
from numpy import*
a,b=map(int,input().split())
c=["0"]*(b+2)
x=[list("0"+input().replace(".","0").replace("
x.insert(0,c)
x.append(c)
x=array(x).astype(int)
for s in range(1,1+a):
    tmp=""
    for t in range(1,1+b):
        if x[s][t]==1:
            tmp+="
        else:
            tmp+=str(sum(x[s-1:s+2,t-1:t+2]))
    print(tmp)

Solution (Python):
from numpy import*
a,b=map(int,input().split())
c=["0"]*(b+2)
x=[list("0"+input().replace(".","0").replace("
x.insert(0,c)
x.append(c)
y=zeros((a+2,b+2))
x=array(x).astype(int)
for s in range(1,1+a):
    tmp=""
    for t in range(1,1+b):
        if x[s][t]==1:
            tmp+="
        else:
            tmp+=str(sum(x[s-1:s+2,t-1:t+2]))
    print(tmp)

Solution (Python):
H,W = map(int,input().split())
S = []
def count(i,j):
    res = 0
    dx = [1,0,-1]
    dy = [1,0,-1]
    for k in range(3):
        for l in range(3):
            if 0 <= i + dy[k] <= H-1 and 0 <= j + dx[l] <= W-1:
                if S[i+dy[k]][j+dx[l]] == "
                    res += 1
    return res

for i in range(H):
    S.append(list(input()))
for i in range(H):
    for j in range(W):
        if S[i][j] == ".":
            S[i][j] = str(count(i,j))
for i in range(H):
    print("".join(S[i]))

Solution (Python):
""" AtCoder """

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

S = ['.' * (W + 2)]

for _ in range(H):
    S.append('.' + input() + '.')

S.append('.' * (W + 2))

x = [-1, 0, 1, 0, -1, 1, 1, -1]
y = [0, -1, 0, 1, -1, 1, -1, 1]

ans = [['

for i in range(1, H + 1):
    for j in range(1, W + 1):
        if S[i][j] != '
            count = 0
            for k in range(8):
                if S[i + y[k]][j+x[k]] == '
                    count += 1
            ans[i - 1][j - 1] = str(count)

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

Solution (Python):
import itertools

directions = list(itertools.product((-1, 0, 1), repeat=2))


def main():
    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 d in directions:
                if d[0] == 0 and d[1] == 0:
                    continue
                ni = i + d[0]
                nj = j + d[1]
                if 0 <= ni and 0 <= nj and ni < H and nj < W:
                    if S[ni][nj] == '
                        cnt += 1
            S[i][j] = str(cnt)
    for i in range(H):
        print("".join(S[i]))


if __name__ == "__main__":
    main()

Solution (Python):
h, w = map(int, input().split())
s = [input().split() for _ in range(h)]
a = [[] for i in range(h)]
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):

        num = 0
        for d in range(8):
            ni = i + dy[d]
            nj = j + dx[d]
            if ni < 0 or h <= ni:
                continue
            if nj < 0 or w <= nj:
                continue
            if s[ni][0][nj] == '
                num += 1
        a[i].append(str(num))

for i in range(h):
    for j in range(w):
        if s[i][0][j] == '
            a[i][j] = '

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

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

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

A = [[] for i in range(H)]
for i in range(H):
    for j in range(W):
        count = 0
        for k in range(8):
            ny = i + dy[k]
            nx = j + dx[k]
            if S[i][j]:
                if 0 <= ny < H and 0 <= nx < W:
                    if S[ny][nx] == "
                        count += 1
        A[i].append(str(count))

for i in range(H):
    for j in range(W):
        if S[i][j] == "
           A[i][j] = "
for i in range(H):
    print("".join(A[i]))

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

if h == 1 and w == 1:
    if s[0]=='
        print('
    else:
        print('0')

elif h == 1:
    ans = ''
    for i in range(w):
        if s[0][i]=='
            ans += '
        elif i != 0 and i != w-1:
            ans += str(s[0][i-1:i+2].count('
        elif i == 0:
            ans += str(s[0][1].count('
        elif i == w-1:
            ans += str(s[0][i-1].count('
    print(ans)

elif w == 1:
    for i in range(h):
        if s[i]=='
            print('
        elif i != 0 and i != h-1:
            print(str(s[i-1:i+2].count('
        elif i == 0:
            print(str(s[1].count('
        elif i == h-1:
            print(str(s[i-1].count('

else:
    for i in range(h):
        ans = ''
        for j in range(w):
            if s[i][j] == '
                ans += '
            else:
                if i != 0 and j!= 0 and i != h-1 and j!= w-1:
                    up = s[i-1][j-1:j+2].count('
                    parallel = s[i][j-1:j+2].count('
                    down = s[i+1][j-1:j+2].count('
                    ans += str(up+parallel+down)
                elif i==0 and j==0:
                    parallel = s[0][1].count('
                    down = s[1][0:2].count('
                    ans += str(parallel+down)
                elif i==0 and j==w-1:
                    parallel = s[0][w-2:].count('
                    down = s[1][w-2:].count('
                    ans += str(parallel+down)
                elif i==h-1 and j==0:
                    up = s[i-1][:2].count('
                    parallel = s[i][:2].count('
                    ans += str(up+parallel)
                elif i==h-1 and j==w-1:
                    up = s[i-1][w-2:].count('
                    parallel = s[i][w-2:].count('
                    ans += str(up+parallel)
                elif i==0:
                    parallel = s[0][j-1:j+2].count('
                    down = s[1][j-1:j+2].count('
                    ans += str(parallel+down)
                elif j==0:
                    up = s[i-1][:2].count('
                    parallel = s[i][:2].count('
                    down = s[i+1][:2].count('
                    ans += str(up+parallel+down)
                elif i==h-1:
                    up = s[i-1][j-1:j+2].count('
                    parallel = s[i][j-1:j+2].count('
                    ans += str(up+parallel)
                else:
                    up = s[i-1][w-2:].count('
                    parallel = s[i][w-2:].count('
                    down = s[i+1][w-2:].count('
                    ans += str(up+parallel+down)
        print(ans)

Solution (Python):

import sys
sys.setrecursionlimit(10**9)

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

grid = [["

for i in range(1, H+1):
    s = input()
    for j, c in enumerate(s):
        grid[i][j+1] = c

cntGrid = [[0]*(W+2) for _ in range(H+2)]
for i in range(1, H+1):
    for j in range(1, W+1):
        if grid[i][j] == "
            cntGrid[i][j] = "
            d = [-1, 0, 1]
            for x in d:
                for y in d:
                    if x == 0 and y == 0:
                        continue
                    else:
                        if not grid[i+x][j+y] == "
                            cntGrid[i+x][j+y] += 1
for l in cntGrid[1:-1]:
    print("".join(map(str, l[1:-1])))

Solution (Python):
def b_minesweeper(H, W, S):
    
    field = [[''] * W for _ in range(H)]
    
    neighbouring_square = ((0, 1), (1, 0), (0, -1), (-1, 0), (1, 1), (1, -1), (-1, -1), (-1, 1))

    def is_in_field(row, col):  
        return (0 <= row < H) and (0 <= col < W)

    for row in range(H):
        for col in range(W):
            if S[row][col] == '
                field[row][col] = '
                continue

            num = 0  
            for dr, dc in neighbouring_square:
                nr, nc = row + dr, col + dc
                if is_in_field(nr, nc) and S[nr][nc] == '
                    num += 1
            field[row][col] = str(num)
    return '\n'.join(''.join(row) for row in field)


H, W = [int(i) for i in input().split()]
S = [input() for _ in range(H)]
print(b_minesweeper(H, W, S))

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

for i in range(H):
  for j in range(W):
    if(S[i][j] == "
      reach[i][j] = "
      if(reach[i-1][j-1] != "
        reach[i-1][j-1] += 1
      if(reach[i-1][j] != "
        reach[i-1][j] += 1
      if(reach[i-1][j+1] != "
        reach[i-1][j+1] += 1
      if(reach[i][j-1] != "
        reach[i][j-1] += 1
      if(reach[i][j+1] != "
        reach[i][j+1] += 1
      if(reach[i+1][j-1] != "
        reach[i+1][j-1] += 1
      if(reach[i+1][j] != "
        reach[i+1][j] += 1
      if(reach[i+1][j+1] != "
        reach[i+1][j+1] += 1
        
for r in reach[:-1]:
  print("".join(list(map(str, r[:W]))))

Solution (Python):

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

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

for i in range(h) :
    for j in range(w) :
        p = i+1
        q = j+1
        if A[i][j] == "
            B[p][q] = 10
            for r in range(-1,2) :
                for s in range(-1,2) :
                    B[p+r][q+s] += 1

for i in range(h+2):
    for j in range(w+2):
        if B[i][j] > 8 :
            B[i][j] = "

for i in range(1,h+1) :
    ans = ""
    for j in range(1,w+1) :
        ans += str(B[i][j])
    print(ans)

Solution (Python):
h, w = map(int, input().split())
m = []
dx = [0, 1, 1, 1, 0, -1, -1, -1]
dy = [1, 1, 0, -1, -1, -1, 0, 1]
for i in range(h):
    m.append(list(input()))
for i in range(h):
    for j in range(w):
        if m[i][j] == '
            continue
        num = 0
        for k in range(8):
            x = j + dx[k]
            y = i + dy[k]
            if x < 0 or x >= w or y < 0 or y >= h:
                continue
            if m[y][x] == '
                num += 1
        m[i][j] = str(num)
for i in range(h):
    print(''.join(m[i]))

Solution (Python):
h, w = map(int, input().split())
f = [input() for _ in range(h)]
for i in range(h):
  for j in range(w):
    if f[i][j] == '
      print('
      continue
    cnt = 0
    for k in range(i-1, i+2):
      if k < 0 or h <= k:
        continue
      for l in range(j-1, j+2):
        if l < 0 or w <= l:
          continue
        if f[k][l] == '
          cnt += 1
    print(cnt, end = "")
  print()

Solution (Python):
H,W=[int(x) for x in input().split()]

bom_list=[]
serch_list=[-1,0,1]

for h in range(H):
  str_list=[x for x in input()]
  for w in range(W):
    if str_list[w]=='
      bom_list.append([h,w])
      
for h in range(H):
  out_list=[]
  for w in range(W):
    bom_num=0
    
    if [h,w] in bom_list:
      out_list.append('
    else:
      for x in [-1,0,1]:
        for y in [-1,0,1]:
          if [h+x,w+y] in bom_list:
            bom_num += 1
      out_list.append(bom_num)
  
  out_str=''
  for s in out_list:
    out_str+=str(s)
    
  print(out_str)

Solution (Python):
h,w=map(int,input().split())
masu=[]
up=1
down=1
left=1
right=1
for i in range(h):
    masu.append([i for i in input()])
for i in range(h):
    for j in range(w):
        bakudan=0
        if masu[i][j]==".":
            if i==0:up=0
            if i==h-1:down=0
            if j==0:left=0
            if j==w-1:right=0
            if up:
                if masu[i-1][j]=="
            if up and left:
                if masu[i-1][j-1]=="
            if up and right:
                if masu[i-1][j+1]=="
            if left:
                if masu[i][j-1]=="
            if left and down:
                if masu[i+1][j-1]=="
            if down:
                if masu[i+1][j]=="
            if down and right:
                if masu[i+1][j+1]=="
            if right:
                if masu[i][j+1]=="
            masu[i][j]=str(bakudan)
        up=1
        down=1
        left=1
        right=1

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

Solution (Python):
h,w = (int(i) for i in input().split())
d = {}
nei = [-101,-100,-99,-1,1,99,100,101]
for i in range(h):
  s = input()
  for j in range(w):
    d[(i+1)*100+j+1] = s[j]
for k in d:
  if d[k] == '.':
    cnt = 0
    for l in nei:
      if k+l in d:
        if d[k+l] == '
          cnt += 1
    d[k] = cnt
for i in range(h):
  ans = ''
  for j in range(w):
    ans += str(d[(i+1)*100+j+1])
  print(ans)

Solution (Python):
n, m = map(int, input().split())
lst = [input() for _ in range(n)]

def check(lst, i, j, n, m):
    if n == 1:
        row_lst = [0]
    elif i == 0:
        row_lst = [i, i+1]
    elif i == n-1:
        row_lst = [i-1, i]
    else:
        row_lst = [i-1, i, i+1]
    if m == 1:
        col_lst = [0]
    elif j == 0:
        col_lst = [j, j+1]
    elif j == m-1:
        col_lst = [j-1, j]
    else:
        col_lst = [j-1, j, j+1]
    count = 0
    for a in row_lst:
        for b in col_lst:
            if lst[a][b] == '
                count += 1
    return count


for i in range(n):
    ans = ''
    for j in range(m):
        if lst[i][j] == '
            ans += '
        else:
            ans += str(check(lst, i, j, n, m))
    print(ans)

Solution (Python):
import numpy as np
H, W = map(int, input().split())
s = [list(input()) for i in range(H)]
sc = np.array([[0]*(W+2)] + [[0] + i + [0] for i in s] + [[0]*(W+2)])
for i in range(1, H+1):
    for p in range(1, W+1):
        if sc[i][p] == '.':
            sums = 0
            for e in sc[i-1:i+2,p-1:p+2]:
                sums += list(e).count('
            sc[i][p] = str(sums)
for i in sc[1:H+1,1:W+1]:
    print(''.join(i))

Solution (Python):
H, W = map(int, input().split())
s = [list(input()) for i in range(H)]
for i in range(H):
    for p in range(W):
        if s[i][p] == '.':
            s[i][p] = 0
for i in range(H):
    for p in range(W):
        if s[i][p] != '
            if (i != 0) and (p != 0) and (s[i-1][p-1] == '
                s[i][p] += 1
            if (i != 0) and (s[i-1][p] == '
                s[i][p] += 1
            if (p != 0) and (s[i][p-1] == '
                s[i][p] += 1
            if (i != (H - 1)) and (p != (W - 1)) and (s[i+1][p+1] == '
                s[i][p] += 1
            if (i != (H-1)) and (s[i+1][p] == '
                s[i][p] += 1
            if (p != (W - 1)) and (s[i][p+1] == '
                s[i][p] += 1
            if (i != 0) and (p != (W-1)) and (s[i-1][p+1] == '
                s[i][p] += 1
            if (i != (H-1)) and (p != 0) and (s[i+1][p-1] == '
                s[i][p] += 1            
for i in s:
    print(''.join(list(map(str, i))))

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

S = []
S.append(list('@'*(W+2)))
for i in range(H):
  a = list('@'+input()+'@')
  S.append(a)
S.append(list('@'*(W+2)))

ans = []
for i in range(H+2):
  ans.append([0]*(W+2))

for i in range(H+2):
 if((i != 0)and(i != H+1)): 
  for j in range(W+2):
    if((j != 0)and(j != W+1)):      
      if(S[i+1][j-1] == '
        ans[i][j] += 1   
      if(S[i+1][j] == '
        ans[i][j] += 1
      if(S[i+1][j+1] == '
        ans[i][j] += 1
      if(S[i][j-1] == '
        ans[i][j] += 1

      if(S[i][j+1] == '
        ans[i][j] += 1
      if(S[i-1][j-1] == '
        ans[i][j] += 1   
      if(S[i-1][j] == '
        ans[i][j] += 1      
      if(S[i-1][j+1] == '
        ans[i][j] += 1
      if(S[i][j] == '
        ans[i][j] = '
      ans[i][j] = str(ans[i][j])
      
      
for i in range(H+2):
 if((i != 0)and(i != H+1)):
  ans[i].pop(0)
  ans[i].pop(-1)
  print(''.join(ans[i]))

Solution (Python):

import sys

def main():
	y, x = map(int, input().split(" "))
	
	lines = []
	for ii in range(y):
		line = input()
		lines.append(list(line))
	
	for i, val in enumerate(lines):
		for j, vol in enumerate(val):
			if vol == ".":
				counts = 0
				if i != 0:
					if lines[i - 1][j] == "
						counts = counts + 1
				if (y - 1) > i:
					if lines[i + 1][j] == "
						counts = counts + 1
				if j != 0:
					if lines[i][j - 1] == "
						counts = counts + 1
				if (x - 1) > j:
					if lines[i][j + 1] == "
						counts = counts + 1
				if i != 0 and j != 0:
					if lines[i - 1][j - 1] == "
						counts = counts + 1
				if (y - 1) > i and (x - 1) > j:
					if lines[i + 1][j + 1] == "
						counts = counts + 1
				if i != 0 and (x - 1) > j:
					if lines[i - 1][j + 1] == "
						counts = counts + 1
				if j != 0 and (y - 1) > i:
					if lines[i + 1][j - 1] == "
						counts = counts + 1
				print("{}".format(counts), end = "")
			else:
				print("
		print("")
	
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):
        if S[i][j] == '.':
            count = 0
            if i!=0:
                if j!=0 and S[i-1][j-1]=='
                    count+=1
                if S[i-1][j]=='
                    count+=1
                if j!=W-1 and S[i-1][j+1]=='
                    count+=1
            if j!=0 and S[i][j-1]=='
                count+=1
            if j!=W-1 and S[i][j+1]=='
                count+=1
            if i!=H-1:
                if j!=0 and S[i+1][j-1]=='
                    count+=1
                if S[i+1][j]=='
                    count+=1
                if j!=W-1 and S[i+1][j+1]=='
                    count+=1
            print(count, end='')
        else:
            print(S[i][j], end='')
    print()

Solution (Python):

H, W = map(int, input().split())
grid = [['*'] * (W + 2)]
for _ in range(H):
    grid.append(['*'] + list(input()) + ['*'])
grid.append(['*'] * (W + 2))

def check(i, j):
    re = 0
    for p in range(3):
        for q in range(3):
            if grid[i - 1 + p][j - 1 + q] == '
                re += 1
    return re

for i in range(1, H + 1):
    for j in range(1, W + 1):
        if grid[i][j] == '.':
            grid[i][j] = check(i, j)

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

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

ans = []
for i in range(H):
    s = ''
    for j in range(W):
        if S[i][j] == '.':
            count = 0
            for x in [-1,0,1]:
                for y in [-1,0,1]:
                    if 0 <= i+x < H and 0 <= j+y < W:
                        count += 1 if S[i+x][j+y] == '
            s += str(count)
        else:
            s += '
    ans.append(s)

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

Solution (Python):
h,w=[int(i) for i in input().split()]
s=[]
for i in range(h):
    s.append([j for j in input()])
for i in range(h):
    text=""
    for j in range(w):
        if s[i][j]!="
            cnt=0
            try:
                if s[i-1 if i-1>=0 else 100][j-1 if j-1>=0 else 100]=="
                    cnt+=1
            except:
                pass
            try:
                if s[i-1 if i-1>=0 else 100][j]=="
                    cnt+=1
            except:
                pass
            try:
                if s[i-1 if i-1>=0 else 100][j+1]=="
                    cnt+=1
            except:
                pass
            try:
                if s[i][j-1 if j-1>=0 else 100]=="
                    cnt+=1
            except:
                pass
            try:
                if s[i][j+1]=="
                    cnt+=1
            except:
                pass
            try:
                if s[i+1][j-1 if j-1>=0 else 100]=="
                    cnt+=1
            except:
                pass
            try:
                if s[i+1][j]=="
                    cnt+=1
            except:
                pass
            try:
                if s[i+1][j+1]=="
                    cnt+=1
            except:
                pass
            text+=str(cnt)
        else:
            text+="
    print(text)

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] == ".": 
      S[i][j] = 0

for i in range(H): 
  for j in range(W):
    if S[i][j] == "
       for k in range(-1, 2): 
          for l in range(-1, 2): 
            ni = i+k 
            nj = j+l 
            if (0 <= ni < H) and (0 <= nj < W) and \
               (S[ni][nj] != '
                
                
              S[ni][nj] += 1
for i in range(H): 
    print(''.join(map(str, S[i])))