Problem p03574 (python)

Fixed submission:
h, w = map(int, input().split())
s = [0] * h
for i in range(h):
    s[i] = str(input())
t = [[0]*w]*h

if h == 1 and w == 1:
    if s[0][0] == '#':
        v = '#'
    else:
        v = 0
    t[0][0] = str(v)
    print(''.join(t[0]))
    
elif h ==1 and w !=1:
    for k in range(w):
        if k == 0:
            u = s[0][1]
        elif k == w-1:
            u = s[0][k-1]
        else:
            u = s[0][k-1] +s[0][k+1]
        v = u.count('#')
        if s[0][k] == '#':
            v = '#'
        t[0][k] = str(v)
    print(''.join(t[0]))

elif h !=1 and w == 1:
    for j in range(h):
        if j == 0:
            u = s[1][0]
        elif j == h-1:
            u = s[j-1][0]
        else:
            u = s[j-1][0] + s[j+1][0]
        v = u.count('#')
        if s[j][0] == '#':
            v = '#'
        t[j][0] = str(v)
        print(''.join(t[j]))

else:
    for j in range(h):
        if j == 0:
            for k in range(w):
                if k == 0:
                    u = s[0][1] + s[1][0] + s[1][1]
                elif k == w-1:
                    u = s[0][k-1] +s[1][k-1] + s[1][k]
                else:
                    u = s[0][k-1] +s[0][k+1] + s[1][k-1] + s[1][k] + s[1][k+1]
                v = u.count('#')
                if s[j][k] == '#':
                    v = '#'
                t[j][k] = str(v)
        elif j == h-1:
            for k in range(w):
                if k == 0:
                    u = s[j][1] + s[j-1][0] + s[j-1][1]
                elif k == w-1:
                    u = s[j][k-1] +s[j-1][k-1] + s[j-1][k]
                else:
                    u = s[j][k-1] +s[j][k+1] + s[j-1][k-1] + s[j-1][k] + s[j-1][k+1]
                v = u.count('#')
                if s[j][k] == '#':
                    v = '#'
                t[j][k] = str(v)
        else:
             for k in range(w):
                if k == 0:
                    u = s[j][1] + s[j-1][0] + s[j-1][1] + s[j+1][0] + s[j+1][1]
                elif k == w-1:
                    u = s[j][k-1] +s[j-1][k-1] + s[j-1][k] + s[j+1][k-1] + s[j+1][k]
                else:
                    u = s[j][k-1] +s[j][k+1] + s[j-1][k-1] + s[j-1][k] + s[j-1][k+1] + s[j+1][k-1] + s[j+1][k] + s[j+1][k+1]
                v = u.count('#')
                if s[j][k] == '#':
                    v = '#'
                t[j][k] = str(v)
        print(''.join(t[j]))

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