Problem p03574 (python)

Fixed submission:
H, W = map(int, input().split())

INPUT = [list(input()) for i in range(H)]
OUTPUT = INPUT.copy()

for i in range(H):
    for j in range(W):
        if INPUT[i][j] == '#':
            OUTPUT[i][j] = '#'
            continue
        else:
            OUTPUT[i][j] = 0
            for I in (i-1, i, i+1):
                if (I < 0 or H <= I):
                    continue
                for J in (j-1, j, j+1):
                    if (J < 0 or W <= J):
                        continue
                    if INPUT[I][J] == '#':
                        OUTPUT[i][j] += 1


for i in range(H):
    print(''.join(map(str, OUTPUT[i])))

Buggy submission (verdict: Runtime Error):
H, W = map(int, input().split())

INPUT = [list(input()) for i in range(H)]
OUTPUT = INPUT.copy()

for i in range(H):
    for j in range(W):
        if INPUT[i][j] == '#':
            OUTPUT[i][j] = '#'a
        else:
            OUTPUT[i][j] = 0
            for I in (i-1, i, i+1):
                if not (I < 0 or H <= I):
                    for J in (j-1, j, j+1):H, W = map(int, input().split())

INPUT = [list(input()) for i in range(H)]
OUTPUT = INPUT.copy()

for i in range(H):
    for j in range(W):
        if INPUT[i][j] == '#':
            OUTPUT[i][j] = '#'
        else:
            OUTPUT[i][j] = 0
            for I in (i-1, i, i+1):
                if not (I < 0 or H <= I):
                    for J in (j-1, j, j+1):
                        if not (J < 0 or W <= J):
                            if INPUT[I][J] == '#':
                                OUTPUT[i][j] += 1


for i in range(H):
    print(''.join(map(str, OUTPUT[i])))

                        if not (J < 0 or W <= J):
                            if INPUT[I][J] == '#':
                                OUTPUT[i][j] += 1


for i in range(H):
    print(''.join(map(str, OUTPUT[i])))

Buggy submission (verdict: Runtime Error):
H, W = map(int, input().split())

INPUT = [list(input()) for i in range(H)]
OUTPUT = INPUT.copy()

for i in range(H):
    for j in range(W):
        if INPUT[i][j] == '#':
            OUTPUT[i][j] = '#'a
        else:
            OUTPUT[i][j] = 0
            for I in (i-1, i, i+1):
                if not (I < 0 or H <= I):
                    for J in (j-1, j, j+1):
                        if not (J < 0 or W <= J):
                            if INPUT[I][J] == '#':
                                OUTPUT[i][j] += 1


for i in range(H):
    print(''.join(map(str, OUTPUT[i])))

Buggy submission (verdict: Runtime Error):
H, W = map(int, input().split())
 
INPUT = [list(input()) for i in range(H)]
OUTPUT = INPUT.copy()
 
for i in range(H):
    for j in range(W):
        if INPUT[i][j] == '#':
            OUTPUT[i][j] = '#'
            continue
        else:
            OUTPUT[i][j] = 0
            for I in (i-1, i, i+1):
                if (I < 0 or l <= H):
                    continue
                for J in (j-1, j, j+1):
                    if (J < 0 or W <= J):
                        continue
                    if INPUT[I][J] == '#':
                        OUTPUT[i][j] += 1
 
 
for i in range(H):
    print(''.join(map(str, OUTPUT[i])))

Buggy submission (verdict: Runtime Error):
H, W = map(int, input().split())
 
INPUT = [list(input()) for i in range(H)]
OUTPUT = INPUT.copy()
 
for i in range(H):
    for j in range(W):
        if INPUT[i][j] == '#':
            OUTPUT[i][j] = '#'
        else:
            OUTPUT[i][j] = 0
            for I in (i-1, i, i+1):
                if not (I < 0 or l <= H):
                    for J in (j-1, j, j+1):
                        if not (J < 0 or W <= J):
                            if INPUT[I][J] == '#':
                                OUTPUT[i][j] += 1
 
 
for i in range(H):
    print(''.join(map(str, OUTPUT[i])))

Buggy submission (verdict: Runtime Error):
h, w=map(int, input().split())
s=[list(input()) for _ in range(h)]

def hantei(count,k,l):
    if s[k][l]=="#":
        count+=1
    return count

for i in range(h):
    for j in range(w):
        if s[i][j]==".":
            count=0
            if i ==0:
                if j==0:
                    for k,l in zip([i,i+1,i+1],[j+1,j,j+1]):
                        count=hantei(count,k,l)
                elif j==w-1:
                    for k,l in zip([i,i+1,i+1],[j-1,j-1,j]):
                        count=hantei(count,k,l)
                else:
                    for k,l in zip([i,i,i+1,i+1,i+1],[j-1,j+1,j-1,j,j+1]):
                        count=hantei(count,k,l)

            elif i==h-1:
                if j==0:
                    for k,l in zip([i-1,i-1,i],[j,j+1,j+1]):
                        count=hantei(count,k,l)
                elif j==w-1:
                    for k,l in zip([i,i-1,i-1],[j-1,j-1,j]):
                        count=hantei(count,k,l)
                else:
                    for k,l in zip([i,i,i-1,i-1,i-1],[j-1,j+1,j-1,j,j+1]):
                        count=hantei(count,k,l)

            elif j==0:
                for k,l in zip([i-1,i-1,i,i+1,i+1],[j,j+1,j+1,j,j+1]):
                        count=hantei(count,k,l)
            elif j==w-1:
                for k,l in zip([i-1,i-1,i,i+1,i+1],[j-1,j,j-1,j-1,j]):
                        count=hantei(count,k,l)
            else:
                for k,l in zip([i-1,i-1,i-1,i,i,i+1,i+1,i+1],[j-1,j,j+1,j-1,j+1,j-1,j,j+1]):
                            count=hantei(count,k,l)

            s[i][j]=str(count)

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