Problem p03574 (python)

Fixed submission:
h, w = map(int, input().split())
s = [[] for _ in range(h+2)]
s[0] = ["." for _ in range(w+2)]
s[h+1] = ["." for _ in range(w+2)]
for i in range(1, h+1):
    s[i].append(".")
    line = list(input())
    for j in range(w):
        s[i].append(line[j])
    s[i].append(".")

def bomb(x,y):
    if s[y][x] == "#":
        return 1
    else:
        return 0

for y in range(1,h+1):
    for x in range(1,w+1):
        if s[y][x] == ".":
            s[y][x] = bomb(x-1,y-1) + bomb(x,y-1) + bomb(x+1,y-1) + bomb(x-1,y) + bomb(x+1,y) + bomb(x-1,y+1) + bomb(x,y+1) + bomb(x+1,y+1)

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

Buggy submission (verdict: Runtime Error):
import sys
h, w = map(int,input().split())
s = []
for i in range(h):
    s.append(list(input()))
ans = [[] for _ in range(h)]
if h == 1 and w == 1:
    print(s[0][0])
    sys.exit()
elif h == 1 and w == 2:
    if s[0][0] == ".":
        if s[0][1] == "#":
            ans[0].append(1)
        else:
            ans[0].append(0)
    else:
        ans[0].append("#")
    if s[0][1] == ".":
        if s[0][0] == "#":
            ans[0].append(1)
        else:
            ans[0].append(0)
    else:
        ans[0].append("#")
    print(ans[0][0] , end="")
    print(ans[0][1])
    sys.exit()
elif h == 2 and w == 1:
    if s[0][0] == ".":
        if s[1][0] == "#":
            ans[0].append(1)
        else:
            ans[0].append(0)
    else:
        ans[0].append("#")
    if s[1][0] == ".":
        if s[0][0] == "#":
            ans[1].append(1)
        else:
            ans[1].append(0)
    else:
        ans[1].append("#")
    print(ans[0][0])
    print(ans[1][0])
    sys.exit()


for i in range(h):
    if i == 0:
        for j in range(w):
            if j == 0:
                if s[i][j] == ".":
                    count = 0
                    if s[i][j+1] == "#":
                        count += 1
                    if s[i+1][j+1] == "#":
                        count += 1
                    if s[i+1][j] == "#":
                        count += 1
                    ans[i].append(count)
                else:
                    ans[i].append("#")
            elif j == w-1:
                if s[i][j] == ".":
                    count = 0
                    if s[i][j-1] == "#":
                        count += 1
                    if s[i+1][j-1] == "#":
                        count += 1
                    if s[i+1][j] == "#":
                        count += 1
                    ans[i].append(count)
                else:
                    ans[i].append("#")
            else:
                if s[i][j] == ".":
                    count = 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
                    ans[i].append(count)
                else:
                    ans[i].append("#")
    elif i == h-1:
        for j in range(w):
            if j == 0:
                if s[i][j] == ".":
                    count = 0
                    if s[i][j+1] == "#":
                        count += 1
                    if s[i-1][j+1] == "#":
                        count += 1
                    if s[i-1][j] == "#":
                        count += 1
                    ans[i].append(count)
                else:
                    ans[i].append("#")
            elif j == w-1:
                if s[i][j] == ".":
                    count = 0
                    if s[i][j-1] == "#":
                        count += 1
                    if s[i-1][j-1] == "#":
                        count += 1
                    if s[i-1][j] == "#":
                        count += 1
                    ans[i].append(count)
                else:
                    ans[i].append("#")
            else:
                if s[i][j] == ".":
                    count = 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
                    ans[i].append(count)
                else:
                    ans[i].append("#")
    else:
        for j in range(w):
            if j == 0:
                if s[i][j] == ".":
                    count = 0
                    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
                    if s[i-1][j] == "#":
                        count += 1
                    ans[i].append(count)
                else:
                    ans[i].append("#")
            elif j == w-1:
                if s[i][j] == ".":
                    count = 0
                    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
                    if s[i-1][j] == "#":
                        count += 1
                    ans[i].append(count)
                else:
                    ans[i].append("#")
            else:
                if s[i][j] == ".":
                    count = 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
                    if s[i-1][j-1] == "#":
                        count += 1
                    if s[i-1][j] == "#":
                        count += 1
                    if s[i-1][j+1] == "#":
                        count += 1
                    ans[i].append(count)
                else:
                    ans[i].append("#")

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

Buggy submission (verdict: Runtime Error):
h, w = map(int,input().split())
s = []
for i in range(h):
    s.append(list(input()))
ans = [[] for _ in range(h)]
for i in range(h):
    if i == 0:
        for j in range(w):
            if j == 0:
                if s[i][j] == ".":
                    count = 0
                    if s[i][j+1] == "#":
                        count += 1
                    if s[i+1][j+1] == "#":
                        count += 1
                    if s[i+1][j] == "#":
                        count += 1
                    ans[i].append(count)
                else:
                    ans[i].append("#")
            elif j == w-1:
                if s[i][j] == ".":
                    count = 0
                    if s[i][j-1] == "#":
                        count += 1
                    if s[i+1][j-1] == "#":
                        count += 1
                    if s[i+1][j] == "#":
                        count += 1
                    ans[i].append(count)
                else:
                    ans[i].append("#")
            else:
                if s[i][j] == ".":
                    count = 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
                    ans[i].append(count)
                else:
                    ans[i].append("#")
    elif i == h-1:
        for j in range(w):
            if j == 0:
                if s[i][j] == ".":
                    count = 0
                    if s[i][j+1] == "#":
                        count += 1
                    if s[i-1][j+1] == "#":
                        count += 1
                    if s[i-1][j] == "#":
                        count += 1
                    ans[i].append(count)
                else:
                    ans[i].append("#")
            elif j == w-1:
                if s[i][j] == ".":
                    count = 0
                    if s[i][j-1] == "#":
                        count += 1
                    if s[i-1][j-1] == "#":
                        count += 1
                    if s[i-1][j] == "#":
                        count += 1
                    ans[i].append(count)
                else:
                    ans[i].append("#")
            else:
                if s[i][j] == ".":
                    count = 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
                    ans[i].append(count)
                else:
                    ans[i].append("#")
    else:
        for j in range(w):
            if j == 0:
                if s[i][j] == ".":
                    count = 0
                    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
                    if s[i-1][j] == "#":
                        count += 1
                    ans[i].append(count)
                else:
                    ans[i].append("#")
            elif j == w-1:
                if s[i][j] == ".":
                    count = 0
                    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
                    if s[i-1][j] == "#":
                        count += 1
                    ans[i].append(count)
                else:
                    ans[i].append("#")
            else:
                if s[i][j] == ".":
                    count = 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
                    if s[i-1][j-1] == "#":
                        count += 1
                    if s[i-1][j] == "#":
                        count += 1
                    if s[i-1][j+1] == "#":
                        count += 1
                    ans[i].append(count)
                else:
                    ans[i].append("#")

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