Buggy python submission (verdict: Runtime Error):
#!/usr/bin/env python3
# input
h, w = map(int, input().split())
s = [input() for _ in range(h)]

def sweep(i, j, s):
    global h; global w
    ans = 0
    # inner 
    if 0 < i < h-1:
        if 0 < j < w-1:
            sweeps = [s[i][j+1], s[i+1][j+1], s[i+1][j], s[i+1][j-1], s[i][j-1], s[i-1][j-1], s[i-1][j], s[i-1][j+1]]
            for sw in sweeps:
                if sw == '#':
                    ans += 1
    # boundary
    if i == 0:
        if j == 0:
            sweeps = [s[i][j+1], s[i+1][j+1], s[i+1][j]]
            for sw in sweeps:
                if sw == '#':
                    ans += 1
        elif j == w-1:
            sweeps = [s[i][j-1], s[i+1][j-1], s[i+1][j]]
            for sw in sweeps:
                if sw == '#':
                    ans += 1
        else:
            sweeps = [s[i][j-1], s[i+1][j-1], s[i+1][j], s[i+1][j+1], s[i][j+1]]
            for sw in sweeps:
                if sw == '#':
                    ans += 1
    elif i == h-1:
        if j == 0:
            sweeps = [s[i-1][j], s[i-1][j+1], s[i][j+1]]
            for sw in sweeps:
                if sw == '#':
                    ans += 1
        elif j == w-1:
            sweeps = [s[i-1][j], s[i-1][j-1], s[i][j-1]]
            for sw in sweeps:
                if sw == '#':
                    ans += 1
        else:
            sweeps = [s[i][j-1], s[i-1][j-1], s[i-1][j], s[i-1][j+1], s[i][j+1]]
            for sw in sweeps:
                if sw == '#':
                    ans += 1
    else:
        if j == 0:
            sweeps = [s[i-1][j], s[i-1][j+1], s[i][j+1], s[i+1][j+1], s[i+1][j]]
            for sw in sweeps:
                if sw == '#':
                    ans += 1
        elif j == w-1:
            sweeps = [s[i-1][j], s[i-1][j-1], s[i][j-1], s[i+1][j-1], s[i+1][j]]
            for sw in sweeps:
                if sw == '#':
                    ans += 1
    return ans

def sweep1line(j, s):
    global w
    ans = 0
    if j == 0:
        if s[0][j+1] == '#':
            ans += 1
    elif j == w-1:
        if s[0][j-1] == '#':
            ans += 1
    else:
        sweeps = [s[0][j-1], s[0][j+1]]
        for sw in sweeps:
            if sw == '#':
                ans += 1
    return ans

# calc
ans = [['' for _ in range(w)] for _ in range(h)]

if h != 1:
    for i in range(h):
        for j in range(w):
            if s[i][j] == '#':
                ans[i][j] = '#'
            if s[i][j] == '.':
                ans[i][j] = str(sweep(i, j, s))
else:
    for j in range(w):
        if s[0][j] == '#':
            ans[0][j] = '#'
        if s[0][j] == '.':
            ans[0][j] = str(sweep1line(j, s))

# output
for i in range(h):
    print(''.join(ans[i]))


Fixed submission:
#!/usr/bin/env python3
# input
h, w = map(int, input().split())
s = [input() for _ in range(h)]

def sweep(i, j, s):
    global h; global w
    ans = 0
    # inner 
    if 0 < i < h-1:
        if 0 < j < w-1:
            sweeps = [s[i][j+1], s[i+1][j+1], s[i+1][j], s[i+1][j-1], s[i][j-1], s[i-1][j-1], s[i-1][j], s[i-1][j+1]]
            for sw in sweeps:
                if sw == '#':
                    ans += 1
    # boundary
    if w > 1:
        if i == 0:
            if j == 0:
                sweeps = [s[i][j+1], s[i+1][j+1], s[i+1][j]]
                for sw in sweeps:
                    if sw == '#':
                        ans += 1
            elif j == w-1:
                sweeps = [s[i][j-1], s[i+1][j-1], s[i+1][j]]
                for sw in sweeps:
                    if sw == '#':
                        ans += 1
            else:
                sweeps = [s[i][j-1], s[i+1][j-1], s[i+1][j], s[i+1][j+1], s[i][j+1]]
                for sw in sweeps:
                    if sw == '#':
                        ans += 1
        elif i == h-1:
            if j == 0:
                sweeps = [s[i-1][j], s[i-1][j+1], s[i][j+1]]
                for sw in sweeps:
                    if sw == '#':
                        ans += 1
            elif j == w-1:
                sweeps = [s[i-1][j], s[i-1][j-1], s[i][j-1]]
                for sw in sweeps:
                    if sw == '#':
                        ans += 1
            else:
                sweeps = [s[i][j-1], s[i-1][j-1], s[i-1][j], s[i-1][j+1], s[i][j+1]]
                for sw in sweeps:
                    if sw == '#':
                        ans += 1
        else:
            if j == 0:
                sweeps = [s[i-1][j], s[i-1][j+1], s[i][j+1], s[i+1][j+1], s[i+1][j]]
                for sw in sweeps:
                    if sw == '#':
                        ans += 1
            elif j == w-1:
                sweeps = [s[i-1][j], s[i-1][j-1], s[i][j-1], s[i+1][j-1], s[i+1][j]]
                for sw in sweeps:
                    if sw == '#':
                        ans += 1

    # w == 1
    else:
        if i == 0:
            sweeps = [s[i+1][0]]
            for sw in sweeps:
                if sw == '#':
                    ans += 1
        elif i == h-1:
            sweeps = [s[i-1][0]]
            for sw in sweeps:
                if sw == '#':
                    ans += 1
        else:
            sweeps = [s[i-1][0], s[i+1][0]]
            for sw in sweeps:
                if sw == '#':
                    ans += 1

    return ans

def sweep1line(j, s):
    global w
    ans = 0
    if w > 1:
        if j == 0:
            if s[0][j+1] == '#':
                ans += 1
        elif j == w-1:
            if s[0][j-1] == '#':
                ans += 1
        else:
            sweeps = [s[0][j-1], s[0][j+1]]
            for sw in sweeps:
                if sw == '#':
                    ans += 1
    return ans

# calc
ans = [['' for _ in range(w)] for _ in range(h)]

if h != 1:
    for i in range(h):
        for j in range(w):
            if s[i][j] == '#':
                ans[i][j] = '#'
            if s[i][j] == '.':
                ans[i][j] = str(sweep(i, j, s))
else:
    for j in range(w):
        if s[0][j] == '#':
            ans[0][j] = '#'
        if s[0][j] == '.':
            ans[0][j] = str(sweep1line(j, s))

# output
for i in range(h):
    print(''.join(ans[i]))

Buggy python submission (verdict: Runtime Error):
#!/usr/bin/env python3
# input
h, w = map(int, input().split())
s = [input() for _ in range(h)]

def sweep(i, j, s):
    global h; global w
    ans = 0
    # inner
    if 0 < i < h-1:
        if 0 < j < w-1:
            sweeps = [s[i][j+1], s[i+1][j+1], s[i+1][j], s[i+1][j-1], s[i][j-1], s[i-1][j-1], s[i-1][j], s[i-1][j+1]]
            for s in sweeps:
                if s == '#':
                    ans += 1
    # boundary
    if i == 0:
        if j == 0:
            sweeps = [s[i][j+1], s[i+1][j+1], s[i+1][j]]
            for s in sweeps:
                if s == '#':
                    ans += 1
        elif j == w-1:
            sweeps = [s[i][j-1], s[i+1][j-1], s[i+1][j]]
            for s in sweeps:
                if s == '#':
                    ans += 1
        else:
            sweeps = [s[i][j-1], s[i+1][j-1], s[i+1][j], s[i+1][j+1], s[i][j+1]]
            for s in sweeps:
                if s == '#':
                    ans += 1
    elif i == h-1:
        if j == 0:
            sweeps = [s[i-1][j], s[i-1][j+1], s[i][j+1]]
            for s in sweeps:
                if s == '#':
                    ans += 1
        elif j == w-1:
            sweeps = [s[i-1][j], s[i-1][j-1], s[i][j-1]]
            for s in sweeps:
                if s == '#':
                    ans += 1
        else:
            sweeps = [s[i][j-1], s[i-1][j-1], s[i-1][j], s[i-1][j+1], s[i][j+1]]
            for s in sweeps:
                if s == '#':
                    ans += 1
    else:
        if j == 0:
            sweeps = [s[i-1][j], s[i-1][j+1], s[i][j+1], s[i+1][j+1], s[i+1][j]]
            for s in sweeps:
                if s == '#':
                    ans += 1
        elif j == w-1:
            sweeps = [s[i-1][j], s[i-1][j-1], s[i][j-1], s[i+1][j-1], s[i+1][j]]
            for s in sweeps:
                if s == '#':
                    ans += 1
    return ans

ans = [['' for _ in range(w)] for _ in range(h)]
for i in range(h):
    for j in range(w):
        if s[i][j] == '#':
            ans[i][j] = '#'
        if s[i][j] == '.':
            ans[i][j] = str(sweep(i, j, s))
# output
for i in range(h):
    print(''.join(ans[i]))


Fixed submission:
#!/usr/bin/env python3
# input
h, w = map(int, input().split())
s = [input() for _ in range(h)]

def sweep(i, j, s):
    global h; global w
    ans = 0
    # inner 
    if 0 < i < h-1:
        if 0 < j < w-1:
            sweeps = [s[i][j+1], s[i+1][j+1], s[i+1][j], s[i+1][j-1], s[i][j-1], s[i-1][j-1], s[i-1][j], s[i-1][j+1]]
            for sw in sweeps:
                if sw == '#':
                    ans += 1
    # boundary
    if w > 1:
        if i == 0:
            if j == 0:
                sweeps = [s[i][j+1], s[i+1][j+1], s[i+1][j]]
                for sw in sweeps:
                    if sw == '#':
                        ans += 1
            elif j == w-1:
                sweeps = [s[i][j-1], s[i+1][j-1], s[i+1][j]]
                for sw in sweeps:
                    if sw == '#':
                        ans += 1
            else:
                sweeps = [s[i][j-1], s[i+1][j-1], s[i+1][j], s[i+1][j+1], s[i][j+1]]
                for sw in sweeps:
                    if sw == '#':
                        ans += 1
        elif i == h-1:
            if j == 0:
                sweeps = [s[i-1][j], s[i-1][j+1], s[i][j+1]]
                for sw in sweeps:
                    if sw == '#':
                        ans += 1
            elif j == w-1:
                sweeps = [s[i-1][j], s[i-1][j-1], s[i][j-1]]
                for sw in sweeps:
                    if sw == '#':
                        ans += 1
            else:
                sweeps = [s[i][j-1], s[i-1][j-1], s[i-1][j], s[i-1][j+1], s[i][j+1]]
                for sw in sweeps:
                    if sw == '#':
                        ans += 1
        else:
            if j == 0:
                sweeps = [s[i-1][j], s[i-1][j+1], s[i][j+1], s[i+1][j+1], s[i+1][j]]
                for sw in sweeps:
                    if sw == '#':
                        ans += 1
            elif j == w-1:
                sweeps = [s[i-1][j], s[i-1][j-1], s[i][j-1], s[i+1][j-1], s[i+1][j]]
                for sw in sweeps:
                    if sw == '#':
                        ans += 1

    # w == 1
    else:
        if i == 0:
            sweeps = [s[i+1][0]]
            for sw in sweeps:
                if sw == '#':
                    ans += 1
        elif i == h-1:
            sweeps = [s[i-1][0]]
            for sw in sweeps:
                if sw == '#':
                    ans += 1
        else:
            sweeps = [s[i-1][0], s[i+1][0]]
            for sw in sweeps:
                if sw == '#':
                    ans += 1

    return ans

def sweep1line(j, s):
    global w
    ans = 0
    if w > 1:
        if j == 0:
            if s[0][j+1] == '#':
                ans += 1
        elif j == w-1:
            if s[0][j-1] == '#':
                ans += 1
        else:
            sweeps = [s[0][j-1], s[0][j+1]]
            for sw in sweeps:
                if sw == '#':
                    ans += 1
    return ans

# calc
ans = [['' for _ in range(w)] for _ in range(h)]

if h != 1:
    for i in range(h):
        for j in range(w):
            if s[i][j] == '#':
                ans[i][j] = '#'
            if s[i][j] == '.':
                ans[i][j] = str(sweep(i, j, s))
else:
    for j in range(w):
        if s[0][j] == '#':
            ans[0][j] = '#'
        if s[0][j] == '.':
            ans[0][j] = str(sweep1line(j, s))

# output
for i in range(h):
    print(''.join(ans[i]))