Problem p03574 (python)

Fixed submission:
H, W = map(int, input().split())
S = [list(input()) for _ in range(H)]

for i in range(H):
    for j in range(W):
        count = 0
        search = [[i-1, j-1], [i-1, j], [i-1, j+1], [i, j-1], [i, j+1], [i+1, j-1], [i+1, j], [i+1, j+1]]
        if S[i][j] == '#':
            continue
        for k in search:
            if k[0]<0 or k[0]>=H or k[1]<0 or k[1]>=W:
                continue
            if S[k[0]][k[1]] == '#':
                count += 1
        S[i][j] = str(count)

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

Buggy submission (verdict: Runtime Error):
H, W = map(int, input().split())
S = [list(input()) for _ in range(H)]

for i in range(H):
    for j in range(W):
        count = 0
        
        if i == 0 and H != 1:
            if j == 0:
                count = [S[i][j+1], S[i+1][j], S[i+1][j+1]].count('#')
            elif j == W - 1:
                count = [S[i][j-1], S[i+1][j], S[i+1][j-1]].count('#')
            else:
                count = [S[i][j-1], S[i][j+1], S[i+1][j-1], S[i+1][j], S[i+1][j+1]].count('#')
        elif i == 0 and H == 1:
            if j == 0:
                count = [S[i][j+1]].count('#')
            elif j == W - 1:
                count = [S[i][j-1]].count('#')
            else:
                count = [S[i][j-1], S[i][j+1]].count('#')

        elif i == H - 1:
            if j == 0:
                count = [S[i][j+1], S[i-1][j], S[i-1][j+1]].count('#')
            elif j == W - 1:
                count = [S[i-1][j-1], S[i-1][j], S[i][j-1]].count('#')
            else:
                count = [S[i][j-1], S[i][j+1], S[i-1][j-1], S[i-1][j], S[i-1][j+1]].count('#')
        else:
            if j == 0:
                count = [S[i-1][j], S[i-1][j+1], S[i][j+1], S[i+1][j], S[i+1][j+1]].count('#')
            elif j == W - 1:
                count =[S[i-1][j-1], S[i-1][j], S[i][j-1], S[i+1][j-1], S[i+1][j]].count('#')
            else:
                count =[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]].count('#')
        if S[i][j] != '#':
            S[i][j] = str(count)
for s in S:
    print("".join(s))

Buggy submission (verdict: Runtime Error):
H, W = map(int, input().split())
S = [list(input()) for _ in range(H)]

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

        elif i == H - 1:
            if j == 0:
                count = [S[i][j+1], S[i-1][j], S[i-1][j+1]].count('#')
            elif j == W - 1:
                count = [S[i-1][j-1], S[i-1][j], S[i][j-1]].count('#')
            else:
                count = [S[i][j-1], S[i][j+1], S[i-1][j-1], S[i-1][j], S[i-1][j+1]].count('#')
        else:
            if j == 0:
                count = [S[i-1][j], S[i-1][j+1], S[i][j+1], S[i+1][j], S[i+1][j+1]].count('#')
            elif j == W - 1:
                count =[S[i-1][j-1], S[i-1][j], S[i][j-1], S[i+1][j-1], S[i+1][j]].count('#')
            else:
                count =[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]].count('#')
        if S[i][j] != '#':
            S[i][j] = str(count)
for s in S:
    print("".join(s))