Problem p03574 (python)

Fixed submission:
h, w = map(int, input().split())
s = [[i for i in input()] for _ in range(h)]
count = 0
if h==1 and w==1:
    if s[0][0]=='#':
        print('#')
    else:
        print(0)
    exit(0)
if h==1:
    for j in range(w):
        if s[0][j]=='.':
            if j == 0:
                if s[0][j+1]=='#':
                    count += 1
                s[0][j]=str(count)
                count=0
            elif j == w-1:
                if s[0][j-1]=='#':
                    count += 1
                s[0][j]=str(count)
                count=0
            else:
                if s[0][j+1]=='#':
                    count += 1
                if s[0][j-1]=='#':
                    count += 1
                s[0][j]=str(count)
                count=0
    print(''.join(s[0]))
    exit(0)
if w==1:
    for i in range(h):
        if s[i][0]=='.':
            if i == 0:
                if s[i+1][0]=='#':
                    count += 1
                s[i]=str(count)
                count=0
            elif i == h-1:
                if s[i-1][0]=='#':
                    count += 1
                s[i]=str(count)
                count=0
            else:
                if s[i+1][0]=='#':
                    count += 1
                if s[i-1][0]=='#':
                    count += 1
                s[i][0]=str(count)
                count=0
    for i in range(h):
        print(''.join(s[i][0]))
    exit(0)

for i in range(h):
    for j in range(w):
        if s[i][j] == '.':
            if i == 0:
                if j == 0:
                    if s[i][j+1]=='#':
                        count+=1
                    if s[i+1][j]=='#':
                       count+=1
                    if s[i+1][j+1]=='#':
                        count+=1
                    s[i][j]=str(count)
                    count = 0
                elif j == w-1:
                    if s[i][j-1]=='#':
                        count+=1
                    if s[i+1][j]=='#':
                        count+=1
                    if s[i+1][j-1]=='#':
                        count+=1
                    s[i][j]=str(count)
                    count = 0
                else:
                    if s[i][j-1]=='#':
                        count+=1
                    if s[i][j+1]=='#':
                        count+=1
                    for k in range(3):
                        if s[i+1][j-1+k]=='#':
                            count+=1
                    s[i][j]=str(count)
                    count = 0
            elif i == h-1:
                if j == 0:
                    if s[i][j+1]=='#':
                        count+=1
                    if s[i-1][j]=='#':
                        count+=1
                    if s[i-1][j+1]=='#':
                        count+=1
                    s[i][j]=str(count)
                    count = 0
                elif j == w-1:
                    if s[i][j-1]=='#':
                        count+=1
                    if s[i-1][j]=='#':
                        count+=1
                    if s[i-1][j-1]=='#':
                        count+=1
                    s[i][j]=str(count)
                    count = 0
                else:
                    if s[i][j-1]=='#':
                        count+=1
                    if s[i][j+1]=='#':
                        count+=1
                    for k in range(3):
                        if s[i-1][j-1+k]=='#':
                            count+=1
                    s[i][j]=str(count)
                    count = 0
            else:
                if j==0:
                     if s[i+1][j]=='#':
                         count+=1
                     if s[i-1][j]=='#':
                         count+=1
                     for k in range(3):
                         if s[i-1+k][j+1]=='#':
                             count+=1
                     s[i][j]=str(count)
                     count = 0
                elif j==w-1:
                    if s[i+1][j]=='#':
                        count+=1
                    if s[i-1][j]=='#':
                        count+=1
                    for k in range(3):
                        if s[i-1+k][j-1]=='#':
                            count+=1
                    s[i][j]=str(count)
                    count = 0
                else:
                    if s[i][j-1]=='#':
                        count+=1
                    if s[i][j+1]=='#':
                        count+=1
                    for k in range(3):
                        if s[i+1][j-1+k]=='#':
                            count+=1
                        if s[i-1][j-1+k]=='#':
                            count+=1

                    s[i][j]=str(count)
                    count = 0
for i in range(h):
    print(''.join(s[i]))

Buggy submission (verdict: Runtime Error):
h, w = map(int, input().split())
s = [[i for i in input()] for _ in range(h)]
count = 0
for i in range(h):
    for j in range(w):
        if s[i][j] == '.':
            if i == 0:
                if j == 0:
                    if s[i][j+1]=='#':
                        count+=1
                    if s[i+1][j]=='#':
                       count+=1
                    if s[i+1][j+1]=='#':
                        count+=1
                    s[i][j]=str(count)
                    count = 0
                elif j == w-1:
                    if s[i][j-1]=='#':
                        count+=1
                    if s[i+1][j]=='#':
                        count+=1
                    if s[i+1][j-1]=='#':
                        count+=1
                    s[i][j]=str(count)
                    count = 0
                else:
                    if s[i][j-1]=='#':
                        count+=1
                    if s[i][j+1]=='#':
                        count+=1
                    for k in range(3):
                        if s[i+1][j-1+k]=='#':
                            count+=1
                    s[i][j]=str(count)
                    count = 0
            elif i == h-1:
                if j == 0:
                    if s[i][j+1]=='#':
                        count+=1
                    if s[i-1][j]=='#':
                        count+=1
                    if s[i-1][j+1]=='#':
                        count+=1
                    s[i][j]=str(count)
                    count = 0
                elif j == w-1:
                    if s[i][j-1]=='#':
                        count+=1
                    if s[i-1][j]=='#':
                        count+=1
                    if s[i-1][j-1]=='#':
                        count+=1
                    s[i][j]=str(count)
                    count = 0
                else:
                    if s[i][j-1]=='#':
                        count+=1
                    if s[i][j+1]=='#':
                        count+=1
                    for k in range(3):
                        if s[i-1][j-1+k]=='#':
                            count+=1
                    s[i][j]=str(count)
                    count = 0
            else:
                if j==0:
                     if s[i+1][j]=='#':
                         count+=1
                     if s[i-1][j]=='#':
                         count+=1
                     for k in range(3):
                         if s[i-1+k][j+1]=='#':
                             count+=1
                     s[i][j]=str(count)
                     count = 0
                elif j==w-1:
                    if s[i+1][j]=='#':
                        count+=1
                    if s[i-1][j]=='#':
                        count+=1
                    for k in range(3):
                        if s[i-1+k][j-1]=='#':
                            count+=1
                    s[i][j]=str(count)
                    count = 0
                else:
                    if s[i][j-1]=='#':
                        count+=1
                    if s[i][j+1]=='#':
                        count+=1
                    for k in range(3):
                        if s[i+1][j-1+k]=='#':
                            count+=1
                        if s[i-1][j-1+k]=='#':
                            count+=1

                    s[i][j]=str(count)
                    count = 0
for i in range(h):
    print(''.join(s[i]))