Problem p03574 (python)

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

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

for i in range(h):
    print(''.join(s[i]))

Buggy submission (verdict: Runtime Error):
h,w = map(int,input().split())
s = [list(input()) for i in range(h)]

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

for i in range(h):
    print(''.join(s[i]))