Problem p03574 (python)

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

for i in range(h):
    s_out.append("")

def mine(a):
    count = 0
    for i in range(len(a)):
        if a[i] == "#":
            count += 1
    return count

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

elif h==1:
    for j in range(w):
        if s[0][j] == "#":
            s_out[0] += "#"
        elif j == 0:
            s_out[0] += str(mine(s[0][1]))
        elif j == w-1:
            s_out[0] += str(mine(s[i][j-1]))
        else:
            s_out[0] += str(mine(s[i][j-1])+mine(s[i][j+1]))
    print(s_out[0])
    
    
elif w == 1:
    for i in range(h):
        if s[i][0] == "#":
            s_out[i] += "#"
        elif i == 0:
            s_out[i] += str(mine(s[1][0]))
        elif i == h-1:
            s_out[i] += str(mine(s[i-1][0]))
        else:
            s_out[i] += str(mine(s[i+1][0])+mine(s[i-1][0]))
    for i in s_out:
        print(i)
    
else:
    for i in range(h):
            for j in range(w):
                if s[i][j] == "#":
                    s_out[i] += "#"
                elif i == 0 and j == 0:
                    s_out[i] += str(mine(s[0][1])+mine(s[1][0:2]))
                elif i == 0 and j == w-1:
                    s_out[i] += str(mine(s[i][j-1])+mine(s[i+1][j-1:j+1]))
                elif i == 0:
                    s_out[i] += str(mine(s[i][j-1])+mine(s[i][j+1])+mine(s[i+1][j-1:j+2]))
                elif i == h-1 and j == 0:
                    s_out[i] += str(mine(s[i-1][j:j+2])+mine(s[i][j+1]))
                elif i == h-1 and j == w-1:
                    s_out[i] += str(mine(s[i-1][j-1:j+1])+mine(s[i][j-1]))
                elif i == h-1:
                    s_out[i] += str(mine(s[i-1][j-1:j+2])+mine(s[i][j-1])+mine(s[i][j+1]))
                elif j == 0:
                    s_out[i] += str(mine(s[i-1][j:j+2])+mine(s[i][j+1])+mine(s[i+1][j:j+2]))
                elif j == w-1:
                    s_out[i] += str(mine(s[i-1][j-1:j+1])+mine(s[i][j-1])+mine(s[i+1][j-1:j+1]))
                else:
                    s_out[i] += str(mine(s[i-1][j-1:j+2])+mine(s[i][j-1])+mine(s[i][j+1])+mine(s[i+1][j-1:j+2]))
    for i in s_out:
        print(i)

Buggy submission (verdict: Runtime Error):
h,w =map(int,input().split())
s = [input() for i in range(h)]
dx = [1,1,1,0,0,-1,-1,-1]
dy = [1,-1,0,1,-1,1,-1,0]
for i in range(h):
    for j in range(w):
        for k in range(8):
            x = i + dx[k]
            y = j + dy[k]
            count = 0
            if x >= 0 and x <= h-1 and y >= 0 and y<= w-1 and s[x][y] == "#":
                count += 1
        s[i][j] = str(count)
for i in s:
  print(s)

Buggy submission (verdict: Runtime Error):
h,w =map(int,input().split())
s = [input() for i in range(h)]
s_out =[]
for i in range(h):
    s_out.append("")

def mine(a):
    count = 0
    for i in range(len(a)):
        if a[i] == "#":
            count += 1
    return count

if h == 1 and w == 1:
    if s[0][0] == "#":
        print("#")
        sys.exit()
    else:
        print("0")
        sys.exit()

elif h==1:
    for j in range(w):
        if s[0][j] == "#":
            s_out[0] += "#"
        elif j == 0:
            s_out[0] += str(mine(s[0][1]))
        elif j == w-1:
            s_out[0] += str(mine(s[i][j-1]))
        else:
            s_out[0] += str(mine(s[i][j-1])+mine(s[i][j+1]))
    print(s_out[0])
    sys.exit()
    
elif w == 1:
    for i in range(h):
        if s[i][0] == "#":
            s_out[i] += "#"
        elif i == 0:
            s_out[i] += str(mine(s[1][0]))
        elif i == h-1:
            s_out[i] += str(mine(s[i-1][0]))
        else:
            s_out[i] += str(mine(s[i+1][0])+mine(s[i-1][0]))
    for i in s_out:
        print(i)
    sys.exit()

for i in range(h):
        for j in range(w):
            if s[i][j] == "#":
                s_out[i] += "#"
            elif i == 0 and j == 0:
                s_out[i] += str(mine(s[0][1])+mine(s[1][0:2]))
            elif i == 0 and j == w-1:
                s_out[i] += str(mine(s[i][j-1])+mine(s[i+1][j-1:j+1]))
            elif i == 0:
                s_out[i] += str(mine(s[i][j-1])+mine(s[i][j+1])+mine(s[i+1][j-1:j+2]))
            elif i == h-1 and j == 0:
                s_out[i] += str(mine(s[i-1][j:j+2])+mine(s[i][j+1]))
            elif i == h-1 and j == w-1:
                s_out[i] += str(mine(s[i-1][j-1:j+1])+mine(s[i][j-1]))
            elif i == h-1:
                s_out[i] += str(mine(s[i-1][j-1:j+2])+mine(s[i][j-1])+mine(s[i][j+1]))
            elif j == 0:
                s_out[i] += str(mine(s[i-1][j:j+2])+mine(s[i][j+1])+mine(s[i+1][j:j+2]))
            elif j == w-1:
                s_out[i] += str(mine(s[i-1][j-1:j+1])+mine(s[i][j-1])+mine(s[i+1][j-1:j+1]))
            else:
                s_out[i] += str(mine(s[i-1][j-1:j+2])+mine(s[i][j-1])+mine(s[i][j+1])+mine(s[i+1][j-1:j+2]))
for i in s_out:
    print(i)

Buggy submission (verdict: Runtime Error):
h,w =map(int,input().split())
s = [input() for i in range(h)]
s_out =[]
for i in range(h):
    s_out.append("")

def mine(a):
    count = 0
    for i in range(len(a)):
        if a[i] == "#":
            count += 1
    return count

for i in range(h):
        for j in range(w):
            if s[i][j] == "#":
                s_out[i] += "#"
            elif i == 0 and j == 0:
                s_out[i] += str(mine(s[0][1])+mine(s[1][0:2]))
            elif i == 0 and j == w-1:
                s_out[i] += str(mine(s[i][j-1])+mine(s[i+1][j-1:j+1]))
            elif i == 0:
                s_out[i] += str(mine(s[i][j-1])+mine(s[i][j+1])+mine(s[i+1][j-1:j+2]))
            elif i == h-1 and j == 0:
                s_out[i] += str(mine(s[i-1][j:j+2])+mine(s[i][j+1]))
            elif i == h-1 and j == w-1:
                s_out[i] += str(mine(s[i-1][j-1:j+1])+mine(s[i][j-1]))
            elif i == h-1:
                s_out[i] += str(mine(s[i-1][j-1:j+2])+mine(s[i][j-1])+mine(s[i][j+1]))
            elif j == 0:
                s_out[i] += str(mine(s[i-1][j:j+2])+mine(s[i][j+1])+mine(s[i+1][j:j+2]))
            elif j == w-1:
                s_out[i] += str(mine(s[i-1][j-1:j+1])+mine(s[i][j-1])+mine(s[i+1][j-1:j+1]))
            else:
                s_out[i] += str(mine(s[i-1][j-1:j+2])+mine(s[i][j-1])+mine(s[i][j+1])+mine(s[i+1][j-1:j+2]))
for i in s_out:
    print(i)

Buggy submission (verdict: Runtime Error):
h,w =map(int,input().split())
s = [input() for i in range(h)]
s_out =[]
for i in range(h):
    s_out.append("")

def mine(a):
    count = 0
    for i in range(len(a)):
        if a[i] == "#":
            count += 1
    return count

for i in range(h):
        for j in range(w):
            if s[i][j] == "#":
                s_out[i] += "#"
            elif i == 0 and j == 0:
                s_out[i] += str(mine(s[0][1])+mine(s[1][0:2]))
            elif i == 0 and j == w-1:
                s_out[i] += str(mine(s[i][j-1])+mine(s[i+1][j-1:j+1]))
            elif i == 0:
                s_out[i] += str(mine(s[i][j-1])+mine(s[i][j+1])+mine(s[i+1][j-1:j+2]))
            elif i == h-1 and j == 0:
                s_out[i] += str(mine(s[i-1][j:j+2])+mine(s[i][j+1]))
            elif i == h-1 and j == w-1:
                s_out[i] += str(mine(s[i-1][j-1:j+1])+mine(s[i][j-1]))
            elif i == h-1:
                s_out[i] += str(mine(s[i-1][j-1:j+2])+mine(s[i][j-1])+mine(s[i][j+1]))
            elif j == 0:
                s_out[i] += str(mine(s[i-1][j:j+2])+mine(s[i][j+1])+mine(s[i+1][j:j+2]))
            elif j == w-1:
                s_out[i] += str(mine(s[i-1][j-1:j+1])+mine(s[i][j-1])+mine(s[i+1][j-1:j+1]))
            else:
                s_out[i] += str(mine(s[i-1][j-1:j+2])+mine(s[i][j-1])+mine(s[i][j+1])+mine(s[i+1][j-1:j+2]))
for i in s_out:
    print(i)