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:
import sys


def check(newLine, index, value, row, col):
    count = 0
    if 0 <= index - 1 < len(newLine) and index % row > 0:
        if newLine[index - 1] == "#":
            count += 1
    if 0 <= index + 1 < len(newLine) and index % row < 5:
        if newLine[index + 1] == "#":
            count += 1

    if 0 <= index - row < len(newLine):
        if newLine[index - row] == "#":
            count += 1
    if 0 <= index + row < len(newLine):
        if newLine[index + row] == "#":
            count += 1

    if 0 <= index - row - 1 < len(newLine) and index % row > 0:
        if newLine[index - row - 1] == "#":
            count += 1
    if 0 <= index - row + 1 < len(newLine) and index % row < 5:
        if newLine[index - row + 1] == "#":
            count += 1

    if 0 <= index + row - 1 < len(newLine) and index % row > 0:
        if newLine[index + row - 1] == "#":
            count += 1
    if 0 <= index + row + 1 < len(newLine) and index % row < 5:
        if newLine[index + row + 1] == "#":
            count += 1

    newLine[index] = str(count)
    return newLine


def main(lines):
    row = int(lines[0][0])
    col = int(lines[0][1])

    newLine = list("".join(lines[1:]))
    for index, value in enumerate(newLine):
        if value == ".":
            newLine = check(newLine, index, value, row, col)
    for i in range(0, len(newLine), row):
        print("".join(newLine[i:i + row]))


if __name__ == '__main__':
    lines = []
    for l in sys.stdin:
        lines.append(l.rstrip('\r\n'))
    main(lines)

Submission status: Runtime Error

Solution code:
H, W = map(int, input().split())
S = list(str(input()) for i in range(H))
Answer = [["#"]*W for i in range(H)]

if S[0][0] == ".":
    count = 0
    if S[0][1] == "#":
        count += 1
    if S[1][0] == "#":
        count += 1
    if S[1][1] == "#":
        count += 1
    Answer[0][0] = count

if S[0][W-1] == ".":
    count = 0
    if S[0][W-2] == "#":
        count += 1
    if S[1][W-1] == "#":
        count += 1
    if S[1][W-2] == "#":
        count += 1
    Answer[0][W-1] = count

if S[H-1][0] == ".":
    count = 0
    if S[H-1][1] == "#":
        count += 1
    if S[H-2][0] == "#":
        count += 1
    if S[H-2][1] == "#":
        count += 1
    Answer[H-1][0] = count

if S[H-1][W-1] == ".":
    count = 0
    if S[H-1][W-2] == "#":
        count += 1
    if S[H-2][W-1] == "#":
        count += 1
    if S[H-2][W-2] == "#":
        count += 1
    Answer[H-1][W-1] = count

for i in range(1, W-1):
    count = 0
    if S[0][i] == ".":
        if S[0][i-1] == "#":
            count += 1
        if S[0][i+1] == "#":
            count += 1
        if S[1][i] == "#":
            count += 1
        if S[1][i-1] == "#":
            count += 1
        if S[1][i-1] == "#":
            count += 1
        Answer[0][i] = count

for i in range(1, W-1):
    count = 0
    if S[H-1][i] == ".":
        if S[H-1][i-1] == "#":
            count += 1
        if S[H-1][i+1] == "#":
            count += 1
        if S[H-2][i] == "#":
            count += 1
        if S[H-2][i-1] == "#":
            count += 1
        if S[H-2][i+1] == "#":
            count += 1
        Answer[H-1][i] = count

for i in range(1, H-1):
    count = 0
    if S[i][0] == ".":
        if S[i][1] == "#":
            count += 1
        if S[i-1][0] == "#":
            count += 1
        if S[i-1][1] == "#":
            count += 1
        if S[i+1][0] == "#":
            count += 1
        if S[i+1][1] == "#":
            count += 1
        Answer[i][0] = count

for i in range(1, H-1):
    count = 0
    if S[i][W-1] == ".":
        if S[i][W-2] == "#":
            count += 1
        if S[i-1][W-1] == "#":
            count += 1
        if S[i-1][W-2] == "#":
            count += 1
        if S[i+1][W-1] == "#":
            count += 1
        if S[i+1][W-2] == "#":
            count += 1
        Answer[i][W-1] = count

for i in range(1, H-1):
    for j in range(1, W-1):
        count = 0
        if S[i][j] == ".":
            if S[i-1][j-1] == "#":
                count += 1
            if S[i-1][j] == "#":
                count += 1
            if S[i-1][j+1] == "#":
                count += 1
            if S[i][j-1] == "#":
                count += 1
            if S[i][j+1] == "#":
                count += 1
            if S[i+1][j-1] == "#":
                count += 1
            if S[i+1][j] == "#":
                count += 1
            if S[i+1][j+1] == "#":
                count += 1
            Answer[i][j] = count

for rooms in Answer:
    room=[str(a) for a in rooms]
    room = "".join(room)
    print(room)

Submission status: Runtime Error

Solution code:
import numpy as np
h,w=map(int, input().split( ))
s=[input() for _ in range(h)]
a = [-1,0,1]
b=[-1,0,1]
n=[(i,j) for i in a for j in b]
#print(n)
ans=[["#"]*w for _ in range(h)]

for i in range(h):
    for j in range(w):
        tmp=0
        if s[i][j]=="#":
            continue
        for x,y in n:
            if 0<=i+x<h and 0<=j+y<w:
                if (x or y) and s[i+x][j+y]=="#":
                    tmp+=1
        ans[i][j]=str(tmp)
for i in range(h):
    print(''.join(ans[i]))

Submission status: Runtime Error

Solution code:
h,w=map(int,input().split())
s = [list(input()) for _ in range(h)]
#print(s)
l=[]
if h==1:
    for i in range(w):
        if s[h-1][i]!="#":
            if i==0:
                l.append(s[h-1][i+1])
                s[h-1][i]=l.count("#")
                l=[]
            elif i==w-1:
                l.append(s[h-1][i-1])
                s[h-1][i]=l.count("#")
                l=[]
            else:
                l.append(s[h-1][i-1])
                l.append(s[h-1][i+1])
                s[h-1][j]=l.count("#")
                l=[]
elif h==2:
    for i in range(h):
        for j in range(w):
            if s[i][j]!="#":
                if i==0 and 1<=j<=w-2:
                    l.append(s[i][j-1])
                    l.append(s[i][j+1])
                    l.append(s[i+1][j-1])
                    l.append(s[i+1][j])
                    l.append(s[i+1][j+1])
                    s[i][j]=l.count("#")
                    l=[]

                elif i==0 and 1<=j<=w-2:
                    l.append(s[i][j-1])
                    l.append(s[i][j+1])
                    l.append(s[i-1][j-1])
                    l.append(s[i-1][j])
                    l.append(s[i-1][j+1])
                    s[i][j]=l.count("#")
                    l=[]


                elif j==0 and i==0:
                    l.append(s[i][j+1])
                    l.append(s[i+1][j+1])
                    l.append(s[i+1][j])

                    s[i][j]=l.count("#")
                    l=[]
                elif j==0 and i==h-1:
                    l.append(s[i-1][j])
                    l.append(s[i][j+1])
                    l.append(s[i-1][j+1])

                    s[i][j]=l.count("#")
                    l=[]
                elif j==w-1 and i==0:
                    l.append(s[i][j-1])
                    l.append(s[i+1][j])
                    l.append(s[i+1][j-1])

                    s[i][j]=l.count("#")
                    l=[]
                elif j==w-1 and i==h-1:
                    l.append(s[i-1][j-1])
                    l.append(s[i][j-1])
                    l.append(s[i-1][j])

                    s[i][j]=l.count("#")
                    l=[]



else:
    for i in range(h):
        for j in range(w):
            if s[i][j]!="#":
                if 1<=j<=w-2:
                    if i==0:
                        l.append(s[i][j-1])
                        l.append(s[i][j+1])

                        l.append(s[i+1][j-1])
                        l.append(s[i+1][j])
                        l.append(s[i+1][j+1])

                        s[i][j]=l.count("#")
                        l=[]
                    elif i ==h-1:
                        l.append(s[i][j-1])
                        l.append(s[i][j+1])

                        l.append(s[i-1][j-1])
                        l.append(s[i-1][j])
                        l.append(s[i-1][j+1])

                        s[i][j]=l.count("#")
                        l=[]

                    else:
                        l.append(s[i][j-1])
                        l.append(s[i][j+1])

                        l.append(s[i+1][j-1])
                        l.append(s[i+1][j])
                        l.append(s[i+1][j+1])

                        l.append(s[i-1][j-1])
                        l.append(s[i-1][j])
                        l.append(s[i-1][j+1])

                        s[i][j]=l.count("#")
                        l=[]
                elif j==0 and i==0:
                    l.append(s[i][j+1])
                    l.append(s[i+1][j+1])
                    l.append(s[i+1][j])

                    s[i][j]=l.count("#")
                    l=[]
                elif j==0 and i==h-1:
                    l.append(s[i-1][j])
                    l.append(s[i][j+1])
                    l.append(s[i-1][j+1])

                    s[i][j]=l.count("#")
                    l=[]
                elif j==w-1 and i==0:
                    l.append(s[i][j-1])
                    l.append(s[i+1][j])
                    l.append(s[i+1][j-1])

                    s[i][j]=l.count("#")
                    l=[]
                elif j==w-1 and i==h-1:
                    l.append(s[i-1][j-1])
                    l.append(s[i][j-1])
                    l.append(s[i-1][j])

                    s[i][j]=l.count("#")
                    l=[]
                elif j==0 and 1<=i<=h-2:
                    l.append(s[i-1][j])
                    l.append(s[i][j+1])
                    l.append(s[i-1][j+1])
                    l.append(s[i+1][j+1])
                    l.append(s[i+1][j])

                    s[i][j]=l.count("#")
                    l=[]
                elif j==w-1 and 1<=i<=h-2:
                    l.append(s[i-1][j])
                    l.append(s[i][j-1])
                    l.append(s[i-1][j-1])
                    l.append(s[i+1][j-1])
                    l.append(s[i+1][j])

                    s[i][j]=l.count("#")
                    l=[]

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

Submission status: Runtime Error

Solution code:
h,w=map(int,input().split())
s = [list(input()) for _ in range(h)]
#print(s)
l=[]

for i in range(h):
    for j in range(w):
        if s[i][j]!="#":
            if 1<=j<=w-2:
                if i==0:
                    l.append(s[i][j-1])
                    l.append(s[i][j+1])

                    l.append(s[i+1][j-1])
                    l.append(s[i+1][j])
                    l.append(s[i+1][j+1])

                    s[i][j]=l.count("#")
                    l=[]
                elif i ==h-1:
                    l.append(s[i][j-1])
                    l.append(s[i][j+1])

                    l.append(s[i-1][j-1])
                    l.append(s[i-1][j])
                    l.append(s[i-1][j+1])

                    s[i][j]=l.count("#")
                    l=[]

                else:
                    l.append(s[i][j-1])
                    l.append(s[i][j+1])

                    l.append(s[i+1][j-1])
                    l.append(s[i+1][j])
                    l.append(s[i+1][j+1])

                    l.append(s[i-1][j-1])
                    l.append(s[i-1][j])
                    l.append(s[i-1][j+1])

                    s[i][j]=l.count("#")
                    l=[]
            elif j==0 and i==0:
                l.append(s[i][j+1])
                l.append(s[i+1][j+1])
                l.append(s[i+1][j])

                s[i][j]=l.count("#")
                l=[]
            elif j==0 and i==h-1:
                l.append(s[i-1][j])
                l.append(s[i][j+1])
                l.append(s[i-1][j+1])

                s[i][j]=l.count("#")
                l=[]
            elif j==w-1 and i==0:
                l.append(s[i][j-1])
                l.append(s[i+1][j])
                l.append(s[i+1][j-1])

                s[i][j]=l.count("#")
                l=[]
            elif j==w-1 and i==h-1:
                l.append(s[i-1][j-1])
                l.append(s[i][j-1])
                l.append(s[i-1][j])

                s[i][j]=l.count("#")
                l=[]
            elif j==0 and 1<=i<=h-2:
                l.append(s[i-1][j])
                l.append(s[i][j+1])
                l.append(s[i-1][j+1])
                l.append(s[i+1][j+1])
                l.append(s[i+1][j])

                s[i][j]=l.count("#")
                l=[]
            elif j==w-1 and 1<=i<=h-2:
                l.append(s[i-1][j])
                l.append(s[i][j-1])
                l.append(s[i-1][j-1])
                l.append(s[i+1][j-1])
                l.append(s[i+1][j])

                s[i][j]=l.count("#")
                l=[]

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

Submission status: Runtime Error

Solution code:
h,w=map(int,input().split())
s = [list(input()) for _ in range(h)]
#print(s)
l=[]

for i in range(h):
    for j in range(w):
        if s[i][j]!="#":
            if 1<=j<=w-2:
                if i==1:
                    l.append(s[i][j-1])
                    l.append(s[i][j+1])

                    l.append(s[i+1][j-1])
                    l.append(s[i+1][j])
                    l.append(s[i+1][j+1])

                    s[i][j]=l.count("#")
                    l=[]
                elif i ==h-1:
                    l.append(s[i][j-1])
                    l.append(s[i][j+1])

                    l.append(s[i-1][j-1])
                    l.append(s[i-1][j])
                    l.append(s[i-1][j+1])

                    s[i][j]=l.count("#")
                    l=[]

                else:
                    l.append(s[i][j-1])
                    l.append(s[i][j+1])

                    l.append(s[i+1][j-1])
                    l.append(s[i+1][j])
                    l.append(s[i+1][j+1])

                    l.append(s[i-1][j-1])
                    l.append(s[i-1][j])
                    l.append(s[i-1][j+1])

                    s[i][j]=l.count("#")
                    l=[]
            elif j==0 and i==0:
                l.append(s[i][j+1])
                l.append(s[i+1][j+1])
                l.append(s[i+1][j])

                s[i][j]=l.count("#")
                l=[]
            elif j==0 and i==h-1:
                l.append(s[i-1][j])
                l.append(s[i][j+1])
                l.append(s[i-1][j+1])

                s[i][j]=l.count("#")
                l=[]
            elif j==w-1 and i==0:
                l.append(s[i][j-1])
                l.append(s[i+1][j-1])
                l.append(s[i-1][j])

                s[i][j]=l.count("#")
                l=[]
            elif j==w-1 and i==h-1:
                l.append(s[i-1][j-1])
                l.append(s[i][j-1])
                l.append(s[i-1][j])

                s[i][j]=l.count("#")
                l=[]
            elif j==0 and 1<=i<=h-2:
                l.append(s[i-1][j])
                l.append(s[i][j+1])
                l.append(s[i-1][j+1])
                l.append(s[i+1][j+1])
                l.append(s[i+1][j])

                s[i][j]=l.count("#")
                l=[]
            elif j==w-1 and 1<=i<=h-2:
                l.append(s[i-1][j])
                l.append(s[i][j-1])
                l.append(s[i-1][j-1])
                l.append(s[i+1][j-1])
                l.append(s[i+1][j])

                s[i][j]=l.count("#")
                l=[]

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

Submission status: Runtime Error

Solution code:
h,w=map(int,input().split())
s = [list(input()) for _ in range(h)]
#print(s)
l=[]

for i in range(h):
    for j in range(w):
        if s[i][j]!="#":
            if 1<=j<=w-2:
                if i==1:
                    l.append(s[i][j-1])
                    l.append(s[i][j+1])

                    l.append(s[i+1][j-1])
                    l.append(s[i+1][j])
                    l.append(s[i+1][j+1])

                    s[i][j]=l.count("#")
                    l=[]
                elif i ==h-1:
                    l.append(s[i][j-1])
                    l.append(s[i][j+1])

                    l.append(s[i-1][j-1])
                    l.append(s[i-1][j])
                    l.append(s[i-1][j+1])

                    s[i][j]=l.count("#")
                    l=[]

                else:
                    l.append(s[i][j-1])
                    l.append(s[i][j+1])

                    l.append(s[i+1][j-1])
                    l.append(s[i+1][j])
                    l.append(s[i+1][j+1])

                    l.append(s[i-1][j-1])
                    l.append(s[i-1][j])
                    l.append(s[i-1][j+1])

                    s[i][j]=l.count("#")
                    l=[]
            elif j==0 and i==0:
                l.append(s[i][j+1])
                l.append(s[i+1][j+1])
                l.append(s[i+1][j])

                s[i][j]=l.count("#")
                l=[]
            elif j==0 and i==h-1:
                l.append(s[i-1][j])
                l.append(s[i][j+1])
                l.append(s[i-1][j+1])

                s[i][j]=l.count("#")
                l=[]
            elif j==w-1 and i==0:
                l.append(s[i][j-1])
                l.append(s[i+1][j-1])
                l.append(s[i-1][j])

                s[i][j]=l.count("#")
                l=[]
            elif j==w-1 and i==h-1:
                l.append(s[i-1][j-1])
                l.append(s[i][j-1])
                l.append(s[i-1][j])

                s[i][j]=l.count("#")
                l=[]
            elif j==0 and 1<=i<=h-2:
                l.append(s[i-1][j])
                l.append(s[i][j+1])
                l.append(s[i-1][j+1])
                l.append(s[i+1][j+1])
                l.append(s[i+1][j])

                s[i][j]=l.count("#")
                l=[]
            elif j==w-1 and 1<=i<=h-2:
                l.append(s[i-1][j])
                l.append(s[i][j-1])
                l.append(s[i-1][j-1])
                l.append(s[i+1][j-1])
                l.append(s[i+1][j])

                s[i][j]=l.count("#")
                l=[]

print(s)

Submission status: Runtime Error

Solution code:
h, w = [int(word) for word in input().split()]

s = [["."]*(w+2)]

for i in range(1, h+1):
    row = [v for v in input().split()]
    row.insert(0, ".")
    row.append(".")
    s.append(row)

s.append(["."]*(w+2))

result = s

for r in range(1, h+1):
    for c in range(1, w+1):
        if s[r][c] == "#":
            result[r][c] = "#"
        else:
            count = 0
            for r2 in [-1, 0 , 1]:
                for c2 in [-1, 0, 1]:
                    if s[r+r2][c+c2] == "#":
                        count += 1
            result[r][c] = str(count)

for r in range(1, h+1):
    ans = "".join(s[r][1:w+1])
    print(ans)

Submission status: Runtime Error

Solution code:
h, w = [int(word) for word in input().split()]

s = [["."]*(w+2)]

for i in range(1, h+1):
    row = [v for v in input().split()]
    row.insert(0, ".")
    row.append(".")
    s.append(row)

s.append(["."]*(w+2))

result = s
print(s)

for r in range(1, h+1):
    for c in range(1, w+1):
        if s[r][c] == "#":
            result[r][c] = "#"
        else:
            count = 0
            for r2 in [-1, 0 , 1]:
                for c2 in [-1, 0, 1]:
                    if s[r+r2][c+c2] == "#":
                        count += 1
            result[r][c] = str(count)

for r in range(1, h+1):
    ans = "".join(s[r][1:w+1])
    print(ans)

Submission status: Runtime Error

Solution code:
h, w = [int(word) for word in input().split()]

s = [["."]*(w+2)]

for i in range(1, h+1):
    row = [v for v in input().split()]
    row.insert(0, ".")
    row.append(".")
    s.append(row)

s.append(["."]*(w+2))

result = s

for r in range(1, h+1):
    for c in range(1, w+1):
        if s[r][c] == "#":
            result[r][c] = "#"
        else:
            count = 0
            for r2 in [-1, 0 , 1]:
                for c2 in [-1, 0, 1]:
                    if s[r+r2][c+c2] == "#":
                        count += 1
            result[r][c] = str(count)

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

Submission status: Runtime Error

Solution code:
h, w = [int(word) for word in input().split()]

s = [["."]*(w+2)]

for i in range(1, h+1):
    row = [v for v in input().split()]
    row.insert(0, ".")
    row.append(".")
    s.append(row)

s.append(["."]*(w+2))

result = s

for r in range(1, h+1):
    for c in range(1, w+1):
        if s[r][c] == "#":
            result[r][c] = "#"
        else:
            count = 0
            for r2 in [-1, 0 , 1]:
                for c2 in [-1, 0, 1]:
                    if s[r+r2][c+c2] == "#":
                        count += 1
            result[r][c] = str(count)

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

Submission status: Runtime Error

Solution code:
# 読み込み
H, W = map(int, input().split())
S = []
for i in range(H):
    S.append(list(map(str, input().rstrip())))

# 外枠を「.」で囲う準備
li = []
for i in range(H+2):
    li.append(["."]*(W+2))

# 外枠付きSの生成
for i in range(1, H+1):
    li[i][1:-1] = S[i-1]

# カウント用配列の準備
ans = []
for i in range(H+2):
    ans.append([0]*(W+2))
ans = np.array(ans)

# カウント処理
for h in range(1, H+1):
    for w in range(1, W+1):
        if li[h][w]=="#":
            ans[h-1:h+2, w-1:w+2] += 1

# カウント結果から必要な場所だけ切り出す
ans = ans.astype("str")
ans = ans[1:-1, 1:-1]

# 「#」で上書きしながら出力
for h in range(H):
    for w in range(W):
        if S[h][w]=="#":
            ans[h, w] = "#"
    print("".join(ans[h]))

Submission status: Runtime Error

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

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

for h in range(H):
    for w in range(W):
        li[h+1][w+1] = input()

S = [i[1:-1] for i in li][1:-1]

import numpy as np
ans = np.zeros([H+2, W+2], dtype=int)

for h in range(1, H):
    for w in range(1, W):
        if li[h][w]=="#":
            ans[h-1:h+2, w-1:w+2] += 1      

ans = ans.astype("str")
ans = ans[1:-1, 1:-1]

for h in range(H):
    for w in range(W):
        if S[h][w]=="#":
            ans[h, w] = "#"

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

Submission status: Runtime Error

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

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

for h in range(H):
    for w in range(W):
        li[h+1][w+1] = input()

S = [i[1:-1] for i in li][1:-1]

import numpy as np
ans = np.zeros([H+2, W+2], dtype=int)

for h in range(1, H+1):
    for w in range(1, W+1):
        if li[h][w]=="#":
            ans[h-1:h+2, w-1:w+2] += 1      

ans = ans.astype("str")
ans = ans[1:-1, 1:-1]

for h in range(H):
    for w in range(W):
        if S[h][w]=="#":
            ans[h, w] = "#"

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

Submission status: Runtime Error

Solution code:
def replace(s, r, c):
    for row in range(r - 1, r + 2):
        for column in range(c - 1, c + 2):
            value = s[row][column]
            if value != "#":
                value = value + 1 if value != "." else 1
                s[row][column] = value

h, w = map(int, input().split(" "))
s = []
for _ in range(h):
    row = input()
    tmp = []
    for i in range(w):
        tmp.append(row[i])
    s.append(tmp)

for row in range(h):
    for column in range(w):
        if s[row][column] == "#":
            replace(s, row, column)

for row in range(h):
    res = ""
    for column in range(w):
        res += str(s[row][column])
    print(res)

Submission status: Runtime Error

Solution code:
h, w = map(int, input().split(" "))
s = []
for _ in range(h):
    row = input()
    tmp = []
    for i in range(w):
        tmp.append(row[i])
    s.append(tmp)

for row in range(h):
    for column in range(w):
        if s[row][column] == "#":
            replace(s, row, column)

for row in range(h):
    res = ""
    for column in range(w):
        res += str(s[row][column])
    print(res)

Submission status: Runtime Error

Solution code:
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] == '.':
            l = max(0, j - 1)
            r = min(j + 2, w)
            if i == 0:
                s[i][j] = s[i][l:r].count('#') + s[i + 1][l:r].count('#')
            elif i == h - 1:
                s[i][j] = s[i - 1][l:r].count('#') + s[i][l:r].count('#')
            else:
                s[i][j] = s[i - 1][l:r].count('#') + s[i][l:r].count('#') + s[i + 1][l:r].count('#')
    print(*s[i], sep='')

Submission status: Runtime Error

Solution code:
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] == '.':
            if i == 0:
                s[i][j] = s[i][max(0, j - 1):j + 2].count('#') + s[i + 1][max(0, j - 1):j + 2].count('#')
            elif i == h - 1:
                s[i][j] = s[i - 1][max(0, j - 1):j + 2].count('#') + s[i][max(0, j - 1):j + 2].count('#')
            else:
                s[i][j] = s[i - 1][max(0, j - 1):j + 2].count('#') + s[i][max(0, j - 1):j + 2].count('#') + s[i + 1][max(0, j - 1):j + 2].count('#')
    print(*s[i], sep='')

Submission status: Runtime Error

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

for i in range(h):
    for j in range(w):
        count=0
        if s[i][j]==".":
            if ((i==0)&(j==0))|((i==0)&(j==w-1))|((i==h-1)&(j==0))|((i==h-1)&(j==w-1)):
                if (i==0)&(j==0):
                    if s[1][0]=="#":
                        count+=1
                    if s[0][1]=="#":
                        count+=1
                    if s[1][1]=="#":
                        count+=1
                if (i==0)&(j==w-1):
                    if s[1][w-1]=="#":
                        count+=1
                    if s[0][w-2]=="#":
                        count+=1
                    if s[1][w-2]=="#":
                        count+=1
                if (i==h-1)&(j==0):
                    if s[h-2][0]=="#":
                        count+=1
                    if s[h-1][1]=="#":
                        count+=1
                    if s[h-2][1]=="#":
                        count+=1
                if (i==h-1)&(j==w-1):
                    if s[h-1][w-2]=="#":
                        count+=1
                    if s[h-2][w-1]=="#":
                        count+=1
                    if s[h-2][w-2]=="#":
                        count+=1
            elif i==0:
                if s[i][j-1]=="#":
                    count+=1
                if s[i][j+1]=="#":
                    count+=1
                if s[i+1][j-1]=="#":
                    count+=1
                if s[i+1][j]=="#":
                    count+=1
                if s[i+1][j+1]=="#":
                    count+=1
            elif i==h-1:
                if s[i][j-1]=="#":
                    count+=1
                if s[i][j+1]=="#":
                    count+=1
                if s[i-1][j-1]=="#":
                    count+=1
                if s[i-1][j]=="#":
                    count+=1
                if s[i-1][j+1]=="#":
                    count+=1
            elif j==0:
                if s[i-1][j]=="#":
                    count+=1
                if s[i+1][j]=="#":
                    count+=1
                if s[i-1][j+1]=="#":
                    count+=1
                if s[i][j+1]=="#":
                    count+=1
                if s[i+1][j+1]=="#":
                    count+=1
            elif j==w-1:
                if s[i-1][j]=="#":
                    count+=1
                if s[i+1][j]=="#":
                    count+=1
                if s[i-1][j-1]=="#":
                    count+=1
                if s[i][j-1]=="#":
                    count+=1
                if s[i+1][j-1]=="#":
                    count+=1
            else:
                if s[i-1][j-1]=="#":
                    count+=1
                if s[i-1][j]=="#":
                    count+=1
                if s[i-1][j+1]=="#":
                    count+=1
                if s[i][j-1]=="#":
                    count+=1
                if s[i][j+1]=="#":
                    count+=1
                if s[i+1][j-1]=="#":
                    count+=1
                if s[i+1][j]=="#":
                    count+=1
                if s[i+1][j+1]=="#":
                    count+=1
            s[i][j]=str(count)
for a in range(h):
    print("".join(s[a]))