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:
#ABC 075 B - Minesweeper
H, W = input().split()
H = int(H)
W = int(W)
S = [input() for i in range(H)]

if H == 1:
    for w in range(1,W-1):
        s = S[0][w]
        if s == '.':
            count = 0
            if S[0][w-1] == '#': count+=1
            if S[0][w+1] == '#': count+=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] == '#': count+=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] == '#': count+=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] == '#': count+=1
            if S[h+1][w] == '#': count+=1
            S[h] = str(count)
    w = 0
    h = 0
    s = S[h][w]
    if s == '.':
            count = 0
            if S[h+1][w] == '#': count+=1
            S[h] = str(count)
    w = W-1
    h = 0
    s = S[h][w]
    if s == '.':
            count = 0
            if S[h-1][w] == '#': count+=1
            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] == '#': count+=1
        if S[0][w+1] == '#': count+=1
        if S[1][w-1] == '#': count+=1
        if S[1][w] == '#': count+=1
        if S[1][w+1] == '#': count+=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] == '#': count+=1
        if S[H-1][w+1] == '#': count+=1
        if S[H-2][w-1] == '#': count+=1
        if S[H-2][w] == '#': count+=1
        if S[H-2][w+1] == '#': count+=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] == '#': count+=1
            if S[h][w+1] == '#': count+=1
            if S[h-1][w-1] == '#': count+=1
            if S[h-1][w] == '#': count+=1
            if S[h-1][w+1] == '#': count+=1
            if S[h+1][w-1] == '#': count+=1
            if S[h+1][w] == '#': count+=1
            if S[h+1][w+1] == '#': count+=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] == '#': count+=1
        if S[h][w+1] == '#': count+=1
        #if S[h-1][w-1] == '#': count+=1
        if S[h-1][w] == '#': count+=1
        if S[h-1][w+1] == '#': count+=1
        #if S[h+1][w-1] == '#': count+=1
        if S[h+1][w] == '#': count+=1
        if S[h+1][w+1] == '#': count+=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] == '#': count+=1
        #if S[h][w+1] == '#': count+=1
        if S[h-1][w-1] == '#': count+=1
        if S[h-1][w] == '#': count+=1
        #if S[h-1][w+1] == '#': count+=1
        if S[h+1][w-1] == '#': count+=1
        if S[h+1][w] == '#': count+=1
        #if S[h+1][w+1] == '#': count+=1
        S[h] = S[h][:w] + str(count)

w = 0
h = 0
s = S[h][w]
if s == '.':
        count = 0
        #if S[h][w-1] == '#': count+=1
        if S[h][w+1] == '#': count+=1
        #if S[h-1][w-1] == '#': count+=1
        #if S[h-1][w] == '#': count+=1
        #if S[h-1][w+1] == '#': count+=1
        #if S[h+1][w-1] == '#': count+=1
        if S[h+1][w] == '#': count+=1
        if S[h+1][w+1] == '#': count+=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] == '#': count+=1
        if S[h][w+1] == '#': count+=1
        #if S[h-1][w-1] == '#': count+=1
        if S[h-1][w] == '#': count+=1
        if S[h-1][w+1] == '#': count+=1
        #if S[h+1][w-1] == '#': count+=1
        #if S[h+1][w] == '#': count+=1
        #if S[h+1][w+1] == '#': count+=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] == '#': count+=1
        #if S[h][w+1] == '#': count+=1
        #if S[h-1][w-1] == '#': count+=1
        #if S[h-1][w] == '#': count+=1
        #if S[h-1][w+1] == '#': count+=1
        if S[h+1][w-1] == '#': count+=1
        if S[h+1][w] == '#': count+=1
        #if S[h+1][w+1] == '#': count+=1
        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] == '#': count+=1
        #if S[h][w+1] == '#': count+=1
        if S[h-1][w-1] == '#': count+=1
        if S[h-1][w] == '#': count+=1
        #if S[h-1][w+1] == '#': count+=1
        #if S[h+1][w-1] == '#': count+=1
        #if S[h+1][w] == '#': count+=1
        #if S[h+1][w+1] == '#': count+=1
        S[h] = S[h][:w] + str(count)
        

        

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

Submission status: Runtime Error

Solution code:
#ABC 075 B - Minesweeper
H, W = input().split()
H = int(H)
W = int(W)
S = [input() for i in range(H)]

if H == 1:
    for w in range(1,W-1):
        s = S[0][w]
        if s == '.':
            count = 0
            if S[0][w-1] == '#': count+=1
            if S[0][w+1] == '#': count+=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] == '#': count+=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] == '#': count+=1
            S[h] = S[h][:w] + 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] == '#': count+=1
        if S[0][w+1] == '#': count+=1
        if S[1][w-1] == '#': count+=1
        if S[1][w] == '#': count+=1
        if S[1][w+1] == '#': count+=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] == '#': count+=1
        if S[H-1][w+1] == '#': count+=1
        if S[H-2][w-1] == '#': count+=1
        if S[H-2][w] == '#': count+=1
        if S[H-2][w+1] == '#': count+=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] == '#': count+=1
            if S[h][w+1] == '#': count+=1
            if S[h-1][w-1] == '#': count+=1
            if S[h-1][w] == '#': count+=1
            if S[h-1][w+1] == '#': count+=1
            if S[h+1][w-1] == '#': count+=1
            if S[h+1][w] == '#': count+=1
            if S[h+1][w+1] == '#': count+=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] == '#': count+=1
        if S[h][w+1] == '#': count+=1
        #if S[h-1][w-1] == '#': count+=1
        if S[h-1][w] == '#': count+=1
        if S[h-1][w+1] == '#': count+=1
        #if S[h+1][w-1] == '#': count+=1
        if S[h+1][w] == '#': count+=1
        if S[h+1][w+1] == '#': count+=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] == '#': count+=1
        #if S[h][w+1] == '#': count+=1
        if S[h-1][w-1] == '#': count+=1
        if S[h-1][w] == '#': count+=1
        #if S[h-1][w+1] == '#': count+=1
        if S[h+1][w-1] == '#': count+=1
        if S[h+1][w] == '#': count+=1
        #if S[h+1][w+1] == '#': count+=1
        S[h] = S[h][:w] + str(count)

w = 0
h = 0
s = S[h][w]
if s == '.':
        count = 0
        #if S[h][w-1] == '#': count+=1
        if S[h][w+1] == '#': count+=1
        #if S[h-1][w-1] == '#': count+=1
        #if S[h-1][w] == '#': count+=1
        #if S[h-1][w+1] == '#': count+=1
        #if S[h+1][w-1] == '#': count+=1
        if S[h+1][w] == '#': count+=1
        if S[h+1][w+1] == '#': count+=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] == '#': count+=1
        if S[h][w+1] == '#': count+=1
        #if S[h-1][w-1] == '#': count+=1
        if S[h-1][w] == '#': count+=1
        if S[h-1][w+1] == '#': count+=1
        #if S[h+1][w-1] == '#': count+=1
        #if S[h+1][w] == '#': count+=1
        #if S[h+1][w+1] == '#': count+=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] == '#': count+=1
        #if S[h][w+1] == '#': count+=1
        #if S[h-1][w-1] == '#': count+=1
        #if S[h-1][w] == '#': count+=1
        #if S[h-1][w+1] == '#': count+=1
        if S[h+1][w-1] == '#': count+=1
        if S[h+1][w] == '#': count+=1
        #if S[h+1][w+1] == '#': count+=1
        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] == '#': count+=1
        #if S[h][w+1] == '#': count+=1
        if S[h-1][w-1] == '#': count+=1
        if S[h-1][w] == '#': count+=1
        #if S[h-1][w+1] == '#': count+=1
        #if S[h+1][w-1] == '#': count+=1
        #if S[h+1][w] == '#': count+=1
        #if S[h+1][w+1] == '#': count+=1
        S[h] = S[h][:w] + str(count)
        

        

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

Submission status: Runtime Error

Solution code:
#ABC 075 B - Minesweeper
H, W = input().split()
H = int(H)
W = int(W)
S = [input() for i in range(H)]


for w in range(1,W-1):
    s = S[0][w]
    if s == '.':
        count = 0
        if S[0][w-1] == '#': count+=1
        if S[0][w+1] == '#': count+=1
        if S[1][w-1] == '#': count+=1
        if S[1][w] == '#': count+=1
        if S[1][w+1] == '#': count+=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] == '#': count+=1
        if S[H-1][w+1] == '#': count+=1
        if S[H-2][w-1] == '#': count+=1
        if S[H-2][w] == '#': count+=1
        if S[H-2][w+1] == '#': count+=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] == '#': count+=1
            if S[h][w+1] == '#': count+=1
            if S[h-1][w-1] == '#': count+=1
            if S[h-1][w] == '#': count+=1
            if S[h-1][w+1] == '#': count+=1
            if S[h+1][w-1] == '#': count+=1
            if S[h+1][w] == '#': count+=1
            if S[h+1][w+1] == '#': count+=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] == '#': count+=1
        if S[h][w+1] == '#': count+=1
        #if S[h-1][w-1] == '#': count+=1
        if S[h-1][w] == '#': count+=1
        if S[h-1][w+1] == '#': count+=1
        #if S[h+1][w-1] == '#': count+=1
        if S[h+1][w] == '#': count+=1
        if S[h+1][w+1] == '#': count+=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] == '#': count+=1
        #if S[h][w+1] == '#': count+=1
        if S[h-1][w-1] == '#': count+=1
        if S[h-1][w] == '#': count+=1
        #if S[h-1][w+1] == '#': count+=1
        if S[h+1][w-1] == '#': count+=1
        if S[h+1][w] == '#': count+=1
        #if S[h+1][w+1] == '#': count+=1
        S[h] = S[h][:w] + str(count)

w = 0
h = 0
s = S[h][w]
if s == '.':
        count = 0
        #if S[h][w-1] == '#': count+=1
        if S[h][w+1] == '#': count+=1
        #if S[h-1][w-1] == '#': count+=1
        #if S[h-1][w] == '#': count+=1
        #if S[h-1][w+1] == '#': count+=1
        #if S[h+1][w-1] == '#': count+=1
        if S[h+1][w] == '#': count+=1
        if S[h+1][w+1] == '#': count+=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] == '#': count+=1
        if S[h][w+1] == '#': count+=1
        #if S[h-1][w-1] == '#': count+=1
        if S[h-1][w] == '#': count+=1
        if S[h-1][w+1] == '#': count+=1
        #if S[h+1][w-1] == '#': count+=1
        #if S[h+1][w] == '#': count+=1
        #if S[h+1][w+1] == '#': count+=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] == '#': count+=1
        #if S[h][w+1] == '#': count+=1
        #if S[h-1][w-1] == '#': count+=1
        #if S[h-1][w] == '#': count+=1
        #if S[h-1][w+1] == '#': count+=1
        if S[h+1][w-1] == '#': count+=1
        if S[h+1][w] == '#': count+=1
        #if S[h+1][w+1] == '#': count+=1
        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] == '#': count+=1
        #if S[h][w+1] == '#': count+=1
        if S[h-1][w-1] == '#': count+=1
        if S[h-1][w] == '#': count+=1
        #if S[h-1][w+1] == '#': count+=1
        #if S[h+1][w-1] == '#': count+=1
        #if S[h+1][w] == '#': count+=1
        #if S[h+1][w+1] == '#': count+=1
        S[h] = S[h][:w] + str(count)
        

        

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

Submission status: Runtime Error

Solution code:
# python template for atcoder1
import sys
sys.setrecursionlimit(10**9)

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

grid = [["#"]*(W+2) for _ in range(H+2)]

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:
                        cntGrid[i+x][j+y] += 1
for l in cntGrid[1:-1]:
    print("".join(map(str, l[1:-1])))

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(10**9+7)

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

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
h,w = map(int,input().split())
s = [input() for i in range(h)]
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('#')
                pa = s[i][j-1:j+2].count('#')
                do = s[i+1][j-1:j+2].count('#')
                ans += str(up+pa+do)
            elif i==0 and j==0:
                pa = s[0][1].count('#')
                do = s[1][0:2].count('#')
                ans += str(pa+do)
            elif i==0 and j==w-1:
                pa = s[0][w-2:].count('#')
                do = s[1][w-2:].count('#')
                ans += str(pa+do)
            elif i==h-1 and j==0:
                up = s[i-1][:2].count('#')
                pa = s[i][:2].count('#')
                ans += str(up+pa)
            elif i==h-1 and j==w-1:
                up = s[i-1][w-2:].count('#')
                pa = s[i][w-2:].count('#')
                ans += str(up+pa)
            elif i==0:
                pa = s[0][j-1:j+2].count('#')
                do = s[1][j-1:j+2].count('#')
                ans += str(pa+do)
            elif j==0:
                up = s[i-1][:2].count('#')
                pa = s[i][:2].count('#')
                do = s[i+1][:2].count('#')
                ans += str(up+pa+do)
            elif i==h-1:
                up = s[i-1][j-1:j+2].count('#')
                pa = s[i][j-1:j+2].count('#')
                ans += str(up+pa)
            elif j==w-1:
                up = s[i-1][w-2:].count('#')
                pa = s[i][w-2:].count('#')
                do = s[i+1][w-2:].count('#')
                ans += str(up+pa+do)
    print(ans)

Submission status: Runtime Error

Solution code:
h,w = map(int,input().split())
s = [input() for i in range(h)]
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('#')
                pa = s[i][j-1:j+2].count('#')
                do = s[i+1][j-1:j+2].count('#')
                ans += str(up+pa+do)
            elif i==0 and j==0:
                pa = s[0][1].count('#')
                do = s[1][0:2].count('#')
                ans += str(pa+do)
            elif i==0 and j==w-1:
                pa = s[0][w-2:].count('#')
                do = s[1][w-2:].count('#')
                ans += str(pa+do)
            elif i==h-1 and j==0:
                up = s[i-1][:2].count('#')
                pa = s[i][:2].count('#')
                ans += str(up+pa)
            elif i==h-1 and j==w-1:
                up = s[i-1][j-1:].count('#')
                pa = s[i][j-1:].count('#')
                ans += str(up+pa)
            elif i==0:
                pa = s[i][j-1:j+2].count('#')
                do = s[i+1][j-1:j+2].count('#')
                ans += str(pa+do)
            elif j==0:
                up = s[i-1][:j+2].count('#')
                pa = s[i][:j+2].count('#')
                do = s[i+1][:j+2].count('#')
                ans += str(up+pa+do)
            elif i==h-1:
                up = s[i-1][j-1:j+2].count('#')
                pa = s[i][j-1:j+2].count('#')
                ans += str(up+pa)
            elif j==w-1:
                up = s[i-1][j-1:].count('#')
                pa = s[i][j-1:].count('#')
                do = s[i+1][j-1:].count('#')
                ans += str(up+pa+do)
    print(ans)

Submission status: Runtime Error

Solution code:
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][w] == '#':
      print('#', end = "")
      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()

Submission status: Runtime Error

Solution code:
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
            elif i==h-1:down=0
            if j==0:left=0
            elif j==w-1:right=0
            if up:
                if masu[i-1][j]=="#":bakudan+=1
            if up and left:
                if masu[i-1][j-1]=="#":bakudan+=1
            if up and right:
                if masu[i-1][j+1]=="#":bakudan+=1
            if left:
                if masu[i][j-1]=="#":bakudan+=1
            if left and down:
                if masu[i+1][j-1]=="#":bakudan+=1
            if down:
                if masu[i+1][j]=="#":bakudan+=1
            if down and right:
                if masu[i+1][j+1]=="#":bakudan+=1
            if right:
                if masu[i][j+1]=="#":bakudan+=1
            masu[i][j]=str(bakudan)
        up=1
        down=1
        left=1
        right=1

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

Submission status: Runtime Error

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

def check(lst, i, j, n, m):
    if 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 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)

Submission status: Runtime Error

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

S = []
ans_list = [[0]*W]*H
print(ans_list)
print(type(1))
print(type(ans_list[0][0]))
for i in range(H):
  s = input().split()
  s = list(s[0])
  S.append(s)
  
def counter1(i,j):#上
  if(S[i-1][j]=='#'):
     ans_list[i][j] += 1
def counter2(i,j):#下
  if(S[i+1][j]=='#'):
     ans_list[i][j] += 1 
def counter3(i,j):#右
  if(S[i][j+1]=='#'):
     ans_list[i][j] += 1   
def counter4(i,j):#左
  if(S[i][j-1]=='#'):
     ans_list[i][j] += 1  
def counter5(i,j):#右上
  if(S[i-1][j+1]=='#'):
     ans_list[i][j] += 1
def counter6(i,j):#右下
  if(S[i+1][j+1]=='#'):
     ans_list[i][j] += 1
def counter7(i,j):#左上
  if(S[i-1][j-1]=='#'):
     ans_list[i][j] += 1
def counter8(i,j):#左下
  if(S[i+1][j-1]=='#'):
     ans_list[i][j] += 1

for i in range(H):
  for j in range(W):
    if(S[i][j] == '#'):
      ans_list[i][j] = '#'
    elif(S[i][j] == '.'):
      if(i == 0):###1行目
        if(j == 0):
         counter2(i,j)#下
         counter3(i,j)#右
         counter6(i,j)#右下
        elif((j != 0)and(j != W-1)):
         counter2(i,j)#下
         counter3(i,j)#右
         counter4(i,j)#左
         counter6(i,j)#右下
         counter8(i,j)#左下
        elif(j == W-1):
         counter2(i,j)#下
         counter4(i,j)#左
         counter8(i,j)#左下
      elif((i != 0)and(i != H-1)):###真ん中の行
        if(j == 0):
         counter1(i,j)#上
         counter2(i,j)#下
         counter3(i,j)#右
         counter5(i,j)#右上
         counter6(i,j)#右下
        elif((j != 0)and(j != W-1)):
         counter1(i,j)#上
         counter2(i,j)#下
         counter3(i,j)#右
         counter4(i,j)#左
         counter5(i,j)#右上
         counter6(i,j)#右下
         counter7(i,j)#左上
         counter8(i,j)#左下
        elif(j == W-1):
         counter1(i,j)#上
         counter2(i,j)#下
         counter4(i,j)#左
         counter7(i,j)#左上
         counter8(i,j)#左下   
      elif(i == H-1):###最後の行
        if(j == 0):
         counter1(i,j)#上
         counter3(i,j)#右
         counter5(i,j)#右上
        elif((j != 0)and(j != W-1)):
         counter1(i,j)#上
         counter3(i,j)#右
         counter4(i,j)#左
         counter5(i,j)#右上
         counter7(i,j)#左上
        elif(j == W-1):
         counter1(i,j)#上
         counter4(i,j)#左
         counter7(i,j)#左上
  

print(ans_list)

Submission status: Runtime Error

Solution code:
h,w=map(int,input().split())
for i in range(h):
  line=(str(input()))
  line2=list(line)
  mapdata.append(line2)
result=[]
k=[]
def get_bomb(i,j):
  count=0
  for x in (i-1,i,i+1):
    for y in (j-1,j,j+1):
      if 0<=x<=h-1 and 0<=y<=w-1:
        if mapdata[x][y]=='#':
          count+=1
  return count
for i in range(h):
  for j in range(w):
    if mapdata[i][j]=='.':
      k.append(str(get_bomb(i,j)))
    else:
      k.append('#')
  result.append(k)
  k=[]
for i in range(len(result)):
  result2=''.join(result[i])
  print(result2)

Submission status: Runtime Error

Solution code:
h,w=map(int,input().split())
result=[]
k=[]
def get_bomb(i,j):
  count=0
  for x in (i-1,i,i+1):
    for y in (j-1,j,j+1):
      if 0<=x<=h-1 and 0<=y<=w-1:
        if mapdata[x][y]=='#':
          count+=1
  return count
for i in range(h):
  line=(str(input()))
  line2=list(line)
  mapdata.append(line2)
for i in range(h):
  for j in range(w):
    if mapdata[i][j]=='.':
      k.append(str(get_bomb(i,j)))
    else:
      k.append('#')
  result.append(k)
  k=[]
for i in range(len(result)):
  result2=''.join(result[i])
  print(result2)

Submission status: Runtime Error

Solution code:
h,w=map(int,input().split())
result=[]
k=[]
def get_bomb(i,j):
  count=0
  for x in (i-1,i,i+1):
    for y in (j-1,j,j+1):
      if 0<=x<=h-1 and 0<=y<=w-1:
        if mapdata[x][y]=='#':
          count+=1
  return count
for i in range(h):
  line=list(str(input()))
  mapdata.append(line)
for i in range(h):
  for j in range(w):
    if mapdata[i][j]=='.':
      k.append(str(get_bomb(i,j)))
    else:
      k.append('#')
  result.append(k)
  k=[]
for i in range(len(result)):
  result2=''.join(result[i])
  print(result2)

Submission status: Runtime Error

Solution code:
h,w=map(int,input().split())
result=[]
k=[]
def get_bomb(i,j):
  count=0
  for x in (i-1,i,i+1):
    for y in (j-1,j,j+1):
      if 0<=x<=h-1 and 0<=y<=w-1:
        if mapdata[x][y]=='#':
          count+=1
  return count
for i in range(h):
  line=list(input())
  mapdata.append(line)
for i in range(h):
  for j in range(w):
    if mapdata[i][j]=='.':
      k.append(str(get_bomb(i,j)))
    else:
      k.append('#')
  result.append(k)
  k=[]
for i in range(len(result)):
  result2=''.join(result[i])
  print(result2)

Submission status: Runtime Error

Solution code:
h,w=map(int,input().split())
mapdata=[]
result=[]
k=[]
def get_bomb(i,j):
  count=0
  for x in (i-1,i,i+1):
    for y in (j-1,j,j+1):
      if 0<=x<=h-1 and 0<=y<=w-1:
        if mapdata[x][y]=='#':
          count+=1
  return count
for i in range(h):
  line=list(input())
  mapdata.append(line)
for i in range(h):
  for j in range(w):
    if mapdata[i][j]=='.':
      k.append(get_bomb(i,j))
    else:
      k.append('#')
  result.append(k)
  k=[]
for i in range(len(result)):
  result2=','.join(result[i])
  print(result2)

Submission status: Runtime Error

Solution code:
h,w=map(int,input().split())
mapdata=[]
result=[]
k=[]
def get_bomb(i,j):
  count=0
  for x in (i-1.i,i+1):
    for y in (j-1,j,j+1):
      if 0<=x<=h-1 and 0<=y<=w-1:
        if mapdata[x][y]=='#':
          count+=1
  return count
for i in range(h):
  line=list(input())
  mapdata.append(line)
for i in range(h):
  for j in range(w):
    if mapdata[i][j]=='.':
      k.append(get_bomb(i,j))
    else:
      k.append('#')
  result.append(k)
  k=[]
for i in range(len(result)):
  print(result[i])