Problem p03574 (python)

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

if h == 1 and w == 1:
    if s[0]=='#':
        print('#')
    else:
        print('0')

elif h == 1:
    ans = ''
    for i in range(w):
        if s[0][i]=='#':
            ans += '#'
        elif i != 0 and i != w-1:
            ans += str(s[0][i-1:i+2].count('#'))
        elif i == 0:
            ans += str(s[0][1].count('#'))
        elif i == w-1:
            ans += str(s[0][i-1].count('#'))
    print(ans)

elif w == 1:
    for i in range(h):
        if s[i]=='#':
            print('#')
        elif i != 0 and i != h-1:
            print(str(s[i-1:i+2].count('#')))
        elif i == 0:
            print(str(s[1].count('#')))
        elif i == h-1:
            print(str(s[i-1].count('#')))

else:
    for i in range(h):
        ans = ''
        for j in range(w):
            if s[i][j] == '#':
                ans += '#'
            else:
                if i != 0 and j!= 0 and i != h-1 and j!= w-1:
                    up = s[i-1][j-1:j+2].count('#')
                    parallel = s[i][j-1:j+2].count('#')
                    down = s[i+1][j-1:j+2].count('#')
                    ans += str(up+parallel+down)
                elif i==0 and j==0:
                    parallel = s[0][1].count('#')
                    down = s[1][0:2].count('#')
                    ans += str(parallel+down)
                elif i==0 and j==w-1:
                    parallel = s[0][w-2:].count('#')
                    down = s[1][w-2:].count('#')
                    ans += str(parallel+down)
                elif i==h-1 and j==0:
                    up = s[i-1][:2].count('#')
                    parallel = s[i][:2].count('#')
                    ans += str(up+parallel)
                elif i==h-1 and j==w-1:
                    up = s[i-1][w-2:].count('#')
                    parallel = s[i][w-2:].count('#')
                    ans += str(up+parallel)
                elif i==0:
                    parallel = s[0][j-1:j+2].count('#')
                    down = s[1][j-1:j+2].count('#')
                    ans += str(parallel+down)
                elif j==0:
                    up = s[i-1][:2].count('#')
                    parallel = s[i][:2].count('#')
                    down = s[i+1][:2].count('#')
                    ans += str(up+parallel+down)
                elif i==h-1:
                    up = s[i-1][j-1:j+2].count('#')
                    parallel = s[i][j-1:j+2].count('#')
                    ans += str(up+parallel)
                else:
                    up = s[i-1][w-2:].count('#')
                    parallel = s[i][w-2:].count('#')
                    down = s[i+1][w-2:].count('#')
                    ans += str(up+parallel+down)
        print(ans)

Buggy submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(10**9+7)

h,w = map(int,input().split())
s = [input() for i in range(h)]
for i in range(h):
    ans = ''
    for j in range(w):
        if s[i][j] == '#':
            ans += '#'
        else:
            if i != 0 and j!= 0 and i != h-1 and j!= w-1:
                up = s[i-1][j-1:j+2].count('#')
                parallel = s[i][j-1:j+2].count('#')
                down = s[i+1][j-1:j+2].count('#')
                ans += str(up+parallel+down)
            elif i==0 and j==0:
                parallel = s[0][1].count('#')
                down = s[1][0:2].count('#')
                ans += str(parallel+down)
            elif i==0 and j==w-1:
                parallel = s[0][w-2:].count('#')
                down = s[1][w-2:].count('#')
                ans += str(parallel+down)
            elif i==h-1 and j==0:
                up = s[i-1][:2].count('#')
                parallel = s[i][:2].count('#')
                ans += str(up+parallel)
            elif i==h-1 and j==w-1:
                up = s[i-1][w-2:].count('#')
                parallel = s[i][w-2:].count('#')
                ans += str(up+parallel)
            elif i==0:
                parallel = s[0][j-1:j+2].count('#')
                down = s[1][j-1:j+2].count('#')
                ans += str(parallel+down)
            elif j==0:
                up = s[i-1][:2].count('#')
                parallel = s[i][:2].count('#')
                down = s[i+1][:2].count('#')
                ans += str(up+parallel+down)
            elif i==h-1:
                up = s[i-1][j-1:j+2].count('#')
                parallel = s[i][j-1:j+2].count('#')
                ans += str(up+parallel)
            else:
                up = s[i-1][w-2:].count('#')
                parallel = s[i][w-2:].count('#')
                down = s[i+1][w-2:].count('#')
                ans += str(up+parallel+down)
    print(ans)

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

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

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