Problem:
Score : 200 points

Problem Statement

#nck {
width: 30px;
height: auto;
}

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.

Constraints

2 \leq H, W \leq 8

a_{i,j} is either # or ..

There exists a valid sequence of moves for Shik to generate the map a.

Input

The input is given from Standard Input in the following format:

H W
a_{11}a_{12}...a_{1W}
:
a_{H1}a_{H2}...a_{HW}

Output

If it is possible that Shik only uses right and down moves, print Possible. Otherwise, print Impossible.

Sample Input 1

4 5
##...
.##..
..##.
...##

Sample Output 1

Possible

The matrix can be generated by a 7-move sequence: right, down, right, down, right, down, and right.

Sample Input 2

5 3
###
..#
###
#..
###

Sample Output 2

Impossible

Sample Input 3

4 5
##...
.###.
.###.
...##

Sample Output 3

Impossible

Solution in Java8 (OpenJDK 1.8.0) (judge verdict: Accepted)

import java.io.*;
import java.math.*;
import java.util.*;

public class Main {
  static int mod = (int) 1e9 + 7;
  public static void main(String[] args) throws FileNotFoundException {
    FasterScanner s = new FasterScanner();
    PrintWriter out = new PrintWriter(System.out);
    int test = 1;
    
    while (test-- > 0) {
      int n = s.nextInt();
      int m = s.nextInt();
      boolean[][] b = new boolean[n][m];
      for(int i = 0; i < n; i++) {
        char[] x = s.nextLine().toCharArray();
        for(int j = 0; j< m; j++)
          b[i][j] = x[j] == '#';
      }
      boolean[][] visited = new boolean[n][m];
      int curx = 0 , cury = 0;
      while(true) {
        visited[curx][cury] = true;
        if(curx < n - 1 && b[curx + 1][cury]) {
          curx++;
        }
        else if(cury < m - 1 && b[curx][cury + 1]) {
          cury++;
        }
        else{
          break;
        }
      }
      for(int i = 0; i < n; i++) {
        for(int j = 0;j < m; j++) {
          if(!visited[i][j] && b[i][j]) {
            System.out.println("Impossible");
            return;
          }
        }
      }
      System.out.println("Possible");
    }
    

    out.close();
  }

  public static long pow(long x, long n, long mod) {
    long res = 1;
    for (long p = x; n > 0; n >>= 1, p = (p * p) % mod) {
      if ((n & 1) != 0) {
        res = (res * p % mod);
      }
    }
    return res;
  }

  static long gcd(long n1, long n2) {
    long r;
    while (n2 != 0) {
      r = n1 % n2;
      n1 = n2;
      n2 = r;
    }
    return n1;
  }

  static class FasterScanner {
    private byte[] buf = new byte[1024];
    private int curChar;
    private int snumChars;

    public int read() {
      if (snumChars == -1)
        throw new InputMismatchException();
      if (curChar >= snumChars) {
        curChar = 0;
        try {
          snumChars = System.in.read(buf);
        } catch (IOException e) {
          throw new InputMismatchException();
        }
        if (snumChars <= 0)
          return -1;
      }
      return buf[curChar++];
    }

    public double nextDouble() {
      return Double.parseDouble(this.nextString());
    }

    public String nextLine() {
      int c = read();
      while (isSpaceChar(c))
        c = read();
      StringBuilder res = new StringBuilder();
      do {
        res.appendCodePoint(c);
        c = read();
      } while (!isEndOfLine(c));
      return res.toString();
    }

    public String nextString() {
      int c = read();
      while (isSpaceChar(c))
        c = read();
      StringBuilder res = new StringBuilder();
      do {
        res.appendCodePoint(c);
        c = read();
      } while (!isSpaceChar(c));
      return res.toString();
    }

    public long nextLong() {
      int c = read();
      while (isSpaceChar(c))
        c = read();
      int sgn = 1;
      if (c == '-') {
        sgn = -1;
        c = read();
      }
      long res = 0;
      do {
        if (c < '0' || c > '9')
          throw new InputMismatchException();
        res *= 10;
        res += c - '0';
        c = read();
      } while (!isSpaceChar(c));
      return res * sgn;
    }

    public int nextInt() {
      int c = read();
      while (isSpaceChar(c))
        c = read();
      int sgn = 1;
      if (c == '-') {
        sgn = -1;
        c = read();
      }
      int res = 0;
      do {
        if (c < '0' || c > '9')
          throw new InputMismatchException();
        res *= 10;
        res += c - '0';
        c = read();
      } while (!isSpaceChar(c));
      return res * sgn;
    }

    public int[] nextIntArray(int n) {
      int[] arr = new int[n];
      for (int i = 0; i < n; i++) {
        arr[i] = nextInt();
      }
      return arr;
    }

    public long[] nextLongArray(int n) {
      long[] arr = new long[n];
      for (int i = 0; i < n; i++) {
        arr[i] = nextLong();
      }
      return arr;
    }

    private boolean isSpaceChar(int c) {
      return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
    }

    private boolean isEndOfLine(int c) {
      return c == '\n' || c == '\r' || c == -1;
    }
  }
}