Problem p03574 (python)

Fixed submission:
import sys
h, w = map(int, input().split())
s = [input() for _ in range(h)]
if h == 1 and w == 1:
    if s[0] == '#':
        print('#')
    else:
        print(0)
    sys.exit()
for i, m in enumerate(s):
    for j in range(w):
        if m[j] != '#':
            if w == 1:
                if i == 0:
                    count = s[1][0].count('#')
                elif i == h-1:
                    count = s[-2][0].count('#')
                else:
                    count = s[i-1][0].count('#') + s[i+1][0].count('#')
            elif h == 1:
                if j == 0:
                    count = m[1].count('#')
                elif j == w-1:
                    count = m[-2].count('#')
                else:
                    count = m[j-1].count('#') + m[j+1].count('#')
            else:
                if i == 0:
                    if j == 0:
                        count = s[i+1][:2].count('#') + m[1].count('#')
                    elif j == w-1:
                        count = s[i+1][-2:].count('#') + m[-2].count('#')
                    else:
                        count = s[i+1][j-1:j+2].count('#') + m[j-1:j+2].count('#')
                elif i == h-1:
                    if j == 0:
                        count = s[-2][:2].count('#') + m[1].count('#')
                    elif j == w-1:
                        count = s[-2][-2:].count('#') + m[-2].count('#')
                    else:
                        count = s[i-1][j-1:j+2].count('#') + m[j-1:j+2].count('#')
                else:
                    if j == 0:
                        count = s[i-1][:2].count('#') + s[i+1][:2].count('#') + m[1].count('#')
                    elif j == w-1:
                        count = s[i-1][-2:].count('#') + s[i+1][-2:].count('#') + m[-2].count('#')
                    else:
                        count = s[i-1][j-1:j+2].count('#') + s[i+1][j-1:j+2].count('#') + m[j-1:j+2].count('#')
            m = m.replace(m[j], str(count), 1)
    print(m)

Buggy submission (verdict: Runtime Error):
h, w = map(int, input().split())
s = [input() for _ in range(h)]
for i, m in enumerate(s):
    for j in range(w):
        if m[j] != '#':
            if w == 1:
                if i == 0:
                    count = s[1][0].count('#')
                elif i == h-1:
                    count = s[-2][0].count('#')
                else:
                    count = s[i-1][0].count('#') + s[i+1][0].count('#')
            elif h == 1:
                if j == 0:
                    count = m[1].count('#')
                elif j == w-1:
                    count = m[-2].count('#')
                else:
                    count = m[j-1].count('#') + m[j+1].count('#')
            else:
                if i == 0:
                    if j == 0:
                        count = s[i+1][:2].count('#') + m[1].count('#')
                    elif j == w-1:
                        count = s[i+1][-2:].count('#') + m[-2].count('#')
                    else:
                        count = s[i+1][j-1:j+2].count('#') + m[j-1:j+2].count('#')
                elif i == h-1:
                    if j == 0:
                        count = s[-2][:2].count('#') + m[1].count('#')
                    elif j == w-1:
                        count = s[-2][-2:].count('#') + m[-2].count('#')
                    else:
                        count = s[i-1][j-1:j+2].count('#') + m[j-1:j+2].count('#')
                else:
                    if j == 0:
                        count = s[i-1][:2].count('#') + s[i+1][:2].count('#') + m[1].count('#')
                    elif j == w-1:
                        count = s[i-1][-2:].count('#') + s[i+1][-2:].count('#') + m[-2].count('#')
                    else:
                        count = s[i-1][j-1:j+2].count('#') + s[i+1][j-1:j+2].count('#') + m[j-1:j+2].count('#')
            m = m.replace(m[j], str(count), 1)
    print(m)

Buggy submission (verdict: Runtime Error):
h, w = map(int, input().split())
s = [input() for _ in range(h)]
for i, m in enumerate(s):
    for j in range(w):
        if m[j] != '#':
            if i == 0:
                if j == 0:
                    count = s[i+1][:2].count('#') + m[1].count('#')
                elif j == w-1:
                    count = s[i+1][-2:].count('#') + m[-2].count('#')
                else:
                    count = s[i+1][j-1:j+2].count('#') + m[j-1:j+2].count('#')
            elif i == h-1:
                if j == 0:
                    count = s[-2][:2].count('#') + m[1].count('#')
                elif j == w-1:
                    count = s[-2][-2:].count('#') + m[-2].count('#')
                else:
                    count = s[i-1][j-1:j+2].count('#') + m[j-1:j+2].count('#')
            else:
                if j == 0:
                    count = s[i-1][:2].count('#') + s[i+1][:2].count('#') + m[1].count('#')
                elif j == w-1:
                    count = s[i-1][-2:].count('#') + s[i+1][-2:].count('#') + m[-2].count('#')
                else:
                    count = s[i-1][j-1:j+2].count('#') + s[i+1][j-1:j+2].count('#') + m[j-1:j+2].count('#')
            m = m.replace(m[j], str(count), 1)
    print(m)

Buggy submission (verdict: Runtime Error):
h, w = map(int, input().split())
s = [input() for _ in range(h)]
for i, m in enumerate(s):
    for j in range(w):
        if m[j] != '#':
            if i == 0:
                if j == 0:
                    count = s[i+1][:2].count('#') + m[1].count('#')
                elif j == w-1:
                    count = s[i+1][-2:].count('#') + m[-2].count('#')
                else:
                    count = s[i+1][j-1:j+2].count('#') + m[j+1].count('#') + m[j-1].count('#')
            elif i == h-1:
                if j == 0:
                    count = s[i-1][:2].count('#') + m[1].count('#')
                elif j == w-1:
                    count = s[i-1][-2:].count('#') + m[-2].count('#')
                else:
                    count = s[i-1][j-1:j+2].count('#') + m[j+1].count('#') + m[j-1].count('#')
            else:
                if j == 0:
                    count = s[i-1][:2].count('#') + s[i+1][:2].count('#') + m[1].count('#')
                elif j == w-1:
                    count = s[i-1][-2:].count('#') + s[i+1][-2:].count('#') + m[-2].count('#')
                else:
                    count = s[i-1][j-1:j+2].count('#') + s[i+1][j-1:j+2].count('#') + m[j-1].count('#') + m[j+1].count('#')
            m = m.replace(m[j], str(count), 1)
    print(*m, sep='')