Problem: We have a grid of H rows and W columns. Initially, there is a stone in the top left cell. Shik is trying to move the stone to the bottom right cell. In each step, he can move the stone one cell to its left, up, right, or down (if such cell exists). It is possible that the stone visits a cell multiple times (including the bottom right and the top left cell).  You are given a matrix of characters a_{ij} (1 \leq i \leq H, 1 \leq j \leq W). After Shik completes all moving actions, a_{ij} is `#` if the stone had ever located at the i-th row and the j-th column during the process of moving. Otherwise, a_{ij} is `.`. Please determine whether it is possible that Shik only uses right and down moves in all steps.

Submission attempts (in order):
# -*-coding: utf-8 -*-
import math
def main():
    H, W = map(int, input().split())
    A = ["." * (W+2)]
    for i in range(H):
        A.append("." + input() + ".")
    A.append("." * (W + 2))
    #print(A)
    ans = "Possible"
    for w in range(1, W+1):
        for h in range(1, H+1):
            if A[h][w] == "#":
                if w == h == 1:
                    pass
                elif h == H and w == W:
                    pass
                else:
                    if A[h-1][w] == A[h+1][w] == "
                        pass
                    elif A[h-1][w] == A[h+1][w] == "." and A[h][w-1] == A[h][w+1] == "
                        pass
                    elif A[h-1][w] == A[h][w+1] == "#" and A[h+1][w] == A[h][w-1] == ".":   # L字
                        pass
                    elif A[h+1][w] == A[h][w-1] == "#" and A[h-1][w] == A[h][w+1] == ".":   # L時
                        pass
                    else:
                        ans = "Impossible"
    print(ans)
if __name__ == '__main__':
    main()

# -*-coding: utf-8 -*-
def check(A, h, w):
    cnt = 0
    if A[h-1][w] == "#":
        cnt += 1
    if A[h][w-1] == "#":
        cnt += 1
    if A[h][w+1] == "#":
        cnt += 1
    if A[h+1][w] == "#":
        cnt += 1
    return cnt

def main():
    H, W = map(int, input().split())
    A = ["." * (W+2)]
    for i in range(H):
        A.append("." + input() + ".")
    A.append("." * (W + 2))
    #print(A)
    ans = "Possible"
    for w in range(1, W+1):
        for h in range(1, H+1):
            if A[h][w] == "#":
                if w == h == 1:
                    pass
                elif h == H and w == W:
                    pass
                elif check(A, h, w) == 2:
                    if A[h-1][w] == A[h+1][w] == "
                        pass
                    elif A[h-1][w] == A[h+1][w] == "." and A[h][w-1] == A[h][w+1] == "
                        pass
                    elif A[h-1][w] == A[h][w+1] == "#" and A[h+1][w] == A[h][w-1] == ".":   # L字
                        pass
                    elif A[h+1][w] == A[h][w-1] == "#" and A[h-1][w] == A[h][w+1] == ".":   # L時
                        pass
                    else:
                        ans = "Impossible"
                else:
                    ans = "Impossible"
    print(ans)
if __name__ == '__main__':
    main()

H, W = map(int, input().split())
A = [input()+"." for _ in range(H)]
A.append("."*(W+1))
ans = "Possible"
for w in range(W):
    for h in range(H):
        if A[h][w] == "#":
            if A[h-1][w] == "#":
                ans = "Impossible"
                break
            elif A[h][w+1] == "#" and A[h+1][w] == ".":
                pass
            elif A[h][w+1] == "." and A[h+1][w] == "#":
                pass
            elif h == H-1 and w == W-1:
                break
            else:
                ans = "Impossible"
                break
print(ans)

H, W = map(int, input().split())
A = [input()+"." for _ in range(H)]
A.append("."*(W+1))
ans = "Possible"
for w in range(W):
    for h in range(H):
        if A[h][w] == "#":
            if A[h+1][w] == "#":
                ans = "Impossible"
                break
            elif A[h][w+1] == "#" and A[h+1][w] == ".":
                pass
            elif A[h][w+1] == "." and A[h+1][w] == "#":
                pass
            elif h == H-1 and w == W-1:
                break
            else:
                ans = "Impossible"
                break
print(ans)

H, W = map(int, input().split())
A = [input() for _ in range(H)]
cnt = 0
for h in range(H):
    for w in range(W):
        if A[h][w] == "#":
            cnt += 1
if cnt == H + W -1:
    print("Possible")
else:
    print("Impossible")

Verdicts: Wrong Answer

Wrong Answer

Wrong Answer

Wrong Answer

Accepted