Problem p03361 (python)

Fixed submission:
H,W = map(int,input().split())
a = [["."]*(W+2)]*(H+1)
f = True
for i in range(H):
    a[i+1] = ["."]
    a[i+1].extend([x for x in input()])
    a[i+1].append(".")
a.append(["."]*(W+2))
for i in range(1,H+1):
    for j in range(1,W+1):
        if a[i][j] == "#":
            if a[i][j+1] == "." and a[i][j-1] == "." and a[i+1][j] == "." and a[i-1][j] == ".":
                f = False
                break
                break
if f:
    print("Yes")
else:
    print("No")

Buggy submission (verdict: Runtime Error):
H,W = map(int,input().split())
a = [[x for x in input()] for i in range(H)]
f = True
if H ==1 and W == 1:
    if a[0][0] == "#":
        print("No")
        f = False
if H == 1 and W != 1:
    if a[0][0] == "#":
        if a[0][1] == ".":
            print("No")
            f = False
    if a[0][-1] == "#":
        if a[0][-2] == ".":
            print("No")
            f = False
    for i in range(1,W-1):
        if a[0][i] == "#":
            if a[0][i-1] == "." and a[0][i+1] == ".":
                print("No")
                f = False
                break

if H != 1 and W == 1:
    if a[0][0] == "#":
        if a[1][0] == ".":
            print("No")
            f = False
    if a[-1][0] == "#":
        if a[-2][0] == ".":
            print("No")
            f = False
    for i in range(1,H-1):
        if a[i][0] == "#":
            if a[i-1][0] == "." and a[i+1][0] == ".":
                print("No")
                f = False
                break

if H != 1 and W != 1:
    if a[0][0] == "#":
        if a[0][1] == "." and a[1][0] == ".":
            print("No")
            f = False
    if a[0][-1] == "#":
        if a[0][-2] == "." and a[1][-1] == ".":
            print("No")
            f = False
    if a[-1][0] == "#":
        if a[-1][1] == "." and a[-2][0] == ".":
            print("No")
            f = False
    if a[-1][-1] == "#":
        if a[-1][-2] == "." and a[-2][-1] == ".":
            print("No")
            f = False
    for i in range(1,H-1):
        if a[0][i] == "#":
            if a[0][i-1] == "." and a[0][i+1] == "." and a[1][i] == ".":
                print("No")
                f = False
                break
        if a[-1][i] == "#":
            if a[-1][i-1] == "." and a[-1][i+1] == "." and a[-2][i] == ".":
                print("No")
                f = False
                break
    for i in range(1,W-1):
        if a[i][0] == "#":
            if a[i-1][0] == "." and a[i+1][0] == "." and a[i][1] == ".":
                print("No")
                f = False
                break
        if a[i][-1] == "#":
            if a[i-1][-1] == "." and a[i+1][-1] == "." and a[i][-2] == ".":
                print("No")
                f = False
                break
        if f:
            for i in range(1,H-1):
                for j in range(1,W-1):
                    if a[i][j] == "#":
                        if a[i][j+1] == "." and a[i][j-1] == "." and a[i+1][j] == "." and a[i-1][j] == ".":
                            print("No")
                            f = False
                            break
                            break
    if f:
        print("Yes")

Buggy submission (verdict: Runtime Error):
H,W = map(int,input().split())
a = [[x for x in input()] for i in range(H)]
f = True
if a[0][0] == "#":
    if a[0][1] == "." and a[1][0] == ".":
        print("No")
        f = False
if a[0][-1] == "#":
    if a[0][-2] == "." and a[1][-1] == ".":
        print("No")
        f = False
if a[-1][0] == "#":
    if a[-1][1] == "." and a[-2][0] == ".":
        print("No")
        f = False
if a[-1][-1] == "#":
    if a[-1][-2] == "." and a[-2][-1] == ".":
        print("No")
        f = False
for i in range(1,H-1):
    if a[0][i] == "#":
        if a[0][i-1] == "." and a[0][i+1] == "." and a[1][i] == ".":
            print("No")
            f = False
            break
    elif a[-1][i] == "#":
        if a[-1][i-1] == "." and a[-1][i+1] == "." and a[-2][i] == ".":
            print("No")
            f = False
            break
for i in range(1,W-1):
    if a[i][0] == "#":
        if a[i-1][0] == "." and a[i+1][0] == "." and a[i][1] == ".":
            print("No")
            f = False
            break
    if a[i][-1] == "#":
        if a[i-1][-1] == "." and a[i+1][-1] == "." and a[i][-2] == ".":
            print("No")
            f = False
            break
    if f:
        for i in range(1,H-1):
            for j in range(1,W-1):
                if a[i][j] == "#":
                    if a[i][j+1] == "." and a[i][j-1] == "." and a[i+1][j] == "." and a[i-1][j] == ".":
                        print("No")
                        f = False
                        break
                        break
if f:
    print("Yes")

Buggy submission (verdict: Runtime Error):
H,W = map(int,input().split())
a = [[x for x in input()] for i in range(H)]
f = True
if a[0][0] == "#":
    if a[0][1] == "." and a[1][0] == ".":
        print("No")
        f = False
elif a[0][-1] == "#":
    if a[0][-2] == "." and a[1][-1] == ".":
        print("No")
        f = False
elif a[-1][0] == "#":
    if a[-1][1] == "." and a[-2][0] == ".":
        print("No")
        f = False
elif a[-1][-1] == "#":
    if a[-1][-2] == "." and a[-2][-1] == ".":
        print("No")
        f = False
else:
    for i in range(1,H-1):
        if a[0][i] == "#":
            if a[0][i-1] == "." and a[0][i+1] == "." and a[1][i] == ".":
                print("No")
                f = False
                break
        elif a[-1][i] == "#":
            if a[-1][i-1] == "." and a[-1][i+1] == "." and a[-2][i] == ".":
                print("No")
                f = False
                break
    for i in range(1,W-1):
        if a[i][0] == "#":
            if a[i-1][0] == "." and a[i+1][0] == "." and a[i][1] == ".":
                print("No")
                f = False
                break
        elif a[i][-1] == "#":
            if a[i-1][-1] == "." and a[i+1][-1] == "." and a[i][-2] == ".":
                print("No")
                f = False
                break
    if f:
        for i in range(1,H-1):
            for j in range(1,W-1):
                if a[i][j] == "#":
                    if a[i][j+1] == "." and a[i][j-1] == "." and a[i+1][j] == "." and a[i-1][j] == ".":
                        print("No")
                        f = False
                        break
                        break
if f:
    print("Yes")