Problem p03574 (python)

Fixed submission:
h,w=map(int,input().split())
s=[list(input()) for _ in range(h)]
l=[]
l.append(['*']*(w+1))
for i in range(h):
    l1=s[i]
    l1.insert(0,'*')
    l1.append('*')
    l.append(l1)
l.append(['*']*(w+1))

for i in range(1,h+1):
    for j in range(1,w+1):
        c=0
        if l[i][j]=='.':
            c+=l[i-1][j-1:j+2].count('#')
            c+=l[i][j-1:j+2].count('#')
            c+=l[i+1][j-1:j+2].count('#')
            l[i].pop(j)
            l[i].insert(j,str(c))
            
for i in range(1,h+1):
    print(''.join(l[i][1:w+1]))

Buggy submission (verdict: Runtime Error):
h,w=map(int,input().split())
s=[input() for _ in range(h)]
l=[[] for _ in range(h)]
for i in range(h):
    for j in range(w):
        if i==0:
            if j==0 and s[0][0]=='.':
                l1=[s[0][1],s[1][0],s[1][1]]
                l1l=l1.count('#')
                l[i].append(str(l1l))
            elif j==w-1 and s[0][w-1]=='.':
                r1=[s[0][w-2],s[1][w-2],s[1][w-1]]
                r1r=r1.count('#')
                l[i].append(str(r1r))
            elif j!=0 and j!=w-1 and s[0][j]=='.':
                l1j=[s[0][j-1],s[0][j+1],s[1][j-1],s[1][j],s[1][j+1]]
                k=l1j.count('#')
                l[i].append(str(k))
            else:l[i].append('#')
        elif i==h-1:
            if j==0 and s[h-1][0]=='.':
                lh=[s[h-1][1],s[h-2][0],s[h-2][1]]
                lhl=lh.count('#')
                l[i].append(str(lhl))
            elif j==w-1 and s[h-1][w-1]=='.':
                rh=[s[h-1][w-2],s[h-2][w-2],s[h-2][w-1]]
                rhr=rh.count('#')
                l[i].append(str(rhr))
            elif j!=0 and j!=w-1 and s[h-1][j]=='.':
                lhj=[s[h-1][j-1],s[h-1][j+1],s[h-2][j-1],s[h-2][j],s[h-2][j+1]]
                lhjj=lhj.count('#')
                l[i].append(str(lhjj))
            else:l[i].append('#')
        else:
            if j==0 and s[i][0]=='.':
                li=[s[i-1][0],s[i-1][1],s[i][1],s[i+1][1],s[i+1][0]]
                lil=li.count('#')
                l[i].append(str(lil))
            elif j==w-1 and s[i][w-1]=='.':
                ri=[s[i-1][w-1],s[i-1][w-2],s[i][w-1],s[i+1][w-2],s[i+1][w-1]]
                rir=ri.count('#')
                l[i].append(str(rir))
            elif j!=0 and j!=w-1 and s[i][j]=='.':
                lij=[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]]
                lijj=lij.count('#')
                l[i].append(str(lijj))
            else:l[i].append('#')
for i in range(h):
    print("".join(l[i]))

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):
        if i==0:
            if j==0 and s[0][0]=='.':
                l1=[s[0][1],s[1][0],s[1][1]]
                l1l=l1.count('#')
                s[0][0]=l1l
            elif j==w-1 and s[0][w-1]=='.':
                r1=[s[0][w-2],s[1][w-2],s[1][w-1]]
                r1r=r1.count('#')
                s[0][w-1]=r1r
            elif j!=0 and j!=w-1 and s[0][j]=='.':
                l1j=[s[0][j-1],s[0][j+1],[s[1][j-1],s[1][j],s[1][j+1]]
                l1jj=l1j.count('#')
                s[0][j]==l1jj
            else:pass
        elif i==h-1:
            if j==0 and s[h-1][0]=='.':
                lh=[s[h-1][1],s[h-2][0],s[h-2][1]]
                lhl=lh.count('#')
                s[h-1][0]=lhl
            elif j==w-1 and s[h-1][w-1]=='.':
                rh=[s[h-1][w-2],s[h-2][w-2],s[h-2][w-1]]
                rhr=rh.count('#')
                s[h-1][w-1]=rhr
            elif j!=0 and j!=w-1 and s[h-1][j]=='.':
                lhj=[s[h-1][j-1],s[h-1][j+1],[s[h-2][j-1],s[h-2][j],s[h-2][j+1]]
                lhjj=lhj.count('#')
                s[0][j]==lhjj
            else:pass
        else:
            if j==0 and s[i][0]=='.':
                li=[s[i-1][0],s[i-1][1],s[i][1],s[i+1][1],s[i+1][0]]
                lil=li.count('#')
                s[i][0]=lil
            elif j==w-1 and s[i][w-1]=='.':
                ri=[s[i-1][w-1],s[i-1][w-2],s[i][w-1],s[i+1][w-2],s[i+1][w-1]]
                rir=ri.count('#')
                s[i][w-1]=rir
            elif j!=0 and j!=w-1 and s[i][j]=='.':
                lij=[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]]
                lijj=lij.count('#')
                s[i][j]==lijj
            else:pass
    print("".join(s[i]))

Buggy submission (verdict: Runtime Error):
h,w=map(int,input().split())
s=[list(input()) for _ in range(w)]
for i in range(h):
    for j in range(w):
        if i==0:
            if j==0 and s[0][0]=='.':
                l1=[s[0][1],s[1][0],s[1][1]]
                l1l=l1.count('#')
                s[0][0]=l1l
            elif j==w-1 and s[0][w-1]=='.':
                r1=[s[0][w-2],s[1][w-2],s[1][w-1]]
                r1r=r1.count('#')
                s[0][w-1]=r1r
            elif j!=0 and j!=w-1 and s[0][j]=='.':
                l1j=[s[0][j-1],s[0][j+1],[s[1][j-1],s[1][j],s[1][j+1]]
                l1jj=l1j.count('#')
                s[0][j]==l1jj
            else:pass
        elif i==h-1:
            if j==0 and s[h-1][0]=='.':
                lh=[s[h-1][1],s[h-2][0],s[h-2][1]]
                lhl=lh.count('#')
                s[h-1][0]=lhl
            elif j==w-1 and s[h-1][w-1]=='.':
                rh=[s[h-1][w-2],s[h-2][w-2],s[h-2][w-1]]
                rhr=rh.count('#')
                s[h-1][w-1]=rhr
            elif j!=0 and j!=w-1 and s[h-1][j]=='.':
                lhj=[s[h-1][j-1],s[h-1][j+1],[s[h-2][j-1],s[h-2][j],s[h-2][j+1]]
                lhjj=lhj.count('#')
                s[0][j]==lhjj
            else:pass
        else:
            if j==0 and s[i][0]=='.':
                li=[s[i-1][0],s[i-1][1],s[i][1],s[i+1][1],s[i+1][0]]
                lil=li.count('#')
                s[i][0]=lil
            elif j==w-1 and s[i][w-1]=='.':
                ri=[s[i-1][w-1],s[i-1][w-2],s[i][w-1],s[i+1][w-2],s[i+1][w-1]]
                rir=ri.count('#')
                s[i][w-1]=rir
            elif j!=0 and j!=w-1 and s[i][j]=='.':
                lij=[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]]
                lijj=lij.count('#')
                s[i][j]==lijj
            else:pass
    print("".join(s[i]))