Problem p03574 (python)

Fixed submission:
H,W = map(int,input().split(" "))
S = []
for i in range(H):
    S.append(list(input()))
for i in range(H):
    for j in range(W):
        if S[i][j] == ".":
            S[i][j] = 0
if H == 1 and W == 1:
    print(S[0][0])
elif H == 1:
    if S[0][0] == "#":
        if S[0][1]!="#": S[0][1]+= 1
    if S[0][W-1]== "#":
        if S[0][W-2]!="#": S[0][W-2]+= 1
    for i in range(1,W-1):
        if S[0][i] =="#":
            if S[0][i-1]!="#": S[0][i-1]+= 1
            if S[0][i+1]!="#": S[0][i+1]+= 1
    
    for i in range(H):
        print("".join(map(str,S[i][:])))


elif W == 1:
    if S[0][0] == "#":
        if S[1][0]!="#": S[1][0]+= 1
    if S[H-1][0]== "#":
        if S[H-2][0]!="#": S[H-2][0]+= 1
    for i in range(1,H-1):
        if S[i][0] =="#":
            if S[i-1][0]!="#": S[i-1][0]+= 1
            if S[i+1][0]!="#": S[i+1][0]+= 1
    for i in range(H):
        print("".join(map(str,S[i][:])))

else:
    #4隅の処理
    if S[0][0] == "#":
        if S[1][0] !="#": S[1][0]+= 1
        if S[0][1] !="#": S[0][1]+= 1
        if S[1][1] !="#": S[1][1]+= 1
    if S[0][W-1] == "#":
        if S[0][W-2] !="#": S[0][W-2]+= 1
        if S[1][W-2] !="#": S[1][W-2]+= 1
        if S[1][W-1] !="#": S[1][W-1]+= 1
    if S[H-1][0] == "#":
        if S[H-2][0] !="#": S[H-2][0]+= 1
        if S[H-1][1] !="#": S[H-1][1]+= 1
        if S[H-2][1] !="#": S[H-2][1]+= 1
    if S[H-1][W-1] == "#":
        if S[H-2][W-1] !="#": S[H-2][W-1]+= 1
        if S[H-2][W-2] !="#": S[H-2][W-2]+= 1
        if S[H-1][W-2] !="#": S[H-1][W-2]+= 1
    #端の処理
    for i in range(1,H-1):
        if S[i][0] == "#":
            if S[i-1][0] !="#": S[i-1][0]+= 1
            if S[i+1][0] !="#": S[i+1][0]+= 1
            if S[i][1] !="#": S[i][1]+= 1
            if S[i-1][1] !="#": S[i-1][1]+= 1
            if S[i+1][1] !="#": S[i+1][1]+= 1

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

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

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

    for i in range(1,H-1):
        for j in range(1,W-1):   
            if S[i][j] == "#":
                if S[i-1][j-1] !="#": S[i-1][j-1]+= 1
                if S[i-1][j] !="#": S[i-1][j]+= 1
                if S[i-1][j+1] !="#": S[i-1][j+1]+= 1
                if S[i][j-1] !="#": S[i][j-1]+= 1
                if S[i][j+1] !="#": S[i][j+1]+= 1
                if S[i+1][j-1] !="#": S[i+1][j-1]+= 1
                if S[i+1][j] !="#": S[i+1][j]+= 1
                if S[i+1][j+1] !="#": S[i+1][j+1]+= 1
    for i in range(H):
        print("".join(map(str,S[i][:])))
# # def replace(i,j,S):
#     yp = i + 1
#     ym = i - 1
#     xp = j + 1
#     xm = j - 1
#     count = 0 
#     if ym >= 0:
#         if S[ym][j] == "#":
#             count += 1
#         if S[yp][j] == "#":
#             count += 1
#     return S
# # print(S)
# print(S)

Buggy submission (verdict: Runtime Error):
H,W = map(int,input().split(" "))
S = []
for i in range(H):
    S.append(list(input()))
for i in range(H):
    for j in range(W):
        if S[i][j] == ".":
            S[i][j] = 0
if H == 1 and W == 1:
    print(S[0][0])
elif H == 1:
    if S[0][0] == "#":
        if S[0][1]!="#": S[0][1]+= 1
    if S[0][H]== "#":
        if S[0][H-1]!="#": S[0][H-1]+= 1
    for i in range(1,H-1):
        if S[0][i-1]!="#": S[0][i-1]+= 1
        if S[0][i+1]!="#": S[0][i+1]+= 1
    
    for i in range(H):
        print("".join(map(str,S[i][:])))


elif W == 1:
    if S[0][0] == "#":
        if S[0][1]!="#": S[0][1]+= 1
    if S[W][0]== "#":
        if S[W-1][0]!="#": S[W-1][0]+= 1
    for i in range(1,W-1):
        if S[i-1][0]!="#": S[i-1][0]+= 1
        if S[i+1][0]!="#": S[i+1][0]+= 1
    for i in range(H):
        print("".join(map(str,S[i][:])))

else:
    #4隅の処理
    if S[0][0] == "#":
        if S[1][0] !="#": S[1][0]+= 1
        if S[0][1] !="#": S[0][1]+= 1
        if S[1][1] !="#": S[1][1]+= 1
    if S[0][W-1] == "#":
        if S[0][W-2] !="#": S[0][W-2]+= 1
        if S[1][W-2] !="#": S[1][W-2]+= 1
        if S[1][W-1] !="#": S[1][W-1]+= 1
    if S[H-1][0] == "#":
        if S[H-2][0] !="#": S[H-2][0]+= 1
        if S[H-1][1] !="#": S[H-1][1]+= 1
        if S[H-2][1] !="#": S[H-2][1]+= 1
    if S[H-1][W-1] == "#":
        if S[H-2][W-1] !="#": S[H-2][W-1]+= 1
        if S[H-2][W-2] !="#": S[H-2][W-2]+= 1
        if S[H-1][W-2] !="#": S[H-1][W-2]+= 1
    #端の処理
    for i in range(1,H-1):
        if S[i][0] == "#":
            if S[i-1][0] !="#": S[i-1][0]+= 1
            if S[i+1][0] !="#": S[i+1][0]+= 1
            if S[i][1] !="#": S[i][1]+= 1
            if S[i-1][1] !="#": S[i-1][1]+= 1
            if S[i+1][1] !="#": S[i+1][1]+= 1

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

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

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

    for i in range(1,H-1):
        for j in range(1,W-1):   
            if S[i][j] == "#":
                if S[i-1][j-1] !="#": S[i-1][j-1]+= 1
                if S[i-1][j] !="#": S[i-1][j]+= 1
                if S[i-1][j+1] !="#": S[i-1][j+1]+= 1
                if S[i][j-1] !="#": S[i][j-1]+= 1
                if S[i][j+1] !="#": S[i][j+1]+= 1
                if S[i+1][j-1] !="#": S[i+1][j-1]+= 1
                if S[i+1][j] !="#": S[i+1][j]+= 1
                if S[i+1][j+1] !="#": S[i+1][j+1]+= 1
    for i in range(H):
        print("".join(map(str,S[i][:])))

Buggy submission (verdict: Runtime Error):
H,W = map(int,input().split(" "))
S = []
for i in range(H):
    S.append(list(input()))
for i in range(H):
    for j in range(W):
        if S[i][j] == ".":
            S[i][j] = 0
if H == 1 and W == 1:
    print(S[0][0])
else:
    #4隅の処理
    if S[0][0] == "#":
        if S[1][0] !="#": S[1][0]+= 1
        if S[0][1] !="#": S[0][1]+= 1
        if S[1][1] !="#": S[1][1]+= 1
    if S[0][W-1] == "#":
        if S[0][W-2] !="#": S[0][W-2]+= 1
        if S[1][W-2] !="#": S[1][W-2]+= 1
        if S[1][W-1] !="#": S[1][W-1]+= 1
    if S[H-1][0] == "#":
        if S[H-2][0] !="#": S[H-2][0]+= 1
        if S[H-1][1] !="#": S[H-1][1]+= 1
        if S[H-2][1] !="#": S[H-2][1]+= 1
    if S[H-1][W-1] == "#":
        if S[H-2][W-1] !="#": S[H-2][W-1]+= 1
        if S[H-2][W-2] !="#": S[H-2][W-2]+= 1
        if S[H-1][W-2] !="#": S[H-1][W-2]+= 1
    #端の処理
    for i in range(1,H-1):
        if S[i][0] == "#":
            if S[i-1][0] !="#": S[i-1][0]+= 1
            if S[i+1][0] !="#": S[i+1][0]+= 1
            if S[i][1] !="#": S[i][1]+= 1
            if S[i-1][1] !="#": S[i-1][1]+= 1
            if S[i+1][1] !="#": S[i+1][1]+= 1

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

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

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

    for i in range(1,H-1):
        for j in range(1,W-1):   
            if S[i][j] == "#":
                if S[i-1][j-1] !="#": S[i-1][j-1]+= 1
                if S[i-1][j] !="#": S[i-1][j]+= 1
                if S[i-1][j+1] !="#": S[i-1][j+1]+= 1
                if S[i][j-1] !="#": S[i][j-1]+= 1
                if S[i][j+1] !="#": S[i][j+1]+= 1
                if S[i+1][j-1] !="#": S[i+1][j-1]+= 1
                if S[i+1][j] !="#": S[i+1][j]+= 1
                if S[i+1][j+1] !="#": S[i+1][j+1]+= 1
    for i in range(H):
        print("".join(map(str,S[i][:])))

Buggy submission (verdict: Runtime Error):
H,W = map(int,input().split(" "))
S = []
for i in range(H):
    S.append(list(input()))
for i in range(H):
    for j in range(W):
        if S[i][j] == ".":
            S[i][j] = 0
#4隅の処理
if S[0][0] == "#":
    if S[1][0] !="#": S[1][0]+= 1
    if S[0][1] !="#": S[0][1]+= 1
    if S[1][1] !="#": S[1][1]+= 1
if S[0][W-1] == "#":
    if S[0][W-2] !="#": S[0][W-2]+= 1
    if S[1][W-2] !="#": S[1][W-2]+= 1
    if S[1][W-1] !="#": S[1][W-1]+= 1
if S[H-1][0] == "#":
    if S[H-2][0] !="#": S[H-2][0]+= 1
    if S[H-1][1] !="#": S[H-1][1]+= 1
    if S[H-2][1] !="#": S[H-2][1]+= 1
if S[H-1][W-1] == "#":
    if S[H-2][W-1] !="#": S[H-2][W-1]+= 1
    if S[H-2][W-2] !="#": S[H-2][W-2]+= 1
    if S[H-1][W-2] !="#": S[H-1][W-2]+= 1
#端の処理
for i in range(1,H-1):
    if S[i][0] == "#":
        if S[i-1][0] !="#": S[i-1][0]+= 1
        if S[i+1][0] !="#": S[i+1][0]+= 1
        if S[i][1] !="#": S[i][1]+= 1
        if S[i-1][1] !="#": S[i-1][1]+= 1
        if S[i+1][1] !="#": S[i+1][1]+= 1

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

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

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

for i in range(1,H-1):
    for j in range(1,W-1):   
        if S[i][j] == "#":
            if S[i-1][j-1] !="#": S[i-1][j-1]+= 1
            if S[i-1][j] !="#": S[i-1][j]+= 1
            if S[i-1][j+1] !="#": S[i-1][j+1]+= 1
            if S[i][j-1] !="#": S[i][j-1]+= 1
            if S[i][j+1] !="#": S[i][j+1]+= 1
            if S[i+1][j-1] !="#": S[i+1][j-1]+= 1
            if S[i+1][j] !="#": S[i+1][j]+= 1
            if S[i+1][j+1] !="#": S[i+1][j+1]+= 1
for i in range(H):
    print("".join(map(str,S[i][:])))