Problem p03361 (python)

Fixed submission:
def check(i, j, w, h, s):
    if (i + 1 < h and s[i + 1][j] == "#"):
        return True
    if (i - 1 >= 0 and s[i - 1][j] == "#"):
        return True
    if (j + 1 < w and s[i][j + 1] == "#"):
        return True
    if (j - 1 >= 0 and s[i][j - 1] == "#"):
        return True
    return False


h, w = map(int, input().split())
s = [list(input()) for _ in range(h)]

for i in range(h):
    for j in range(w):
        if (s[i][j] == "#"):
            if check(i, j, w, h, s):
                True
            else:
                print("No")
                exit()
print("Yes")

Buggy submission (verdict: Runtime Error):
H, W = map(int, input().split())
lists = []
s_list = []
for i in range(H):
    s = str(input())
    s_list.append(s)
    lists.append(s)
cnt = 0
for i in range(H):
    for j in range(W):
        if i == 0 and j == 0:  # 左端上の処理
            cnt = 0
            if lists[i][j] == "#":
                if lists[i][1] == ".":
                    cnt += 1
                if lists[1][0] == ".":
                    cnt += 1
                if cnt == 2:
                    print("No")
                    exit()
                else:
                    pass
        if i == H-1 and j == 0:  # 左端下の処理
            cnt = 0
            if lists[i][j] == "#":
                if lists[i][1] == ".":
                    cnt += 1
                if lists[i-1][j+1] == ".":
                    cnt += 1
                if cnt == 2:
                    print("No")
                    exit()
                else:
                    pass
        if i == 0 and j == W - 1:  # 右端上の処理
            cnt = 0
            if lists[i][j] == "#":
                if lists[i][j - 1] == ".":
                    cnt += 1
                if lists[i + 1][j] == ".":
                    cnt += 1
                if cnt == 2:
                    print("No")
                    exit()
                else:
                    pass
        if i == H and j == W - 1:
            cnt = 0
            if lists[i][j] == "#":
                if lists[i][j - 1] == ".":
                    cnt += 1
                if lists[i - 1][j - 1] == ".":
                    cnt += 1
                if cnt == 2:
                    print("No")
                    exit()
                else:
                    pass
        if i == 0:
            if j == 0 or j == W - 1:
                pass
            cnt = 0
            if lists[i][j] == "#":
                if lists[i][j + 1] == ".":
                    cnt += 1
                if lists[i][j - 1] == ".":
                    cnt += 1
                if lists[i + 1][j] == ".":
                    cnt += 1
                if cnt == 3:
                    print("No")
                    exit()
            else:
                pass
        if i == H - 1 and (j != 0 or j != W - 1):
            if j == 0 or j == W - 1:
                pass
            cnt = 0
            if lists[i][j] == "#":
                if lists[i][j + 1] == ".":
                    cnt += 1
                if lists[i][j - 1] == ".":
                    cnt += 1
                if lists[i - 1][j] == ".":
                    cnt += 1
                if cnt == 3:
                    print(i, j)
                    print("No")
                    exit()
                else:
                    pass
        if (i != 0 or i != W - 1) and j == 0:
            cnt = 0
            if lists[i][j] == "#":
                if lists[i + 1][j] == ".":
                    cnt += 1
                if lists[i - 1][j] == ".":
                    cnt += 1
                if lists[i][j + 1] == ".":
                    cnt += 1
                if cnt == 3:
                    print("No")
                    exit()
                else:
                    pass
        if (i != 0 or i != W - 1) and j == W - 1:
            cnt = 0
            if lists[i][j] == "#":
                if lists[i + 1][j] == ".":
                    cnt += 1
                if lists[i - 1][j] == ".":
                    cnt += 1
                if lists[i][j - 1] == ".":
                    cnt += 1
                if cnt == 3:
                    print("No")
                    exit()
                else:
                    pass

print("Yes")