Problem p03574 (python)

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

if W ==1:
    if H ==2:
        if S[0]==".":
            A[0]=str(S[1].count("#"))
        if S[1]==".":
            A[1]=str(S[0].count("#"))
    else:
        for i in range(H):
            if S[i][0]==".":
                if i ==0:
                    A[0][0]=str(S[1][0].count("#"))
                elif i ==H-1:
                    A[H-1][0]=str(S[H-2][0].count("#")+S[H-1][0].count("#"))
                else:
                    A[i][0]=str(S[i-1][0].count("#")+S[i][0].count("#")+S[i+1][0].count("#"))
    for i in range(H):
        print("".join(A[i]))
    exit()

for i in range(W):
    for j in range(H):
        if S[j][i]==".":
            if i ==0 and j ==0:
                A[j][i] =str(S[0][0:2].count("#")+S[1][0:2].count("#"))
            elif i ==W-1 and j ==0:
                A[j][i] =str(S[0][W-2:W].count("#")+S[1][W-2:W].count("#"))
            elif i ==0 and j ==H-1:
                A[j][i] =str(S[H-2][0:2].count("#")+S[H-1][0:2].count("#"))
            elif i ==W-1 and j ==H-1:
                A[j][i] =str(S[H-2][W-2:W].count("#")+S[H-1][W-2:W].count("#"))
            elif i ==0:
                A[j][i] =str(S[j-1][0:2].count("#")+S[j][0:2].count("#")+S[j+1][0:2].count("#"))
            elif i ==W-1:
                A[j][i] =str(S[j-1][W-2:W].count("#")+S[j][W-2:W].count("#")+S[j+1][W-2:W].count("#"))
            elif j ==0:
                A[j][i] =str(S[0][i-1:i+2].count("#")+S[1][i-1:i+2].count("#"))
            elif j ==H-1:
                A[j][i] =str(S[H-2][i-1:i+2].count("#")+S[H-1][i-1:i+2].count("#"))
            else:
                A[j][i] =str(S[j-1][i-1:i+2].count("#")+S[j][i-1:i+2].count("#")+S[j+1][i-1:i+2].count("#"))
           
                
for i in range(H):
    print("".join(A[i]))

Buggy submission (verdict: Runtime Error):
H,W = map(int,input().split())
S = [input() for i in range(H)]
A = [["#"]*W for i in range(H)]

for i in range(W):
    for j in range(H):
        if S[j][i]==".":
            if i ==0 and j ==0:
                A[j][i] =str(S[0][0:2].count("#")+S[1][0:2].count("#"))
            elif i ==W-1 and j ==0:
                A[j][i] =str(S[0][W-2:W].count("#")+S[1][W-2:W].count("#"))
            elif i ==0 and j ==H-1:
                A[j][i] =str(S[H-2][0:2].count("#")+S[H-1][0:2].count("#"))
            elif i ==W-1 and j ==H-1:
                A[j][i] =str(S[H-2][W-2:W].count("#")+S[H-1][W-2:W].count("#"))
            elif i ==0:
                A[j][i] =str(S[j-1][0:2].count("#")+S[j][0:2].count("#")+S[j+1][0:2].count("#"))
            elif i ==W-1:
                A[j][i] =str(S[j-1][W-2:W].count("#")+S[j][W-2:W].count("#")+S[j+1][W-2:W].count("#"))
            elif j ==0:
                A[j][i] =str(S[0][i-1:i+2].count("#")+S[1][i-1:i+2].count("#"))
            elif j ==H-1:
                A[j][i] =str(S[H-2][i-1:i+2].count("#")+S[H-1][i-1:i+2].count("#"))
            else:
                A[j][i] =str(S[j-1][i-1:i+2].count("#")+S[j][i-1:i+2].count("#")+S[j+1][i-1:i+2].count("#"))
           
                
for i in range(H):
    print("".join(A[i]))