Buggy python submission (verdict: Runtime Error):
#ABC 075 B - Minesweeper
H, W = input().split()
H = int(H)
W = int(W)
S = [input() for i in range(H)]

if H == 1:
    for w in range(1,W-1):
        s = S[0][w]
        if s == '.':
            count = 0
            if S[0][w-1] == '#': count+=1
            if S[0][w+1] == '#': count+=1
            S[0] = S[0][:w] + str(count) + S[0][w+1:]
    w = 0
    h = 0
    s = S[h][w]
    if s == '.':
            count = 0
            if S[h][w+1] == '#': count+=1
            S[h] = str(count) + S[h][w+1:]
    w = W-1
    h = 0
    s = S[h][w]
    if s == '.':
            count = 0
            if S[h][w-1] == '#': count+=1
            S[h] = S[h][:w] + str(count)
    for h in range(H):
        print(S[h])
    exit()
    
if W == 1:
    for h in range(1,H-1):
        w = 0
        s = S[h][w]
        if s == '.':
            count = 0
            if S[h-1][w] == '#': count+=1
            if S[h+1][w] == '#': count+=1
            S[h] = str(count)
    w = 0
    h = 0
    s = S[h][w]
    if s == '.':
            count = 0
            if S[h+1][w] == '#': count+=1
            S[h] = str(count)
    w = W-1
    h = 0
    s = S[h][w]
    if s == '.':
            count = 0
            if S[h-1][w] == '#': count+=1
            S[h] = str(count)
    for h in range(H):
        print(S[h])
    exit()

    


for w in range(1,W-1):
    s = S[0][w]
    if s == '.':
        count = 0
        if S[0][w-1] == '#': count+=1
        if S[0][w+1] == '#': count+=1
        if S[1][w-1] == '#': count+=1
        if S[1][w] == '#': count+=1
        if S[1][w+1] == '#': count+=1
        S[0] = S[0][:w] + str(count) + S[0][w+1:]

for w in range(1,W-1):
    s = S[H-1][w]
    if s == '.':
        count = 0
        if S[H-1][w-1] == '#': count+=1
        if S[H-1][w+1] == '#': count+=1
        if S[H-2][w-1] == '#': count+=1
        if S[H-2][w] == '#': count+=1
        if S[H-2][w+1] == '#': count+=1
        S[H-1] = S[H-1][:w] + str(count) + S[H-1][w+1:]
        
for h in range(1,H-1):
    for w in range(1,W-1):
        s = S[h][w]
        if s == '.':
            count = 0
            if S[h][w-1] == '#': count+=1
            if S[h][w+1] == '#': count+=1
            if S[h-1][w-1] == '#': count+=1
            if S[h-1][w] == '#': count+=1
            if S[h-1][w+1] == '#': count+=1
            if S[h+1][w-1] == '#': count+=1
            if S[h+1][w] == '#': count+=1
            if S[h+1][w+1] == '#': count+=1
            S[h] = S[h][:w] + str(count) + S[h][w+1:]

for h in range(1,H-1):
    w = 0
    s = S[h][w]
    if s == '.':
        count = 0
        #if S[h][w-1] == '#': count+=1
        if S[h][w+1] == '#': count+=1
        #if S[h-1][w-1] == '#': count+=1
        if S[h-1][w] == '#': count+=1
        if S[h-1][w+1] == '#': count+=1
        #if S[h+1][w-1] == '#': count+=1
        if S[h+1][w] == '#': count+=1
        if S[h+1][w+1] == '#': count+=1
        S[h] = str(count) + S[h][w+1:]

for h in range(1,H-1):
    w = W-1
    s = S[h][w]
    if s == '.':
        count = 0
        if S[h][w-1] == '#': count+=1
        #if S[h][w+1] == '#': count+=1
        if S[h-1][w-1] == '#': count+=1
        if S[h-1][w] == '#': count+=1
        #if S[h-1][w+1] == '#': count+=1
        if S[h+1][w-1] == '#': count+=1
        if S[h+1][w] == '#': count+=1
        #if S[h+1][w+1] == '#': count+=1
        S[h] = S[h][:w] + str(count)

w = 0
h = 0
s = S[h][w]
if s == '.':
        count = 0
        #if S[h][w-1] == '#': count+=1
        if S[h][w+1] == '#': count+=1
        #if S[h-1][w-1] == '#': count+=1
        #if S[h-1][w] == '#': count+=1
        #if S[h-1][w+1] == '#': count+=1
        #if S[h+1][w-1] == '#': count+=1
        if S[h+1][w] == '#': count+=1
        if S[h+1][w+1] == '#': count+=1
        S[h] = str(count) + S[h][w+1:]
        
w = 0
h = H-1
s = S[h][w]
if s == '.':
        count = 0
        #if S[h][w-1] == '#': count+=1
        if S[h][w+1] == '#': count+=1
        #if S[h-1][w-1] == '#': count+=1
        if S[h-1][w] == '#': count+=1
        if S[h-1][w+1] == '#': count+=1
        #if S[h+1][w-1] == '#': count+=1
        #if S[h+1][w] == '#': count+=1
        #if S[h+1][w+1] == '#': count+=1
        S[h] = str(count) + S[h][w+1:]

w = W-1
h = 0
s = S[h][w]
if s == '.':
        count = 0
        if S[h][w-1] == '#': count+=1
        #if S[h][w+1] == '#': count+=1
        #if S[h-1][w-1] == '#': count+=1
        #if S[h-1][w] == '#': count+=1
        #if S[h-1][w+1] == '#': count+=1
        if S[h+1][w-1] == '#': count+=1
        if S[h+1][w] == '#': count+=1
        #if S[h+1][w+1] == '#': count+=1
        S[h] = S[h][:w] + str(count)

w = W-1
h = H-1
s = S[h][w]
if s == '.':
        count = 0
        if S[h][w-1] == '#': count+=1
        #if S[h][w+1] == '#': count+=1
        if S[h-1][w-1] == '#': count+=1
        if S[h-1][w] == '#': count+=1
        #if S[h-1][w+1] == '#': count+=1
        #if S[h+1][w-1] == '#': count+=1
        #if S[h+1][w] == '#': count+=1
        #if S[h+1][w+1] == '#': count+=1
        S[h] = S[h][:w] + str(count)
        

        

for h in range(H):
    print(S[h])

Fixed submission:
#ABC 075 B - Minesweeper
H, W = input().split()
H = int(H)
W = int(W)
S = [input() for i in range(H)]

if H == 1:
    if W == 1:
        if S[0][0] == '.':
            print('0')
        if S[0][0] == '#':
            print('#')
        exit()
    for w in range(1,W-1):
        s = S[0][w]
        if s == '.':
            count = 0
            if S[0][w-1] == '#': count+=1
            if S[0][w+1] == '#': count+=1
            S[0] = S[0][:w] + str(count) + S[0][w+1:]
    w = 0
    h = 0
    s = S[h][w]
    if s == '.':
            count = 0
            if S[h][w+1] == '#': count+=1
            S[h] = str(count) + S[h][w+1:]
    w = W-1
    h = 0
    s = S[h][w]
    if s == '.':
            count = 0
            if S[h][w-1] == '#': count+=1
            S[h] = S[h][:w] + str(count)
    for h in range(H):
        print(S[h])
    exit()
    
if W == 1:
    for h in range(1,H-1):
        w = 0
        s = S[h][w]
        if s == '.':
            count = 0
            if S[h-1][w] == '#': count+=1
            if S[h+1][w] == '#': count+=1
            S[h] = str(count)
    w = 0
    h = 0
    s = S[h][w]
    if s == '.':
            count = 0
            if S[h+1][w] == '#': count+=1
            S[h] = str(count)
    w = W-1
    h = 0
    s = S[h][w]
    if s == '.':
            count = 0
            if S[h-1][w] == '#': count+=1
            S[h] = str(count)
    for h in range(H):
        print(S[h])
    exit()

    


for w in range(1,W-1):
    s = S[0][w]
    if s == '.':
        count = 0
        if S[0][w-1] == '#': count+=1
        if S[0][w+1] == '#': count+=1
        if S[1][w-1] == '#': count+=1
        if S[1][w] == '#': count+=1
        if S[1][w+1] == '#': count+=1
        S[0] = S[0][:w] + str(count) + S[0][w+1:]

for w in range(1,W-1):
    s = S[H-1][w]
    if s == '.':
        count = 0
        if S[H-1][w-1] == '#': count+=1
        if S[H-1][w+1] == '#': count+=1
        if S[H-2][w-1] == '#': count+=1
        if S[H-2][w] == '#': count+=1
        if S[H-2][w+1] == '#': count+=1
        S[H-1] = S[H-1][:w] + str(count) + S[H-1][w+1:]
        
for h in range(1,H-1):
    for w in range(1,W-1):
        s = S[h][w]
        if s == '.':
            count = 0
            if S[h][w-1] == '#': count+=1
            if S[h][w+1] == '#': count+=1
            if S[h-1][w-1] == '#': count+=1
            if S[h-1][w] == '#': count+=1
            if S[h-1][w+1] == '#': count+=1
            if S[h+1][w-1] == '#': count+=1
            if S[h+1][w] == '#': count+=1
            if S[h+1][w+1] == '#': count+=1
            S[h] = S[h][:w] + str(count) + S[h][w+1:]

for h in range(1,H-1):
    w = 0
    s = S[h][w]
    if s == '.':
        count = 0
        #if S[h][w-1] == '#': count+=1
        if S[h][w+1] == '#': count+=1
        #if S[h-1][w-1] == '#': count+=1
        if S[h-1][w] == '#': count+=1
        if S[h-1][w+1] == '#': count+=1
        #if S[h+1][w-1] == '#': count+=1
        if S[h+1][w] == '#': count+=1
        if S[h+1][w+1] == '#': count+=1
        S[h] = str(count) + S[h][w+1:]

for h in range(1,H-1):
    w = W-1
    s = S[h][w]
    if s == '.':
        count = 0
        if S[h][w-1] == '#': count+=1
        #if S[h][w+1] == '#': count+=1
        if S[h-1][w-1] == '#': count+=1
        if S[h-1][w] == '#': count+=1
        #if S[h-1][w+1] == '#': count+=1
        if S[h+1][w-1] == '#': count+=1
        if S[h+1][w] == '#': count+=1
        #if S[h+1][w+1] == '#': count+=1
        S[h] = S[h][:w] + str(count)

w = 0
h = 0
s = S[h][w]
if s == '.':
        count = 0
        #if S[h][w-1] == '#': count+=1
        if S[h][w+1] == '#': count+=1
        #if S[h-1][w-1] == '#': count+=1
        #if S[h-1][w] == '#': count+=1
        #if S[h-1][w+1] == '#': count+=1
        #if S[h+1][w-1] == '#': count+=1
        if S[h+1][w] == '#': count+=1
        if S[h+1][w+1] == '#': count+=1
        S[h] = str(count) + S[h][w+1:]
        
w = 0
h = H-1
s = S[h][w]
if s == '.':
        count = 0
        #if S[h][w-1] == '#': count+=1
        if S[h][w+1] == '#': count+=1
        #if S[h-1][w-1] == '#': count+=1
        if S[h-1][w] == '#': count+=1
        if S[h-1][w+1] == '#': count+=1
        #if S[h+1][w-1] == '#': count+=1
        #if S[h+1][w] == '#': count+=1
        #if S[h+1][w+1] == '#': count+=1
        S[h] = str(count) + S[h][w+1:]

w = W-1
h = 0
s = S[h][w]
if s == '.':
        count = 0
        if S[h][w-1] == '#': count+=1
        #if S[h][w+1] == '#': count+=1
        #if S[h-1][w-1] == '#': count+=1
        #if S[h-1][w] == '#': count+=1
        #if S[h-1][w+1] == '#': count+=1
        if S[h+1][w-1] == '#': count+=1
        if S[h+1][w] == '#': count+=1
        #if S[h+1][w+1] == '#': count+=1
        S[h] = S[h][:w] + str(count)

w = W-1
h = H-1
s = S[h][w]
if s == '.':
        count = 0
        if S[h][w-1] == '#': count+=1
        #if S[h][w+1] == '#': count+=1
        if S[h-1][w-1] == '#': count+=1
        if S[h-1][w] == '#': count+=1
        #if S[h-1][w+1] == '#': count+=1
        #if S[h+1][w-1] == '#': count+=1
        #if S[h+1][w] == '#': count+=1
        #if S[h+1][w+1] == '#': count+=1
        S[h] = S[h][:w] + str(count)
        

        

for h in range(H):
    print(S[h])

Buggy python submission (verdict: Runtime Error):
#ABC 075 B - Minesweeper
H, W = input().split()
H = int(H)
W = int(W)
S = [input() for i in range(H)]

if H == 1:
    for w in range(1,W-1):
        s = S[0][w]
        if s == '.':
            count = 0
            if S[0][w-1] == '#': count+=1
            if S[0][w+1] == '#': count+=1
            S[0] = S[0][:w] + str(count) + S[0][w+1:]
    w = 0
    h = 0
    s = S[h][w]
    if s == '.':
            count = 0
            if S[h][w+1] == '#': count+=1
            S[h] = str(count) + S[h][w+1:]
    w = W-1
    h = 0
    s = S[h][w]
    if s == '.':
            count = 0
            if S[h][w-1] == '#': count+=1
            S[h] = S[h][:w] + str(count)
    for h in range(H):
        print(S[h])
    exit()

    


for w in range(1,W-1):
    s = S[0][w]
    if s == '.':
        count = 0
        if S[0][w-1] == '#': count+=1
        if S[0][w+1] == '#': count+=1
        if S[1][w-1] == '#': count+=1
        if S[1][w] == '#': count+=1
        if S[1][w+1] == '#': count+=1
        S[0] = S[0][:w] + str(count) + S[0][w+1:]

for w in range(1,W-1):
    s = S[H-1][w]
    if s == '.':
        count = 0
        if S[H-1][w-1] == '#': count+=1
        if S[H-1][w+1] == '#': count+=1
        if S[H-2][w-1] == '#': count+=1
        if S[H-2][w] == '#': count+=1
        if S[H-2][w+1] == '#': count+=1
        S[H-1] = S[H-1][:w] + str(count) + S[H-1][w+1:]
        
for h in range(1,H-1):
    for w in range(1,W-1):
        s = S[h][w]
        if s == '.':
            count = 0
            if S[h][w-1] == '#': count+=1
            if S[h][w+1] == '#': count+=1
            if S[h-1][w-1] == '#': count+=1
            if S[h-1][w] == '#': count+=1
            if S[h-1][w+1] == '#': count+=1
            if S[h+1][w-1] == '#': count+=1
            if S[h+1][w] == '#': count+=1
            if S[h+1][w+1] == '#': count+=1
            S[h] = S[h][:w] + str(count) + S[h][w+1:]

for h in range(1,H-1):
    w = 0
    s = S[h][w]
    if s == '.':
        count = 0
        #if S[h][w-1] == '#': count+=1
        if S[h][w+1] == '#': count+=1
        #if S[h-1][w-1] == '#': count+=1
        if S[h-1][w] == '#': count+=1
        if S[h-1][w+1] == '#': count+=1
        #if S[h+1][w-1] == '#': count+=1
        if S[h+1][w] == '#': count+=1
        if S[h+1][w+1] == '#': count+=1
        S[h] = str(count) + S[h][w+1:]

for h in range(1,H-1):
    w = W-1
    s = S[h][w]
    if s == '.':
        count = 0
        if S[h][w-1] == '#': count+=1
        #if S[h][w+1] == '#': count+=1
        if S[h-1][w-1] == '#': count+=1
        if S[h-1][w] == '#': count+=1
        #if S[h-1][w+1] == '#': count+=1
        if S[h+1][w-1] == '#': count+=1
        if S[h+1][w] == '#': count+=1
        #if S[h+1][w+1] == '#': count+=1
        S[h] = S[h][:w] + str(count)

w = 0
h = 0
s = S[h][w]
if s == '.':
        count = 0
        #if S[h][w-1] == '#': count+=1
        if S[h][w+1] == '#': count+=1
        #if S[h-1][w-1] == '#': count+=1
        #if S[h-1][w] == '#': count+=1
        #if S[h-1][w+1] == '#': count+=1
        #if S[h+1][w-1] == '#': count+=1
        if S[h+1][w] == '#': count+=1
        if S[h+1][w+1] == '#': count+=1
        S[h] = str(count) + S[h][w+1:]
        
w = 0
h = H-1
s = S[h][w]
if s == '.':
        count = 0
        #if S[h][w-1] == '#': count+=1
        if S[h][w+1] == '#': count+=1
        #if S[h-1][w-1] == '#': count+=1
        if S[h-1][w] == '#': count+=1
        if S[h-1][w+1] == '#': count+=1
        #if S[h+1][w-1] == '#': count+=1
        #if S[h+1][w] == '#': count+=1
        #if S[h+1][w+1] == '#': count+=1
        S[h] = str(count) + S[h][w+1:]

w = W-1
h = 0
s = S[h][w]
if s == '.':
        count = 0
        if S[h][w-1] == '#': count+=1
        #if S[h][w+1] == '#': count+=1
        #if S[h-1][w-1] == '#': count+=1
        #if S[h-1][w] == '#': count+=1
        #if S[h-1][w+1] == '#': count+=1
        if S[h+1][w-1] == '#': count+=1
        if S[h+1][w] == '#': count+=1
        #if S[h+1][w+1] == '#': count+=1
        S[h] = S[h][:w] + str(count)

w = W-1
h = H-1
s = S[h][w]
if s == '.':
        count = 0
        if S[h][w-1] == '#': count+=1
        #if S[h][w+1] == '#': count+=1
        if S[h-1][w-1] == '#': count+=1
        if S[h-1][w] == '#': count+=1
        #if S[h-1][w+1] == '#': count+=1
        #if S[h+1][w-1] == '#': count+=1
        #if S[h+1][w] == '#': count+=1
        #if S[h+1][w+1] == '#': count+=1
        S[h] = S[h][:w] + str(count)
        

        

for h in range(H):
    print(S[h])


Fixed submission:
#ABC 075 B - Minesweeper
H, W = input().split()
H = int(H)
W = int(W)
S = [input() for i in range(H)]

if H == 1:
    if W == 1:
        if S[0][0] == '.':
            print('0')
        if S[0][0] == '#':
            print('#')
        exit()
    for w in range(1,W-1):
        s = S[0][w]
        if s == '.':
            count = 0
            if S[0][w-1] == '#': count+=1
            if S[0][w+1] == '#': count+=1
            S[0] = S[0][:w] + str(count) + S[0][w+1:]
    w = 0
    h = 0
    s = S[h][w]
    if s == '.':
            count = 0
            if S[h][w+1] == '#': count+=1
            S[h] = str(count) + S[h][w+1:]
    w = W-1
    h = 0
    s = S[h][w]
    if s == '.':
            count = 0
            if S[h][w-1] == '#': count+=1
            S[h] = S[h][:w] + str(count)
    for h in range(H):
        print(S[h])
    exit()
    
if W == 1:
    for h in range(1,H-1):
        w = 0
        s = S[h][w]
        if s == '.':
            count = 0
            if S[h-1][w] == '#': count+=1
            if S[h+1][w] == '#': count+=1
            S[h] = str(count)
    w = 0
    h = 0
    s = S[h][w]
    if s == '.':
            count = 0
            if S[h+1][w] == '#': count+=1
            S[h] = str(count)
    w = W-1
    h = 0
    s = S[h][w]
    if s == '.':
            count = 0
            if S[h-1][w] == '#': count+=1
            S[h] = str(count)
    for h in range(H):
        print(S[h])
    exit()

    


for w in range(1,W-1):
    s = S[0][w]
    if s == '.':
        count = 0
        if S[0][w-1] == '#': count+=1
        if S[0][w+1] == '#': count+=1
        if S[1][w-1] == '#': count+=1
        if S[1][w] == '#': count+=1
        if S[1][w+1] == '#': count+=1
        S[0] = S[0][:w] + str(count) + S[0][w+1:]

for w in range(1,W-1):
    s = S[H-1][w]
    if s == '.':
        count = 0
        if S[H-1][w-1] == '#': count+=1
        if S[H-1][w+1] == '#': count+=1
        if S[H-2][w-1] == '#': count+=1
        if S[H-2][w] == '#': count+=1
        if S[H-2][w+1] == '#': count+=1
        S[H-1] = S[H-1][:w] + str(count) + S[H-1][w+1:]
        
for h in range(1,H-1):
    for w in range(1,W-1):
        s = S[h][w]
        if s == '.':
            count = 0
            if S[h][w-1] == '#': count+=1
            if S[h][w+1] == '#': count+=1
            if S[h-1][w-1] == '#': count+=1
            if S[h-1][w] == '#': count+=1
            if S[h-1][w+1] == '#': count+=1
            if S[h+1][w-1] == '#': count+=1
            if S[h+1][w] == '#': count+=1
            if S[h+1][w+1] == '#': count+=1
            S[h] = S[h][:w] + str(count) + S[h][w+1:]

for h in range(1,H-1):
    w = 0
    s = S[h][w]
    if s == '.':
        count = 0
        #if S[h][w-1] == '#': count+=1
        if S[h][w+1] == '#': count+=1
        #if S[h-1][w-1] == '#': count+=1
        if S[h-1][w] == '#': count+=1
        if S[h-1][w+1] == '#': count+=1
        #if S[h+1][w-1] == '#': count+=1
        if S[h+1][w] == '#': count+=1
        if S[h+1][w+1] == '#': count+=1
        S[h] = str(count) + S[h][w+1:]

for h in range(1,H-1):
    w = W-1
    s = S[h][w]
    if s == '.':
        count = 0
        if S[h][w-1] == '#': count+=1
        #if S[h][w+1] == '#': count+=1
        if S[h-1][w-1] == '#': count+=1
        if S[h-1][w] == '#': count+=1
        #if S[h-1][w+1] == '#': count+=1
        if S[h+1][w-1] == '#': count+=1
        if S[h+1][w] == '#': count+=1
        #if S[h+1][w+1] == '#': count+=1
        S[h] = S[h][:w] + str(count)

w = 0
h = 0
s = S[h][w]
if s == '.':
        count = 0
        #if S[h][w-1] == '#': count+=1
        if S[h][w+1] == '#': count+=1
        #if S[h-1][w-1] == '#': count+=1
        #if S[h-1][w] == '#': count+=1
        #if S[h-1][w+1] == '#': count+=1
        #if S[h+1][w-1] == '#': count+=1
        if S[h+1][w] == '#': count+=1
        if S[h+1][w+1] == '#': count+=1
        S[h] = str(count) + S[h][w+1:]
        
w = 0
h = H-1
s = S[h][w]
if s == '.':
        count = 0
        #if S[h][w-1] == '#': count+=1
        if S[h][w+1] == '#': count+=1
        #if S[h-1][w-1] == '#': count+=1
        if S[h-1][w] == '#': count+=1
        if S[h-1][w+1] == '#': count+=1
        #if S[h+1][w-1] == '#': count+=1
        #if S[h+1][w] == '#': count+=1
        #if S[h+1][w+1] == '#': count+=1
        S[h] = str(count) + S[h][w+1:]

w = W-1
h = 0
s = S[h][w]
if s == '.':
        count = 0
        if S[h][w-1] == '#': count+=1
        #if S[h][w+1] == '#': count+=1
        #if S[h-1][w-1] == '#': count+=1
        #if S[h-1][w] == '#': count+=1
        #if S[h-1][w+1] == '#': count+=1
        if S[h+1][w-1] == '#': count+=1
        if S[h+1][w] == '#': count+=1
        #if S[h+1][w+1] == '#': count+=1
        S[h] = S[h][:w] + str(count)

w = W-1
h = H-1
s = S[h][w]
if s == '.':
        count = 0
        if S[h][w-1] == '#': count+=1
        #if S[h][w+1] == '#': count+=1
        if S[h-1][w-1] == '#': count+=1
        if S[h-1][w] == '#': count+=1
        #if S[h-1][w+1] == '#': count+=1
        #if S[h+1][w-1] == '#': count+=1
        #if S[h+1][w] == '#': count+=1
        #if S[h+1][w+1] == '#': count+=1
        S[h] = S[h][:w] + str(count)
        

        

for h in range(H):
    print(S[h])

Buggy python submission (verdict: Runtime Error):
#ABC 075 B - Minesweeper
H, W = input().split()
H = int(H)
W = int(W)
S = [input() for i in range(H)]


for w in range(1,W-1):
    s = S[0][w]
    if s == '.':
        count = 0
        if S[0][w-1] == '#': count+=1
        if S[0][w+1] == '#': count+=1
        if S[1][w-1] == '#': count+=1
        if S[1][w] == '#': count+=1
        if S[1][w+1] == '#': count+=1
        S[0] = S[0][:w] + str(count) + S[0][w+1:]

for w in range(1,W-1):
    s = S[H-1][w]
    if s == '.':
        count = 0
        if S[H-1][w-1] == '#': count+=1
        if S[H-1][w+1] == '#': count+=1
        if S[H-2][w-1] == '#': count+=1
        if S[H-2][w] == '#': count+=1
        if S[H-2][w+1] == '#': count+=1
        S[H-1] = S[H-1][:w] + str(count) + S[H-1][w+1:]
        
for h in range(1,H-1):
    for w in range(1,W-1):
        s = S[h][w]
        if s == '.':
            count = 0
            if S[h][w-1] == '#': count+=1
            if S[h][w+1] == '#': count+=1
            if S[h-1][w-1] == '#': count+=1
            if S[h-1][w] == '#': count+=1
            if S[h-1][w+1] == '#': count+=1
            if S[h+1][w-1] == '#': count+=1
            if S[h+1][w] == '#': count+=1
            if S[h+1][w+1] == '#': count+=1
            S[h] = S[h][:w] + str(count) + S[h][w+1:]

for h in range(1,H-1):
    w = 0
    s = S[h][w]
    if s == '.':
        count = 0
        #if S[h][w-1] == '#': count+=1
        if S[h][w+1] == '#': count+=1
        #if S[h-1][w-1] == '#': count+=1
        if S[h-1][w] == '#': count+=1
        if S[h-1][w+1] == '#': count+=1
        #if S[h+1][w-1] == '#': count+=1
        if S[h+1][w] == '#': count+=1
        if S[h+1][w+1] == '#': count+=1
        S[h] = str(count) + S[h][w+1:]

for h in range(1,H-1):
    w = W-1
    s = S[h][w]
    if s == '.':
        count = 0
        if S[h][w-1] == '#': count+=1
        #if S[h][w+1] == '#': count+=1
        if S[h-1][w-1] == '#': count+=1
        if S[h-1][w] == '#': count+=1
        #if S[h-1][w+1] == '#': count+=1
        if S[h+1][w-1] == '#': count+=1
        if S[h+1][w] == '#': count+=1
        #if S[h+1][w+1] == '#': count+=1
        S[h] = S[h][:w] + str(count)

w = 0
h = 0
s = S[h][w]
if s == '.':
        count = 0
        #if S[h][w-1] == '#': count+=1
        if S[h][w+1] == '#': count+=1
        #if S[h-1][w-1] == '#': count+=1
        #if S[h-1][w] == '#': count+=1
        #if S[h-1][w+1] == '#': count+=1
        #if S[h+1][w-1] == '#': count+=1
        if S[h+1][w] == '#': count+=1
        if S[h+1][w+1] == '#': count+=1
        S[h] = str(count) + S[h][w+1:]
        
w = 0
h = H-1
s = S[h][w]
if s == '.':
        count = 0
        #if S[h][w-1] == '#': count+=1
        if S[h][w+1] == '#': count+=1
        #if S[h-1][w-1] == '#': count+=1
        if S[h-1][w] == '#': count+=1
        if S[h-1][w+1] == '#': count+=1
        #if S[h+1][w-1] == '#': count+=1
        #if S[h+1][w] == '#': count+=1
        #if S[h+1][w+1] == '#': count+=1
        S[h] = str(count) + S[h][w+1:]

w = W-1
h = 0
s = S[h][w]
if s == '.':
        count = 0
        if S[h][w-1] == '#': count+=1
        #if S[h][w+1] == '#': count+=1
        #if S[h-1][w-1] == '#': count+=1
        #if S[h-1][w] == '#': count+=1
        #if S[h-1][w+1] == '#': count+=1
        if S[h+1][w-1] == '#': count+=1
        if S[h+1][w] == '#': count+=1
        #if S[h+1][w+1] == '#': count+=1
        S[h] = S[h][:w] + str(count)

w = W-1
h = H-1
s = S[h][w]
if s == '.':
        count = 0
        if S[h][w-1] == '#': count+=1
        #if S[h][w+1] == '#': count+=1
        if S[h-1][w-1] == '#': count+=1
        if S[h-1][w] == '#': count+=1
        #if S[h-1][w+1] == '#': count+=1
        #if S[h+1][w-1] == '#': count+=1
        #if S[h+1][w] == '#': count+=1
        #if S[h+1][w+1] == '#': count+=1
        S[h] = S[h][:w] + str(count)
        

        

for h in range(H):
    print(S[h])


Fixed submission:
#ABC 075 B - Minesweeper
H, W = input().split()
H = int(H)
W = int(W)
S = [input() for i in range(H)]

if H == 1:
    if W == 1:
        if S[0][0] == '.':
            print('0')
        if S[0][0] == '#':
            print('#')
        exit()
    for w in range(1,W-1):
        s = S[0][w]
        if s == '.':
            count = 0
            if S[0][w-1] == '#': count+=1
            if S[0][w+1] == '#': count+=1
            S[0] = S[0][:w] + str(count) + S[0][w+1:]
    w = 0
    h = 0
    s = S[h][w]
    if s == '.':
            count = 0
            if S[h][w+1] == '#': count+=1
            S[h] = str(count) + S[h][w+1:]
    w = W-1
    h = 0
    s = S[h][w]
    if s == '.':
            count = 0
            if S[h][w-1] == '#': count+=1
            S[h] = S[h][:w] + str(count)
    for h in range(H):
        print(S[h])
    exit()
    
if W == 1:
    for h in range(1,H-1):
        w = 0
        s = S[h][w]
        if s == '.':
            count = 0
            if S[h-1][w] == '#': count+=1
            if S[h+1][w] == '#': count+=1
            S[h] = str(count)
    w = 0
    h = 0
    s = S[h][w]
    if s == '.':
            count = 0
            if S[h+1][w] == '#': count+=1
            S[h] = str(count)
    w = W-1
    h = 0
    s = S[h][w]
    if s == '.':
            count = 0
            if S[h-1][w] == '#': count+=1
            S[h] = str(count)
    for h in range(H):
        print(S[h])
    exit()

    


for w in range(1,W-1):
    s = S[0][w]
    if s == '.':
        count = 0
        if S[0][w-1] == '#': count+=1
        if S[0][w+1] == '#': count+=1
        if S[1][w-1] == '#': count+=1
        if S[1][w] == '#': count+=1
        if S[1][w+1] == '#': count+=1
        S[0] = S[0][:w] + str(count) + S[0][w+1:]

for w in range(1,W-1):
    s = S[H-1][w]
    if s == '.':
        count = 0
        if S[H-1][w-1] == '#': count+=1
        if S[H-1][w+1] == '#': count+=1
        if S[H-2][w-1] == '#': count+=1
        if S[H-2][w] == '#': count+=1
        if S[H-2][w+1] == '#': count+=1
        S[H-1] = S[H-1][:w] + str(count) + S[H-1][w+1:]
        
for h in range(1,H-1):
    for w in range(1,W-1):
        s = S[h][w]
        if s == '.':
            count = 0
            if S[h][w-1] == '#': count+=1
            if S[h][w+1] == '#': count+=1
            if S[h-1][w-1] == '#': count+=1
            if S[h-1][w] == '#': count+=1
            if S[h-1][w+1] == '#': count+=1
            if S[h+1][w-1] == '#': count+=1
            if S[h+1][w] == '#': count+=1
            if S[h+1][w+1] == '#': count+=1
            S[h] = S[h][:w] + str(count) + S[h][w+1:]

for h in range(1,H-1):
    w = 0
    s = S[h][w]
    if s == '.':
        count = 0
        #if S[h][w-1] == '#': count+=1
        if S[h][w+1] == '#': count+=1
        #if S[h-1][w-1] == '#': count+=1
        if S[h-1][w] == '#': count+=1
        if S[h-1][w+1] == '#': count+=1
        #if S[h+1][w-1] == '#': count+=1
        if S[h+1][w] == '#': count+=1
        if S[h+1][w+1] == '#': count+=1
        S[h] = str(count) + S[h][w+1:]

for h in range(1,H-1):
    w = W-1
    s = S[h][w]
    if s == '.':
        count = 0
        if S[h][w-1] == '#': count+=1
        #if S[h][w+1] == '#': count+=1
        if S[h-1][w-1] == '#': count+=1
        if S[h-1][w] == '#': count+=1
        #if S[h-1][w+1] == '#': count+=1
        if S[h+1][w-1] == '#': count+=1
        if S[h+1][w] == '#': count+=1
        #if S[h+1][w+1] == '#': count+=1
        S[h] = S[h][:w] + str(count)

w = 0
h = 0
s = S[h][w]
if s == '.':
        count = 0
        #if S[h][w-1] == '#': count+=1
        if S[h][w+1] == '#': count+=1
        #if S[h-1][w-1] == '#': count+=1
        #if S[h-1][w] == '#': count+=1
        #if S[h-1][w+1] == '#': count+=1
        #if S[h+1][w-1] == '#': count+=1
        if S[h+1][w] == '#': count+=1
        if S[h+1][w+1] == '#': count+=1
        S[h] = str(count) + S[h][w+1:]
        
w = 0
h = H-1
s = S[h][w]
if s == '.':
        count = 0
        #if S[h][w-1] == '#': count+=1
        if S[h][w+1] == '#': count+=1
        #if S[h-1][w-1] == '#': count+=1
        if S[h-1][w] == '#': count+=1
        if S[h-1][w+1] == '#': count+=1
        #if S[h+1][w-1] == '#': count+=1
        #if S[h+1][w] == '#': count+=1
        #if S[h+1][w+1] == '#': count+=1
        S[h] = str(count) + S[h][w+1:]

w = W-1
h = 0
s = S[h][w]
if s == '.':
        count = 0
        if S[h][w-1] == '#': count+=1
        #if S[h][w+1] == '#': count+=1
        #if S[h-1][w-1] == '#': count+=1
        #if S[h-1][w] == '#': count+=1
        #if S[h-1][w+1] == '#': count+=1
        if S[h+1][w-1] == '#': count+=1
        if S[h+1][w] == '#': count+=1
        #if S[h+1][w+1] == '#': count+=1
        S[h] = S[h][:w] + str(count)

w = W-1
h = H-1
s = S[h][w]
if s == '.':
        count = 0
        if S[h][w-1] == '#': count+=1
        #if S[h][w+1] == '#': count+=1
        if S[h-1][w-1] == '#': count+=1
        if S[h-1][w] == '#': count+=1
        #if S[h-1][w+1] == '#': count+=1
        #if S[h+1][w-1] == '#': count+=1
        #if S[h+1][w] == '#': count+=1
        #if S[h+1][w+1] == '#': count+=1
        S[h] = S[h][:w] + str(count)
        

        

for h in range(H):
    print(S[h])