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):
from collections import deque
h, w = map(int, input().split())
field = []
check = [[False for _ in range(w)] for _ in range(h)]
for i in range(h):
    s = list(input().strip())
    field.append(s)

sx, sy = 0, 0
d = deque()
dirx = [0, 1, 0, -1]
diry = [1, 0, -1, 0]
d.append([sx, sy])

while (d):
    nowx, nowy = d.popleft()
    check[nowx][nowy] = True
    for x, y in zip(dirx, diry):
        nx = nowx + x
        ny = nowy + y
        if nx in range(h) and ny in range(w) and (
                not check[nx][ny]) and field[nx][ny] == '#':
            d.append([nx, ny])
for i in range(h):
    for j in range(w):
        if not check[i][j] and field[i][j] == '#':
            print('Impossible')
            exit()


d.append([sx, sy])
check = [[False for _ in range(w)] for _ in range(h)]
while (d):
    nowx, nowy = d.pop()
    for x, y in zip(dirx[0:2], diry[0:2]):
        nx = nowx + x
        ny = nowy + y
        if nx in range(h) and ny in range(w) and field[nx][ny] == '#':
            d.append([nx, ny])
            check[nx][ny] = True
    for x, y in zip(dirx[2:4], diry[2:4]):
        nx = nowx + x
        ny = nowy + y
        if nx in range(h) and ny in range(
                w) and field[nx][ny] == '#' and not check[nx][ny]:
            print('Impossible')
            exit()

    if len(d) == 2:
        print('Impossible')
        exit()
    if nowx == h - 1 and nowy == w - 1:
        print('Possible')
        exit()

print('Impossible')


from collections import deque
h, w = map(int, input().split())
field = []
check = [[False for _ in range(w)] for _ in range(h)]
for i in range(h):
    s = list(input().strip())
    field.append(s)

sx, sy = 0, 0
d = deque()
dirx = [0, 1, 0, -1]
diry = [1, 0, -1, 0]
d.append([sx, sy])

while (d):
    nowx, nowy = d.popleft()
    check[nowx][nowy] = True
    for x, y in zip(dirx, diry):
        nx = nowx + x
        ny = nowy + y
        if nx in range(h) and ny in range(w) and (
                not check[nx][ny]) and field[nx][ny] == '#':
            d.append([nx, ny])
for i in range(h):
    for j in range(w):
        if not check[i][j] and field[i][j] == '#':
            print('Impossible')
            exit()


d.append([sx, sy])
check = [[False for _ in range(w)] for _ in range(h)]
while (d):
    nowx, nowy = d.pop()
    check[nowx][nowy] = True

    for x, y in zip(dirx[0:2], diry[0:2]):
        nx = nowx + x
        ny = nowy + y
        if nx in range(h) and ny in range(w) and field[nx][ny] == '#':
            d.append([nx, ny])
    for x, y in zip(dirx[2:4], diry[2:4]):
        nx = nowx + x
        ny = nowy + y
        if nx in range(h) and ny in range(
                w) and field[nx][ny] == '#' and not check[nx][ny]:
            print('Impossible')
            exit()

    if len(d) == 2:
        print('Impossible')
        exit()
    if nowx == h - 1 and nowy == w - 1:
        print('Possible')
        exit()

print('Impossible')


Verdicts: Wrong Answer

Accepted