Problem p03361 (python)

Fixed submission:
H,W = map(int,input().split(" "))
picture = []

for i in range(H):
    picture.append(list(input()))

for i in range(H):
    for j in range(W):
        if picture[i][j] == "#":
            if i == 0:
                if (j == 0)and(picture[0][1] == ".")and(picture[1][0]):
                    print("No")
                    exit()
                elif (j == W-1)and(picture[0][j-1] == ".")and(picture[1][j] == "."):
                    print("No")
                    exit()
                else:
                    if (picture[i][j-1] == ".")and(picture[i+1][j] == ".")and(picture[i][j+1] == "."):
                        print("No")
                        exit()
            elif i == H-1:
                if (j == 0)and(picture[i-1][j] == ".")and(picture[i][j+1] == "."):
                    print("No")
                    exit()
                elif (j == W-1)and(picture[i-1][j] == ".")and(picture[i][j-1] == "."):
                    print("No")
                    exit()
                else:
                    if (picture[i][j-1] == ".")and(picture[i-1][j] == ".")and(picture[i][j+1] == "."):
                        print("No")
                        exit()
            elif j == 0:
                if (picture[i+1][j] == ".")and(picture[i][j+1] == ".")and(picture[i-1][j] == "."):
                    print("No")
                    exit()
            elif j == W-1:
                if (picture[i-1][j] == ".")and(picture[i][j-1] == ".")and(picture[i+1][j] == "."):
                    print("No")
                    exit()
            else:
                if (picture[i+1][j] == ".")and(picture[i-1][j] == ".")and(picture[i][j+1] == ".")and(picture[i][j-1] == "."):
                    print("No")
                    exit()
print("Yes")

Buggy submission (verdict: Runtime Error):
H,W = map(int,input().split(" "))
picture = []

for i in range(H):
    picture.append(list(input()))

for i in range(H):
    for j in range(W):
        if picture[i][j] == ".":
            continue
        elif picture[i][j] == "S":
            if i == 0:
                if (j == W-1)and(picture[1][j] == "#"):
                    picture[1][j] == "S"
                else:
                    if picture[0][j+1] == "#":
                        picture[0][j+1] = "S"
                    if picture[1][j] == "#":
                        picture[1][j] = "S"
            elif i == H-1:
                if (j == 0)and(picture[i][1] == "#"):
                    picture[i][1] ="S"
                elif j == W-1:
                    continue
                else:
                    if picture[i][j+1] == "#":
                        picture[i][j+1] = "S"
            elif j == W-1:
                if picture[i+1][j] == "#":
                    picture[i+1][j] = "S"
            else:
                if picture[i][j+1] == "#":
                    picture[i][j+1] = "S"
                if picture[i+1][j] == "#":
                    picture[i+1][j] = "S"
        else:
            if i==0:
                if (j != 0)and(j != W-1):
                    if (picture[i][j-1] == ".")and(picture[i-1][j] == ".")and(picture[i][j+1] == "."):
                        print("No")
                        exit()
                    if picture[i][j+1] == "#":
                        picture[i][j+1] == "S"
                    if picture[i+1][j] == "#":
                        picture[i+1][j] == "S"
                elif j == 0:
                    if (picture[1][0] == ".")and(picture[0][1] == "."):
                        print("No")
                        exit()
                    if picture[1][0] == "#":
                        picture[1][0] = "S"
                    if picture[0][1] == "#":
                        picture[0][1] = "S"
                else:
                    print("No")
                    exit()
            elif i==H-1:
                if (j != 0)and(j != W-1):
                    if (picture[i][j-1] == ".")and(picture[i][j+1] == "."):
                        print("No")
                        exit()
                    if picture[i][j+1] == "#":
                        picture[i][j+1] == "S"
                elif j == 0:
                    if picture[i][1] == ".":
                        print("No")
                        exit()
                    if picture[i][1] == "#":
                        picture[i][1] = "S"
                else:
                    if picture[i][j] == "#":
                        print("No")
                        exit()
            elif j==0:
                if (picture[i+1][j] == ".")and(picture[i][1] == "."):
                    print("No")
                    exit()
                if picture[i+1][0] == "#":
                    picture[i+1][0] = "S"
                if picture[i][1] == "#":
                    picture[i][1] = "S"
            elif j==W-1:
                if picture[i+1][j] == ".":
                    print("No")
                    exit()
                if picture[i+1][j] == "#":
                    picture[i+1][j] = "S"
            else:
                if (picture[i+1][j] == ".")and(picture[i-1][j] == ".")and(picture[i][j+1] == ".")and(picture[i][j-1] == "."):
                    print("No")
                    exit()
                if picture[i+1][j] == "#":
                    picture[i+1][j] = "S"
                if picture[i-1][j] == "#":
                    picture[i-1][j] = "S"
                if picture[i][j+1] == "#":
                    picture[i][j+1] = "S"
                if picture[i][j-1] == "#":
                    picture[i][j-1] = "S"
print("Yes")