Problem p03574 (python)

Fixed submission:
h, w = list(map(int, input().split()))
s = []
a = "."*(w+2)
s.append(a)
for i in range(h):
    p = input()
    x = "."+p+"."
    s.append(x)
s.append(a)


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

#print(s)

Buggy submission (verdict: Runtime Error):
import math
import itertools
import statistics
#n = int(input())
#n, d = list(input().split())
h, w = list(map(int, input().split()))
s = []
for i in range(h):
    s.append(input())

count = 0

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

Buggy submission (verdict: Runtime Error):
import math
import itertools
import statistics
#n = int(input())
#n, d = list(input().split())
h, w = list(map(int, input().split()))
s = []
for i in range(h):
    s.append(input())

count = 0

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