Problem p03361 (python)

Fixed submission:
#!/usr/bin/env python3
import sys

YES = "Yes"  # type: str
NO = "No"  # type: str


def inRange(h: int, w: int, H: int, W: int):
    return h >= 0 and w >= 0 and h < H and w < W


def solve(H: int, W: int, s: "List[str]"):
    failed = False
    for h in range(H):
        for w in range(W):
            cur = s[h][w]
            if cur == "#":
                if inRange(h-1, w, H, W) and s[h-1][w] == "#":
                    continue
                if inRange(h+1, w, H, W) and s[h+1][w] == "#":
                    continue
                if inRange(h, w-1, H, W) and s[h][w-1] == "#":
                    continue
                if inRange(h, w+1, H, W) and s[h][w+1] == "#":
                    continue
                failed = True
                break
        if failed:
            break
    print("No") if failed else print("Yes")
    return


# Generated by 1.1.4 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    H = int(next(tokens))  # type: int
    W = int(next(tokens))  # type: int
    s = [next(tokens) for _ in range(H)]  # type: "List[str]"
    solve(H, W, s)


if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
#!/usr/bin/env python3
import sys

YES = "Yes"  # type: str
NO = "No"  # type: str


def inRange(h: int, w: int, H: int, W: int):
    return h >= 0 and w >= 0 and h < H and w < W


def solve(H: int, W: int, s: "List[str]"):
    failed = False
    for h in range(H):
        for w in range(W):
            cur = s[h][w]
            if cur == "#":
                if inRange(h-1, w, H, W) and s[h-1][w] == "#":
                    continue
                if inRange(h+1, w, H, W) and s[h+1][w] == "#":
                    continue
                if inRange(h, w-1, H, W) and s[h][w-1] == "#":
                    continue
                if inRange(h, w+1, H, W) and s[h][w+1] == "#":
                    continue
                failed = True
                break
        if failed:
            break
    print("No") if failed else print("Yes")
    return


# Generated by 1.1.4 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    H = int(next(tokens))  # type: int
    W = int(next(tokens))  # type: int
    s = [next(tokens) for _ in range(W)]  # type: "List[str]"
    solve(H, W, s)


if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
#!/usr/bin/env python3
import sys

YES = "Yes"  # type: str
NO = "No"  # type: str


def inRange(h: int, w: int, H: int, W: int):
    return h >= 0 and w >= 0 and h < H and w < W


def solve(H: int, W: int, s: "List[str]"):
    for h in range(H):
        for w in range(W):
            cur = s[h][w]
            if cur == "#":
                if inRange(h-1, w, H, W) and s[h-1][w] == "#":
                    continue
                if inRange(h+1, w, H, W) and s[h+1][w] == "#":
                    continue
                if inRange(h, w-1, H, W) and s[h][w-1] == "#":
                    continue
                if inRange(h, w+1, H, W) and s[h][w+1] == "#":
                    continue
                print("No")
                sys.exit(0)
    print("Yes")
    return


# Generated by 1.1.4 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    H = int(next(tokens))  # type: int
    W = int(next(tokens))  # type: int
    s = [next(tokens) for _ in range(W)]  # type: "List[str]"
    solve(H, W, s)


if __name__ == '__main__':
    main()