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



Sample input:
3 5
.....
.#.#.
.....


Sample output:
11211
1#2#1
11211

Submission status: Runtime Error

Solution code:
def calc(array, i, j):
    ret = 0
    if i == 0 and j == 0:
        if array[1][0] == "#":
            ret += 1
        if array[0][1] == "#":
            ret += 1
        if array[1][1] == "#":
            ret += 1
        return ret
    if i == 0 and j == len(array[0])-1:
        if array[i][j-1] == "#":
            ret += 1
        if array[i+1][j-1] == "#":
            ret += 1
        if array[i+1][j] == "#":
            ret += 1
        return ret
    if i == len(array)-1 and j == 0:
        if array[i-1][j] == "#":
            ret += 1
        if array[i-1][j+1] == "#":
            ret += 1
        if array[i][j+1] == "#":
            ret += 1
        return ret
    if i == len(array)-1 and j == len(array)-1:
        if array[i][j-1] == "#":
            ret += 1
        if array[i-1][j] == "#":
            ret += 1
        if array[i-1][j-1] == "#":
            ret += 1
        return ret
    if i == 0:
        if array[i][j-1] == "#":
            ret += 1
        if array[i][j+1] == "#":
            ret += 1
        if array[i+1][j-1] == "#":
            ret += 1
        if array[i+1][j] == "#":
            ret += 1
        if array[i+1][j+1] == "#":
            ret += 1
        return ret
    if i == len(array)-1:
        if array[i][j-1] == "#":
            ret += 1
        if array[i][j+1] == "#":
            ret += 1
        if array[i-1][j-1] == "#":
            ret += 1
        if array[i-1][j] == "#":
            ret += 1
        if array[i-1][j+1] == "#":
            ret += 1
        return ret
    if j == 0:
        if array[i-1][j] == "#":
            ret += 1
        if array[i+1][j] == "#":
            ret += 1
        if array[i-1][j+1] == "#":
            ret += 1
        if array[i][j+1] == "#":
            ret += 1
        if array[i+1][j+1] == "#":
            ret += 1
        return ret
    if j == len(array[0])-1:
        if array[i-1][j] == "#":
            ret += 1
        if array[i+1][j] == "#":
            ret += 1
        if array[i-1][j-1] == "#":
            ret += 1
        if array[i][j-1] == "#":
            ret += 1
        if array[i+1][j-1] == "#":
            ret += 1
        return ret
    else:
        if array[i][j-1] == "#":
            ret += 1
        if array[i][j+1] == "#":
            ret += 1
        if array[i-1][j-1] == "#":
            ret += 1
        if array[i-1][j] == "#":
            ret += 1
        if array[i-1][j+1] == "#":
            ret += 1
        if array[i+1][j-1] == "#":
            ret += 1
        if array[i+1][j] == "#":
            ret += 1
        if array[i+1][j+1] == "#":
            ret += 1
        return ret


num = input().split()
num = [int(x) for x in num]

array = []
for h in range(num[0]):
    array.append(list(input()))

for i in range(num[0]):
    for j in range(num[1]):
        if array[i][j] == ".":
            array[i][j] = calc(array, i, j)
for i in range(0, len(array)):
    array[i] = [str(x) for x in array[i]]
    print("".join(array[i]))

Submission status: Runtime Error

Solution code:
H, W = map(int, input().split())
S = []
T = []
for i in range(H):
    S.append(input())
    T.append([0 for j in range(W)])
if H == 1 and W == 1:
    if S[0][0] == "#":
        T[0][0] = "#"
    else:
        T[0][0] = 0
elif H == 1:
    for j in range(W):
        if S[0][j] == "#":
            T[0][j] = "#"
        elif S[0][j] == ".":
            if j == 0:
                if S[0][j + 1] == "#":
                    T[0][j] += 1
            if j == W - 1:
                if S[0][j - 1] == "#":
                    T[0][j] += 1
elif W == 1:
    for i in range(H):
        if S[i][0] == "#":
            T[i][0] = "#"
        elif S[i][0] == ".":
            if i == 0:
                if S[i + 1][0] == "#":
                    T[i][0] += 1
            if i == H - 1:
                if S[i - 1][0] == "#":
                    T[i][0] += 1
for i in range(H):
    for j in range(W):
        if S[i][j] == "#":
            T[i][j] = "#"
        elif S[i][j] == ".":
            if i == 0 and j == 0:
                if S[i + 1][j] == "#":
                    T[i][j] += 1
                if S[i][j + 1] == "#":
                    T[i][j] += 1
                if S[i + 1][j + 1] == "#":
                    T[i][j] += 1
            elif i == 0 and j == W - 1:
                if S[i + 1][j] == "#":
                    T[i][j] += 1
                if S[i][j - 1] == "#":
                    T[i][j] += 1
                if S[i + 1][j - 1] == "#":
                    T[i][j] += 1
            elif i == H - 1 and j == 0:
                if S[i][j + 1] == "#":
                    T[i][j] += 1
                if S[i - 1][j] == "#":
                    T[i][j] += 1
                if S[i - 1][j + 1] == "#":
                    T[i][j] += 1
            elif i == H - 1 and j == W - 1:
                if S[i - 1][j] == "#":
                    T[i][j] += 1
                if S[i][j - 1] == "#":
                    T[i][j] += 1
                if S[i - 1][j - 1] == "#":
                    T[i][j] += 1
            elif i == 0:
                if S[i][j - 1] == "#":
                    T[i][j] += 1
                if S[i][j + 1] == "#":
                    T[i][j] += 1
                if S[i + 1][j - 1] == "#":
                    T[i][j] += 1
                if S[i + 1][j] == "#":
                    T[i][j] += 1
                if S[i + 1][j + 1] == "#":
                    T[i][j] += 1
            elif j == 0:
                if S[i - 1][j] == "#":
                    T[i][j] += 1
                if S[i + 1][j] == "#":
                    T[i][j] += 1
                if S[i - 1][j + 1] == "#":
                    T[i][j] += 1
                if S[i][j + 1] == "#":
                    T[i][j] += 1
                if S[i + 1][j + 1] == "#":
                    T[i][j] += 1
            elif i == H - 1:
                if S[i - 1][j - 1] =="#":
                    T[i][j] += 1
                if S[i - 1][j] == "#":
                    T[i][j] += 1
                if S[i - 1][j + 1] == "#":
                    T[i][j] += 1
                if S[i][j - 1] == "#":
                    T[i][j] += 1
                if S[i][j + 1] == "#":
                    T[i][j] += 1
            elif j == W - 1:
                if S[i - 1][j - 1] == "#":
                    T[i][j] += 1
                if S[i][j - 1] == "#":
                    T[i][j] += 1
                if S[i + 1][j - 1] == "#":
                    T[i][j] += 1
                if S[i - 1][j] == "#":
                    T[i][j] += 1
                if S[i + 1][j] == "#":
                    T[i][j] += 1
            else:
                if S[i - 1][j - 1] == "#":
                    T[i][j] += 1
                if S[i - 1][j] == "#":
                    T[i][j] += 1
                if S[i - 1][j + 1] == "#":
                    T[i][j] += 1
                if S[i][j - 1] == "#":
                    T[i][j] += 1
                if S[i][j + 1] == "#":
                    T[i][j] += 1
                if S[i + 1][j - 1] == "#":
                    T[i][j] += 1
                if S[i + 1][j] == "#":
                    T[i][j] += 1
                if S[i + 1][j + 1] == "#":
                    T[i][j] += 1
for i in range(H):
    for j in range(W - 1):
        print(T[i][j], end="")
    print(T[i][W - 1])

Submission status: Runtime Error

Solution code:
h, w = input().split()
for i in range(int(h)):
  row = [input() for i in range(int(w))]
print(row)

Submission status: Runtime Error

Solution code:
def main():
    b = []
    line = input().split(" ")
    H = int(line[0])
    W = int(line[1])
    for l in range(H):
        b.append(input())

    result = []
    for i, line in enumerate(b):
        result_row = ""
        for j, c in enumerate(line):
            if c == ".":
                n = count(i, j, b, H, W)
                result_row += str(n)
            else:
                result_row += b[i][j]
        result.append(result_row)

    for l in result:
        print(l)


def count(row, column, board, H, W):
    n = 0
    rs, re = judge_row(row, H)
    cs, ce = judge_col(column, W)
    for r in range(rs, re):
        for c in range(cs, ce):
            if board[r][c] == "#":
                n += 1
    return n


def judge_row(row, H):
    if row == 0:
        return row, row+1+1 
    elif row == H-1:
        return row-1, row+1
    else:
        return row-1, row+1+1


def judge_col(col, W):
    if col == 0:
        return col, col+1+1 
    elif col == W-1:
        return col-1, col+1
    else:
        return col-1, col+1+1


if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:

x = list(map(int, input().split()))
k = [list(map(str, input())) for i in range(x[0])]

def check(i,j,k):
    if k[i][j]=='#':
        return '#'
    count=0

    if i==0 and j==0:
        for ii in range(0,2):
            for jj in range(0,2):
                if ii==0 and jj==0:
                    continue
                if k[i+ii][j+jj]=='#':
                    count+=1
        return count
    if i==0 and j==len(k[0])-1:
        for ii in range(0,2):
            for jj in range(-1,1):
                if ii==0 and jj==0:
                    continue
                if k[i+ii][j+jj]=='#':
                    count+=1
        return count    

    if i==len(k)-1 and j==0:
        for ii in range(-1,1):
            for jj in range(0,2):
                if ii==0 and jj==0:
                    continue
                if k[i+ii][j+jj]=='#':
                    count+=1
        return count 
    if i==len(k)-1 and j==len(k[0])-1:
        for ii in range(-1,1):
            for jj in range(-1,1):
                if ii==0 and jj==0:
                    continue
                if k[i+ii][j+jj]=='#':
                    count+=1
        return count
    if i==0:
        for ii in range(0,2):
            for jj in range(-1,2):
                if ii==0 and jj==0:
                    continue
                if k[i+ii][j+jj]=='#':
                    count+=1
        return count

    if i==len(k)-1:
        for ii in range(-1,1):
            for jj in range(-1,2):
                if ii==0 and jj==0:
                    continue
                if k[i+ii][j+jj]=='#':
                    count+=1
        return count

    if j==0:
        for ii in range(-1,2):
            for jj in range(0,2):
                if ii==0 and jj==0:
                    continue
                if k[i+ii][j+jj]=='#':
                    count+=1 
        return count

    if j==len(k[0])-1:
        for ii in range(-1,2):
            for jj in range(-1,1):
                if ii==0 and jj==0:
                    continue
                if k[i+ii][j+jj]=='#':
                    count+=1
        return count
       
    for ii in range(-1,2):
        for jj in range(-1,2):
            if ii==0 and jj==0:
                continue
            if k[i+ii][j+jj]=='#':
                count+=1
    return count

res=[]
for ik in range(len(k)):
    for jk in range(len(k[0])):
        res.append(check(ik,jk,k))  

for i in range(x[0]):
    sta=0+x[1]*i
    fin=x[1]+x[1]*i
    while sta<fin:
        print(res[sta],end='')
        sta +=1
    print('')
quit()

Submission status: Runtime Error

Solution code:
H, W = map(int, input().split())
S = []
T = []
for i in range(H):
    S.append(input())
    T.append([0 for j in range(W)])
for i in range(H):
    for j in range(W):
        if S[i][j] == "#":
            T[i][j] = "#"
        elif S[i][j] == ".":
            if i == 0 and j == 0:
                if S[i + 1][j] == "#":
                    T[i][j] += 1
                if S[i][j + 1] == "#":
                    T[i][j] += 1
                if S[i + 1][j + 1] == "#":
                    T[i][j] += 1
            elif i == 0 and j == W - 1:
                if S[i + 1][j] == "#":
                    T[i][j] += 1
                if S[i][j - 1] == "#":
                    T[i][j] += 1
                if S[i + 1][j - 1] == "#":
                    T[i][j] += 1
            elif i == H - 1 and j == 0:
                if S[i][j + 1] == "#":
                    T[i][j] += 1
                if S[i - 1][j] == "#":
                    T[i][j] += 1
                if S[i - 1][j + 1] == "#":
                    T[i][j] += 1
            elif i == H - 1 and j == W - 1:
                if S[i - 1][j] == "#":
                    T[i][j] += 1
                if S[i][j - 1] == "#":
                    T[i][j] += 1
                if S[i - 1][j - 1] == "#":
                    T[i][j] += 1
            elif i == 0:
                if S[i][j - 1] == "#":
                    T[i][j] += 1
                if S[i][j + 1] == "#":
                    T[i][j] += 1
                if S[i + 1][j - 1] == "#":
                    T[i][j] += 1
                if S[i + 1][j] == "#":
                    T[i][j] += 1
                if S[i + 1][j + 1] == "#":
                    T[i][j] += 1
            elif j == 0:
                if S[i - 1][j] == "#":
                    T[i][j] += 1
                if S[i + 1][j] == "#":
                    T[i][j] += 1
                if S[i - 1][j + 1] == "#":
                    T[i][j] += 1
                if S[i][j + 1] == "#":
                    T[i][j] += 1
                if S[i + 1][j + 1] == "#":
                    T[i][j] += 1
            elif i == H - 1:
                if S[i - 1][j - 1] =="#":
                    T[i][j] += 1
                if S[i - 1][j] == "#":
                    T[i][j] += 1
                if S[i - 1][j + 1] == "#":
                    T[i][j] += 1
                if S[i][j - 1] == "#":
                    T[i][j] += 1
                if S[i][j + 1] == "#":
                    T[i][j] += 1
            elif j == W - 1:
                if S[i - 1][j - 1] == "#":
                    T[i][j] += 1
                if S[i][j - 1] == "#":
                    T[i][j] += 1
                if S[i + 1][j - 1] == "#":
                    T[i][j] += 1
                if S[i - 1][j] == "#":
                    T[i][j] += 1
                if S[i + 1][j] == "#":
                    T[i][j] += 1
            else:
                if S[i - 1][j - 1] == "#":
                    T[i][j] += 1
                if S[i - 1][j] == "#":
                    T[i][j] += 1
                if S[i - 1][j + 1] == "#":
                    T[i][j] += 1
                if S[i][j - 1] == "#":
                    T[i][j] += 1
                if S[i][j + 1] == "#":
                    T[i][j] += 1
                if S[i + 1][j - 1] == "#":
                    T[i][j] += 1
                if S[i + 1][j] == "#":
                    T[i][j] += 1
                if S[i + 1][j + 1] == "#":
                    T[i][j] += 1
for i in range(H):
    for j in range(W - 1):
        print(T[i][j], end="")
    print(T[i][W - 1])

Submission status: Runtime Error

Solution code:
H, W = map(int, input().split())
S = {}
for _ in range(H):
    S[_] = input()
    
def change(S_str, x, j):
    s_list = list(S_str)
    s_list[j] = str(x.count('#'))
    S_str = "".join(s_list)
    return S_str

for i in range(H):
    for j in range(W):
        s = S[i][j]
        if s == '.':
            if H == 1 and W == 1:
                S[i] = '0'
            elif H == 1 and i == 0:
                x = S[i][j+1]
                S[i] = change(S[i], x, j)
            elif H == 1 and i == W-1:
                x = S[i][j-1]
                S[i] = change(S[i], x, j)
            elif H == 1:
                x = S[i][j-1] + S[i][j+1]
                S[i] = change(S[i], x, j)
            elif W == 1 and j == 0:
                x = S[i+1][j]
                S[i] = change(S[i], x, j)
            elif W == 1 and j == W-1:
                x = S[i-1][j]
                S[i] = change(S[i], x, j)
            elif W == 1:
                x = S[i-1][j] + S[i+1][j]
                S[i] = change(S[i], x, j)
            elif i == 0 and j == 0:
                x = S[i][j+1] + S[i+1][j] + S[i+1][j+1]
                S[i] = change(S[i], x, j)
            elif i == 0 and j == W-1:
                x = S[i][j-1] + S[i+1][j-1] + S[i+1][j]
                S[i] = change(S[i], x, j)
            elif i == 0:
                x = S[i][j-1] + S[i][j+1] + S[i+1][j-1] + S[i+1][j] + S[i+1][j+1]
                S[i] = change(S[i], x, j)
            elif i == H-1 and j == 0:
                x = S[i][j+1] + S[i-1][j] + S[i-1][j+1]
                S[i] = change(S[i], x, j)
            elif i == H-1 and j == W-1:
                x = S[i][j-1] + S[i-1][j-1] + S[i-1][j]
                S[i] = change(S[i], x, j)
            elif i == H-1:
                x = S[i][j-1] + S[i][j+1] + S[i-1][j-1] + S[i-1][j] + S[i-1][j+1]
                S[i] = change(S[i], x, j)
            elif j == 0:
                x = S[i-1][j] + S[i-1][j+1] + S[i][j+1] + S[i+1][j] + S[i+1][j+1]
                S[i] = change(S[i], x, j)
            elif j == W-1:
                x = S[i-1][j-1] + S[i-1][j] + S[i][j-1] + S[i+1][j-1] + S[i+1][j]
                S[i] = change(S[i], x, j)
            else:
                x = S[i-1][j-1] + S[i-1][j] + S[i-1][j+1] + S[i][j-1] + S[i][j+1] + S[i+1][j-1] + S[i+1][j] + S[i+1][j+1]
                S[i] = change(S[i], x, j)
                
for k in range(H):
    print(S[k])

Submission status: Runtime Error

Solution code:
from sys import stdin, stdout
from collections import defaultdict
def readLine_str_list():return list(map(str, stdin.readline().split()))
def readLine_int_list():return list(map(int, stdin.readline().split()))
def readAll_str(): return list(list(map(str,i.split())) for i in stdin.read().split("\n"))
def g_twoD_list(p,q): return [[0 for i in range(p)] for j in range(q)]


def main():
    h,w = readLine_int_list()
    a = g_twoD_list(h,w)
    b = defaultdict(lambda: 0)
    
    _h = 0
    _w = 0
    for l in readAll_str():
        for i in l:
            _w = 0
            for j in i:
                b[(_h,_w)] = j
                _w += 1
            _h += 1
            
    dir_b = [(-1,0), (1,0), (0,-1), (0,1), (-1,-1), (-1,1), (1,-1), (1,1)]
    for i in range(h):
        for j in range(w):
            cnt = 0
            for d in dir_b:
                r = b[(i+d[0], j+d[1])]
                if r == '#':
                    cnt += 1
            if b[(i,j)] == '.':
                b[(i,j)] = str(cnt)
    # print(b)
    for i in b:
        if 0 <= i[0] < h and 0 <= i[1] < w:
            a[i[0]][i[1]] = b[i]
            
    for i in a:
        print(''.join(i))
if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
H, W = map(int, input().split())
S = {}
for _ in range(H):
    S[_] = input()
    
def change(S_str, x, j):
    s_list = list(S_str)
    s_list[j] = str(x.count('#'))
    S_str = "".join(s_list)
    return S_str

for i in range(H):
    for j in range(W):
        s = S[i][j]
        if s == '.':
            if i == 0 and j == 0:
                x = S[i][j+1] + S[i+1][j] + S[i+1][j+1]
                S[i] = change(S[i], x, j)
            elif i == 0 and j == W-1:
                x = S[i][j-1] + S[i+1][j-1] + S[i+1][j]
                S[i] = change(S[i], x, j)
            elif i == 0:
                x = S[i][j-1] + S[i][j+1] + S[i+1][j-1] + S[i+1][j] + S[i+1][j+1]
                S[i] = change(S[i], x, j)
            elif i == H-1 and j == 0:
                x = S[i][j+1] + S[i-1][j] + S[i-1][j+1]
                S[i] = change(S[i], x, j)
            elif i == H-1 and j == W-1:
                x = S[i][j-1] + S[i-1][j-1] + S[i-1][j]
                S[i] = change(S[i], x, j)
            elif i == H-1:
                x = S[i][j-1] + S[i][j+1] + S[i-1][j-1] + S[i-1][j] + S[i-1][j+1]
                S[i] = change(S[i], x, j)
            elif j == 0:
                x = S[i-1][j] + S[i-1][j+1] + S[i][j+1] + S[i+1][j] + S[i+1][j+1]
                S[i] = change(S[i], x, j)
            elif j == W-1:
                x = S[i-1][j-1] + S[i-1][j] + S[i][j-1] + S[i+1][j-1] + S[i+1][j]
                S[i] = change(S[i], x, j)
            else:
                x = S[i-1][j-1] + S[i-1][j] + S[i-1][j+1] + S[i][j-1] + S[i][j+1] + S[i+1][j-1] + S[i+1][j] + S[i+1][j+1]
                S[i] = change(S[i], x, j)
                
for k in range(H):
    print(S[k])

Submission status: Runtime Error

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

S = [[]]

for h in range(H):
    S.append([])
    S[-1] = list(input())#.split()

del S[0]

for i in range(H):
    for j in range(W):
        if S[i][j] == '.': S[i][j] = 0
        if S[i][j] == '#': S[i][j] = -100

for i in range(H):
    for j in range(W):
        if S[i][j] < 0: 
            try:
                S[i-1][j] += 1
            except:
                ;
            try:
                S[i][j-1] += 1
            except:
                ;
            try:
                S[i+1][j] += 1
            except:
                ;
            try:
                S[i][j+1] += 1
            except:
                ;

for i in range(H):
    for j in range(W):
        if S[i][j] < 0: S[i][j] = '#'

for i in range(len(S)):
    for j in range(len(S[i])):
        print(S[i][j], end="")
    print()

Submission status: Runtime Error

Solution code:
def get_bomb_num(i, j):
    cnt = 0
    for p in range(-1,2):
        for s in range(-1,2):
            l = i + p
            m = j + s
----
            if (p == 0 and s == 0) or (l < 0) or (l >= H) or (m < 0) or (m >= W):
                continue--
            if b[l][m] == '#':
                cnt += 1
    return cnt

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

for i in range(H):
    for j in range(W):
        if b[i][j] == '#':
            print('#', end ='')
        else:
            print(get_bomb_num(i,j), end='')
    print('')

Submission status: Runtime Error

Solution code:
#! -*- coding:utf-8 -*-
h,w = map(int,input().split())
S = []
for i in range(h):
    S.append(str(input()))
arr = [[]*w]*h

for i in range(h):
    arr[i] = list(S[i])

for i in range(h):
    for j in range(w):
        if arr[i][j] == '#':
            print('#',sep = '', end = '')
        else:
            if i == 0 and j == 0:
                c = 0
                if arr[0][1] == '#':
                    c += 1
                if arr[1][0] == '#':
                    c += 1
                if arr[1][1] == '#':
                    c += 1
                print(c,sep = '', end = '')
            elif i == 0 and j == w-1:
                c = 0
                if arr[0][-2] == '#':
                    c += 1
                if arr[1][-1] == '#':
                    c += 1
                if arr[1][-2] == '#':
                    c += 1
                print(c,sep = '', end = '')
            elif i == 0:
                c = 0
                if arr[i][j-1] == '#':
                    c+= 1
                if arr[i][j+1] == '#':
                    c +=1
                if arr[i+1][j-1] == '#':
                    c += 1
                if arr[i+1][j] == '#':
                    c += 1
                if arr[i+1][j+1] == '#':
                    c += 1
                print(c,sep = '', end = '')
            elif i == h-1 and j == 0:
                c = 0
                if arr[-1][1] == '#':
                    c += 1
                if arr[-2][0] == '#':
                    c += 1
                if arr[-2][1] == '#':
                    c += 1
                print(c,sep = '', end = '')
            elif i == h-1 and j == w-1:
                c = 0
                if arr[-1][-2] == '#':
                    c += 1
                if arr[-2][-1] == '#':
                    c += 1
                if arr[-2][-2] == '#':
                    c += 1
                print(c,sep = '', end = '')
            elif i == h-1:
                c = 0
                if arr[i][j-1] == '#':
                    c+= 1
                if arr[i][j+1] == '#':
                    c +=1
                if arr[i-1][j-1] == '#':
                    c += 1
                if arr[i-1][j] == '#':
                    c += 1
                if arr[i-1][j+1] == '#':
                    c += 1
                print(c,sep = '', end = '')
            elif j == 0:
                c = 0
                if arr[i-1][j] == '#':
                    c += 1
                if arr[i+1][j] == '#':
                    c += 1
                if arr[i][j+1] == '#':
                    c += 1
                if arr[i-1][j+1] == '#':
                    c += 1
                if arr[i+1][j+1] == '#':
                    c += 1
                print(c,sep = '', end = '')
            elif j == w-1:
                c = 0
                if arr[i-1][j] == '#':
                    c += 1
                if arr[i+1][j] == '#':
                    c += 1
                if arr[i][j-1] == '#':
                    c += 1
                if arr[i-1][j-1] == '#':
                    c += 1
                if arr[i+1][j-1] == '#':
                    c += 1
                print(c,sep = '', end = '')
            else:
                c = 0
                if arr[i-1][j-1] == '#':
                    c += 1
                if arr[i-1][j] == '#':
                    c += 1
                if arr[i-1][j+1] == '#':
                    c += 1
                if arr[i][j-1] == '#':
                    c += 1
                if arr[i][j+1] == '#':
                    c += 1
                if arr[i+1][j-1] == '#':
                    c += 1
                if arr[i+1][j] == '#':
                    c += 1
                if arr[i+1][j+1] == '#':
                    c += 1
                print(c,sep = '', end = '')
    print('')

Submission status: Runtime Error

Solution code:
inputs = input().split()
H = int(inputs[0])
W = int(inputs[1])

grid = []

grid.append('.'*W+'..')
for i in range(H):
    inputs = input().strip()
    inputs = '.' + inputs + '.'
    grid.append(inputs)
grid.append('.'*W+'..')


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

ans = [[0 for x in range(W)] for y in range(H)]

for row in range(len(grid)-2):
    for col in range(W):
        if grid[row+1][col+1] == '#':
            ans[row][col] = '#'
        for i in range(8):
            if grid[row+1 + dy[i]][col+1 + dx[i]] == '#':
                ans[row][col] += 1
                
for row in ans:
    print(''.join(map(lambda x: str(x), row)))