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 statistics import median
#import collections

from fractions import gcd
from itertools import combinations 
from collections import deque
from collections import defaultdict
import bisect
#
#    d = m - k[i] - k[j]

#
#
#

#
#

import sys
sys.setrecursionlimit(10000000)
mod = 10**9 + 7

def readInts():
  return list(map(int,input().split()))
def main():
    H,W = readInts()
    FIELD = [input() for _ in range(H)]
    ARRIVED = [[False]*W for _ in range(H)]
    def Move(y,x):
        ARRIVED[y][x] = True
        if y == H-1 and x == W-1:
            print('Possible')
            exit()
        y_ = True
        x_ = True
        if y+1 < H:
            y_ = True
        else:
            y_ = False
        if x+1 < W:
            x_ = True
        else:
            x_ = False
        xu = True if x - 1 >= 0 else False
        yu = True if y - 1 >= 0 else False
        if y_ and FIELD[y+1][x] == '#':
            if x_ and FIELD[y][x+1] == '#':
                print('Impossible')
                exit()
            elif yu and FIELD[y-1][x] == '#' and not ARRIVED[y-1][x]:
                print(y-1,x)
                print('Impossible')
                exit()
            else:
                Move(y+1,x)
        elif x_ and FIELD[y][x+1] == '#':
            if y_ and FIELD[y+1][x] == '#':
                print('Impossible')
                exit()
            elif xu and FIELD[y][x-1] == '#'  and not ARRIVED[y][x-1]:
                print(y,x-1)
                print('Impossible')
                exit()
            else:
                Move(y,x+1)
        else:
            print('Impossible')
            exit()
    
    Move(0,0)
if __name__ == '__main__':
  main()


from statistics import median
#import collections

from fractions import gcd
from itertools import combinations 
from collections import deque
from collections import defaultdict
import bisect
#
#    d = m - k[i] - k[j]

#
#
#

#
#

import sys
sys.setrecursionlimit(10000000)
mod = 10**9 + 7

def readInts():
  return list(map(int,input().split()))
def main():
    H,W = readInts()
    FIELD = [input() for _ in range(H)]
    ARRIVED = [[False]*W for _ in range(H)]
    def Move(y,x):
        ARRIVED[y][x] = True
        if y == H-1 and x == W-1:
            print('Possible')
            exit()
        y_ = True
        x_ = True
        if y+1 < H:
            y_ = True
        else:
            y_ = False
        if x+1 < W:
            x_ = True
        else:
            x_ = False
        xu = True if x - 1 >= 0 else False
        yu = True if y - 1 >= 0 else False
        if y_ and FIELD[y+1][x] == '#':
            if x_ and FIELD[y][x+1] == '#':
                print('Impossible')
                exit()
            elif xu and FIELD[y][x-1] == '#' and not ARRIVED[y][x-1]:
                print('Impossible')
                exit()
            else:
                Move(y+1,x)
        elif x_ and FIELD[y][x+1] == '#':
            if y_ and FIELD[y+1][x] == '#':
                print('Impossible')
                exit()
            elif yu and FIELD[y-1][x] == '#'  and not ARRIVED[y-1][x]:
                print('Impossible')
                exit()
            else:
                Move(y,x+1)
        else:
            print('Impossible')
            exit()
    
    Move(0,0)
if __name__ == '__main__':
  main()


from statistics import median
#import collections

from fractions import gcd
from itertools import combinations 
from collections import deque
from collections import defaultdict
import bisect
#
#    d = m - k[i] - k[j]

#
#
#

#
#

import sys
sys.setrecursionlimit(10000000)
mod = 10**9 + 7

def readInts():
  return list(map(int,input().split()))
def main():
    H,W = readInts()
    FIELD = [input() for _ in range(H)]
    ARRIVED = [[False]*W for _ in range(H)]
    def Move(y,x):
        ARRIVED[y][x] = True
        if y == H-1 and x == W-1:
            print('Possible')
            exit()
        y_ = True
        x_ = True
        if y+1 < H:
            y_ = True
        else:
            y_ = False
        if x+1 < W:
            x_ = True
        else:
            x_ = False
        xu = True if x - 1 >= 0 else False
        yu = True if y - 1 >= 0 else False
        if y_ and FIELD[y+1][x] == '#':
            if x_ and FIELD[y][x+1] == '#':
                print('Impossible')
                exit()
            elif xu and FIELD[y-1][x] == '#' and not ARRIVED[y-1][x]:
                print('Impossible')
                exit()
            else:
                Move(y+1,x)
        elif x_ and FIELD[y][x+1] == '#':
            if y_ and FIELD[y+1][x] == '#':
                print('Impossible')
                exit()
            elif yu and FIELD[y][x-1] == '#'  and not ARRIVED[y][x-1]:
                print('Impossible')
                exit()
            else:
                Move(y,x+1)
        else:
            print('Impossible')
            exit()
    
    Move(0,0)
if __name__ == '__main__':
  main()


from statistics import median
#import collections

from fractions import gcd
from itertools import combinations 
from collections import deque
from collections import defaultdict
import bisect
#
#    d = m - k[i] - k[j]

#
#
#

#
#

import sys
sys.setrecursionlimit(10000000)
mod = 10**9 + 7

def readInts():
  return list(map(int,input().split()))
def main():
    H,W = readInts()
    FIELD = [input() for _ in range(H)]
    def Move(y,x):
        if y == H-1 and x == W-1:
            print('Possible')
            exit()
        y_ = True
        x_ = True
        if y+1 < H:
            y_ = True
        else:
            y_ = False
        if x+1 < W:
            x_ = True
        else:
            x_ = False
        if y_ and FIELD[y+1][x] == '#':
            if x_ and FIELD[y][x+1] == '#':
                print('Impossible')
                exit()
            else:
                Move(y+1,x)
        elif x_ and FIELD[y][x+1] == '#':
            if y_ and FIELD[y+1][x] == '#':
                print('Impossible')
                exit()
            else:
                Move(y,x+1)
        else:
            print('Impossible')
            exit()
    
    Move(0,0)
if __name__ == '__main__':
  main()


from statistics import median
#import collections

from fractions import gcd
from itertools import combinations 
from collections import deque
from collections import defaultdict
import bisect
#
#    d = m - k[i] - k[j]

#
#
#

#
#

import sys
sys.setrecursionlimit(10000000)
mod = 10**9 + 7

def readInts():
  return list(map(int,input().split()))
def main():
    H,W = readInts()
    FIELD = [input() for _ in range(H)]
    ARRIVED = [[False]*W for _ in range(H)]
    def Move(y,x):
        ARRIVED[y][x] = True
        if y == H-1 and x == W-1:
            print('Possible')
            exit()
        y_ = True
        x_ = True
        if y+1 < H:
            y_ = True
        else:
            y_ = False
        if x+1 < W:
            x_ = True
        else:
            x_ = False
        xu = True if x - 1 >= 0 else False
        yu = True if y - 1 >= 0 else False
        if y_ and FIELD[y+1][x] == '#':
            if x_ and FIELD[y][x+1] == '#':
                print('Impossible')
                exit()
            elif yu and FIELD[y-1][x] == '#' and not ARRIVED[y-1][x]:
                #print(y-1,x)
                print('Impossible')
                exit()
            else:
                Move(y+1,x)
        elif x_ and FIELD[y][x+1] == '#':
            if y_ and FIELD[y+1][x] == '#':
                print('Impossible')
                exit()
            elif xu and FIELD[y][x-1] == '#'  and not ARRIVED[y][x-1]:
                #print(y,x-1)
                print('Impossible')
                exit()
            else:
                Move(y,x+1)
        else:
            print('Impossible')
            exit()
    
    Move(0,0)
if __name__ == '__main__':
  main()


#from statistics import median
#import collections

#from fractions import gcd
#from itertools import combinations # (string,3) 3回
#from collections import deque
from collections import defaultdict

#
#    d = m - k[i] - k[j]

#
#
#

#
#

import sys
sys.setrecursionlimit(10000000)
mod = 10**9 + 7
#mod = 9982443453
def readInts():
  return list(map(int,input().split()))
def I():
  return int(input())
h,w = readInts()
A = [input() for _ in range(h)]
cnt = 0
for y in range(h):
    for x in range(w):
        if A[y][x] == '#':
            cnt += 1
if h + w - 1 == cnt:
    print('Possible')
else:
    print('Impossible')


Verdicts: Wrong Answer

Wrong Answer

Wrong Answer

Wrong Answer

Wrong Answer

Accepted