Problem p03574 (python)

Fixed submission:
h, w = map(int, input().split())
s = []
for i in range(h):
    s.append(list(input()))

# print(s)

dx = [0, 0, 1, 1, 1, -1, -1, -1]
dy = [1, -1, 0, -1, 1, 0, 1, -1]
t = []

for i in range(h):
    temp = []
    for j in range(w):
        count = 0
        if s[i][j] == '#':
            temp.append('#')
            continue
        for k in range(len(dx)):
            xx = j + dx[k]
            yy = i + dy[k]
            if (xx < 0) or (xx > w - 1) or (yy < 0) or (yy > h - 1):
                continue
            # print(xx, yy)
            if s[yy][xx] == '#':
                count += 1
        temp.append(str(count))
    t.append(temp)
for i in range(h):
    row = ''
    for x in t[i]:
        row += x
    print(row)

Buggy submission (verdict: Runtime Error):
h, w = map(int, input().split())
s = []
for i in range(h):
    s.append(list(input()))
t = []
for i in range(h):
    temp = []
    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 - 1] == '#':
                        count += 1
                    if s[i + 1][j] == '#':
                        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 - 1][j] == '#':
                        count += 1
                    if s[i - 1][j + 1] == '#':
                        count += 1
                    if s[i][j + 1] == '#':
                        count += 1

                elif j == w - 1:
                    if s[i - 1][j - 1] == '#':
                        count += 1
                    if s[i - 1][j] == '#':
                        count += 1
                    if s[i][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

            else:
                if j == 0:
                    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 + 1][j] == '#':
                        count += 1
                    if s[i + 1][j + 1] == '#':
                        count += 1

                elif j == w - 1:
                    if s[i - 1][j - 1] == '#':
                        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] == '#':
                        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

            temp.append(str(count))
        else:
            temp.append('#')
    t.append(temp)
for i in range(h):
    row = ''
    for x in t[i]:
        row += x
    print(row)