Buggy python submission (verdict: Runtime Error):
H, W = map(int, input().split())
S = list(str(input()) for i in range(H))
Answer = [["#"]*W for i in range(H)]

if S[0][0] == ".":
    count = 0
    if S[0][1] == "#":
        count += 1
    if S[1][0] == "#":
        count += 1
    if S[1][1] == "#":
        count += 1
    Answer[0][0] = count

if S[0][W-1] == ".":
    count = 0
    if S[0][W-2] == "#":
        count += 1
    if S[1][W-1] == "#":
        count += 1
    if S[1][W-2] == "#":
        count += 1
    Answer[0][W-1] = count

if S[H-1][0] == ".":
    count = 0
    if S[H-1][1] == "#":
        count += 1
    if S[H-2][0] == "#":
        count += 1
    if S[H-2][1] == "#":
        count += 1
    Answer[H-1][0] = count

if S[H-1][W-1] == ".":
    count = 0
    if S[H-1][W-2] == "#":
        count += 1
    if S[H-2][W-1] == "#":
        count += 1
    if S[H-2][W-2] == "#":
        count += 1
    Answer[H-1][W-1] = count

for i in range(1, W-1):
    count = 0
    if S[0][i] == ".":
        if S[0][i-1] == "#":
            count += 1
        if S[0][i+1] == "#":
            count += 1
        if S[1][i] == "#":
            count += 1
        if S[1][i-1] == "#":
            count += 1
        if S[1][i-1] == "#":
            count += 1
        Answer[0][i] = count

for i in range(1, W-1):
    count = 0
    if S[H-1][i] == ".":
        if S[H-1][i-1] == "#":
            count += 1
        if S[H-1][i+1] == "#":
            count += 1
        if S[H-2][i] == "#":
            count += 1
        if S[H-2][i-1] == "#":
            count += 1
        if S[H-2][i+1] == "#":
            count += 1
        Answer[H-1][i] = count

for i in range(1, H-1):
    count = 0
    if S[i][0] == ".":
        if S[i][1] == "#":
            count += 1
        if S[i-1][0] == "#":
            count += 1
        if S[i-1][1] == "#":
            count += 1
        if S[i+1][0] == "#":
            count += 1
        if S[i+1][1] == "#":
            count += 1
        Answer[i][0] = count

for i in range(1, H-1):
    count = 0
    if S[i][W-1] == ".":
        if S[i][W-2] == "#":
            count += 1
        if S[i-1][W-1] == "#":
            count += 1
        if S[i-1][W-2] == "#":
            count += 1
        if S[i+1][W-1] == "#":
            count += 1
        if S[i+1][W-2] == "#":
            count += 1
        Answer[i][W-1] = count

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
            Answer[i][j] = count

for rooms in Answer:
    room=[str(a) for a in rooms]
    room = "".join(room)
    print(room)


Fixed submission:
H, W = map(int, input().split())
S = list(str(input()) for i in range(H))
Answer = [["#"]*W for i in range(H)]

if H ==1 and W ==1:
    if S[0][0] == ".":
        Answer[0][0] = 0
    for rooms in Answer:
        room=[str(a) for a in rooms]
        room = "".join(room)
        print(room)
    exit()


elif H == 1:
    if S[0][0] == ".":
        if S[0][1] == "#":
            Answer[0][0] = 1
        else:
            Answer[0][0] = 0
    if S[0][W-1] == ".":
        if S[0][W-2] == "#":
            Answer[0][W-1] = 1
        else:
            Answer[0][W-1] = 0
    for i in range(1, W-1):
        count = 0
        if S[0][i] == ".":
            if S[0][i-1] == "#":
                count += 1
            if S[0][i+1] == "#":
                count += 1
            Answer[0][i] = count
    for rooms in Answer:
        room=[str(a) for a in rooms]
        room = "".join(room)
        print(room)
    exit()

elif W == 1:
    if S[0][0] == ".":
        if S[1][0] == "#":
            Answer[0][0] = 1
        else:
            Answer[0][0] = 0
    if S[H-1][0] == ".":
        if S[H-2][0] == "#":
            Answer[H-1][0] = 1
        else:
            Answer[H-1][0] = 0
    for i in range(1, H-1):
        count = 0
        if S[i][0] == ".":
            if S[i-1][0] == "#":
                count += 1
            if S[i+1][0] == "#":
                count += 1
            Answer[i][0] = count
    for rooms in Answer:
        room=[str(a) for a in rooms]
        room = "".join(room)
        print(room)
    exit()

if S[0][0] == ".":
    count = 0
    if S[0][1] == "#":
        count += 1
    if S[1][0] == "#":
        count += 1
    if S[1][1] == "#":
        count += 1
    Answer[0][0] = count

if S[0][W-1] == ".":
    count = 0
    if S[0][W-2] == "#":
        count += 1
    if S[1][W-1] == "#":
        count += 1
    if S[1][W-2] == "#":
        count += 1
    Answer[0][W-1] = count

if S[H-1][0] == ".":
    count = 0
    if S[H-1][1] == "#":
        count += 1
    if S[H-2][0] == "#":
        count += 1
    if S[H-2][1] == "#":
        count += 1
    Answer[H-1][0] = count

if S[H-1][W-1] == ".":
    count = 0
    if S[H-1][W-2] == "#":
        count += 1
    if S[H-2][W-1] == "#":
        count += 1
    if S[H-2][W-2] == "#":
        count += 1
    Answer[H-1][W-1] = count

for i in range(1, W-1):
    count = 0
    if S[0][i] == ".":
        if S[0][i-1] == "#":
            count += 1
        if S[0][i+1] == "#":
            count += 1
        if S[1][i] == "#":
            count += 1
        if S[1][i-1] == "#":
            count += 1
        if S[1][i+1] == "#":
            count += 1
        Answer[0][i] = count

for i in range(1, W-1):
    count = 0
    if S[H-1][i] == ".":
        if S[H-1][i-1] == "#":
            count += 1
        if S[H-1][i+1] == "#":
            count += 1
        if S[H-2][i] == "#":
            count += 1
        if S[H-2][i-1] == "#":
            count += 1
        if S[H-2][i+1] == "#":
            count += 1
        Answer[H-1][i] = count

for i in range(1, H-1):
    count = 0
    if S[i][0] == ".":
        if S[i][1] == "#":
            count += 1
        if S[i-1][0] == "#":
            count += 1
        if S[i-1][1] == "#":
            count += 1
        if S[i+1][0] == "#":
            count += 1
        if S[i+1][1] == "#":
            count += 1
        Answer[i][0] = count

for i in range(1, H-1):
    count = 0
    if S[i][W-1] == ".":
        if S[i][W-2] == "#":
            count += 1
        if S[i-1][W-1] == "#":
            count += 1
        if S[i-1][W-2] == "#":
            count += 1
        if S[i+1][W-1] == "#":
            count += 1
        if S[i+1][W-2] == "#":
            count += 1
        Answer[i][W-1] = count

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
            Answer[i][j] = count

for rooms in Answer:
    room=[str(a) for a in rooms]
    room = "".join(room)
    print(room)