Problem p03361 (python)

Fixed submission:
if __name__ == '__main__':
    H, W = map(int, input().split())
    s_list = [list(input()) for i in range(H)]


    answer = "Yes"

    for x in range(H):
        for y in range(W):
            if s_list[x][y] == '.':
                continue
            is_all_white = True
            dxs = [0, 1, 0, -1]
            dys = [1, 0, -1, 0]
            for dx, dy in zip(dxs, dys):
                nx = x + dx
                ny = y + dy
                if 0 <= nx <= H-1 and 0 <= ny <= W-1:
                    if s_list[nx][ny] == "#":
                        is_all_white = False
                        break
            if is_all_white:
                answer = "No"
                break
        if s_list[x][y] == '#' and is_all_white:
            break

    print(answer)

Buggy submission (verdict: Runtime Error):
if __name__ == '__main__':
    H, W = map(int, input().split())
    s_list = [list(input()) for i in range(H)]


    answer = "Yes"

    for x in range(H):
        for y in range(W):
            if s_list[x][y] == '.':
                continue
            is_all_white = True
            dxs = [0, 1, 0, -1]
            dys = [1, 0, -1, 0]
            for dx, dy in zip(dxs, dys):
                nx = x + dx
                ny = y + dy
                if 0 <= nx <= H-1 and 0 <= ny <= W-1:
                    if s_list[nx][ny] == "#":
                        is_all_white = False
                        break
            if is_all_white:
                answer = "No"
                break
        if is_all_white:
            break

    print(answer)

Buggy submission (verdict: Runtime Error):
if __name__ == '__main__':
    H, W = map(int, input().split())
    s_list = [list(input()) for i in range(H)]

    answer = "Yes"
    for i in range(H):
        for j in range(W):
            if s_list[i][j] == "#" and answer == "Yes":

                if i == 0:
                    if j == 0:
                        if s_list[i][j+1] == "." and s_list[i+1][j] == ".":
                            answer = "No"
                            break
                    elif j == W-1:
                        if s_list[i][j-1] == "." and s_list[i+1][j] == ".":
                            answer = "No"
                            break
                    else:
                        if s_list[i][j+1] == "." and s_list[i][j-1] == "." and s_list[i+1][j] == ".":
                            answer = "No"
                            break
                elif i == H - 1:
                    if s_list[i][j+1] == "." and s_list[i-1][j] == ".":
                            answer = "No"
                            break
                    elif j == W-1:
                        if s_list[i][j-1] == "." and s_list[i-1][j] == ".":
                            answer = "No"
                            break
                    else:
                        if s_list[i][j+1] == "." and s_list[i][j-1] == "." and s_list[i-1][j] == ".":
                            answer = "No"
                            break
                else:
                    if j == 0:
                        if s_list[i][j+1] == "." and s_list[i+1][j] == "." and s_list[i-1][j] == ".":
                            answer = "No"
                            break
                    elif j == W-1:
                        if s_list[i][j-1] == "." and s_list[i+1][j] == "." and s_list[i-1][j] == ".":
                            answer = "No"
                            break
                    else:
                        if s_list[i][j+1] == "." and s_list[i][j-1] == "." and s_list[i+1][j] == "." and s_list[i-1][j] == ".":
                            answer = "No"
                            break

    print(answer)

Buggy submission (verdict: Runtime Error):
if __name__ == '__main__':
    H, W = map(int, input().split())
    s_list = [list(input()) for i in range(H)]

    answer = "Yes"
    for i in range(H):
        for j in range(W):
            if s_list[i][j] == "#" and answer == "Yes":

                if i == 0:
                    if j == 0:
                        if s_list[i][j+1] == "." and s_list[i+1][j] == ".":
                            answer = "No"
                            break
                    elif j == W-1:
                        if s_list[i][j-1] == "." and s_list[i+1][j] == ".":
                            answer = "No"
                            break
                    else:
                        if s_list[i][j+1] == "." and s_list[i][j-1] == "." and s_list[i+1][j] == ".":
                            answer = "No"
                            break
                elif i == H - 1:
                    if s_list[i][j+1] == "." and s_list[i-1][j] == ".":
                            answer = "No"
                            break
                    elif j == W-1:
                        if s_list[i][j-1] == "." and s_list[i-1][j] == ".":
                            answer = "No"
                            break
                    else:
                        if s_list[i][j+1] == "." and s_list[i][j-1] == "." and s_list[i-1][j] == ".":
                            answer = "No"
                            break
                else:
                    if j == 0:
                        if s_list[i][j+1] == "." and s_list[i+1][j] == "." and s_list[i-1][j] == ".":
                            answer = "No"
                            break
                    elif j == W-1:
                        if s_list[i][j-1] == "." and s_list[i+1][j] == "." and s_list[i-1][j] == ".":
                            answer = "No"
                            break
                    else:
                        if s_list[i][j+1] == "." and s_list[i][j-1] == "." and s_list[i+1][j] == "." and s_list[i-1][j] == ".":
                            answer = "No"
                            break

    print(answer)