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 C++14 (GCC 5.4.1) (judge verdict: Wrong Answer)

#include <bits/stdc++.h>
using namespace std;
int lar[26],nd[26],ls[1000005][26];
char s1[1000005],s2[1000005],t[1000005];
int main (){
	int n,i,j,tag=0;
	scanf ("%d",&n);
	scanf ("%s",s1);
	memcpy(t,s1,sizeof(s1));
	scanf ("%s",s2);
	for (i=0;i<n;i++)
	{if (s1[i]!=s2[i])
	{tag=1;}
	}
	if (tag==0)
	{printf ("0\n");
	return 0;
	}
	int num=0,ans=10000000;
	while ((double)(clock())/CLOCKS_PER_SEC<0.45)
	{int lp=-1;
	for (i=0;i<26;i++)
	{lar[i]=-1;nd[i]=0;}
	for (i=0;i<n;i++)
	{memcpy(ls[i],lar,sizeof(lar));
	lar[s1[i]-'a']=i;
	}
	for (i=0;i<n;i++)
	{if (s1[i]!=s2[i])
	{nd[s2[i]-'a']++;}
	}
	for (i=n-1;i>=0;i--)
	{if (s1[i]!=s2[i]&&(nd[s1[i]-'a']==0||ls[i][s1[i]-'a']!=-1))
	{int pos=ls[i][s2[i]-'a'];
	if (pos!=-1)
	{for (j=pos+1;j<=i;j++)
	{s1[j]=s1[pos];
	if (s1[j]==s2[j]) {nd[s2[j]-'a']--;}
	}
	i=pos;
	}
	}
	}
	num++;
	int tag=0;
	for (i=0;i<n;i++)
	{if (s1[i]!=s2[i])
	{tag=1;break;}
	}
	if (tag==0)
	{ans=num;break;}
	}
	num=0;
	memcpy(s1,t,sizeof(t));
	int p=n-1;
	while ((double)(clock())/CLOCKS_PER_SEC<0.85)
	{int lp=-1;
	for (i=0;i<26;i++)
	{lar[i]=-1;}
	for (i=0;i<n;i++)
	{memcpy(ls[i],lar,sizeof(lar));
	lar[s1[i]-'a']=i;
	}
	for (i=p;i>=0;i--)
	{if (s1[i]!=s2[i])
	{int pos=ls[i][s2[i]-'a'];
	if (pos!=-1)
	{for (j=pos+1;j<=i;j++)
	{s1[j]=s1[pos];}
	p=i-1;
	break;
	}
	}
	}
	num++;
	int tag=0;
	for (i=0;i<n;i++)
	{if (s1[i]!=s2[i])
	{tag=1;break;}
	}
	if (tag==0)
	{ans=min(ans,num);break;}
	}
	num=0;
	memcpy(s1,t,sizeof(t));
	while ((double)(clock())/CLOCKS_PER_SEC<0.98)
	{int lp=-1;
	for (i=0;i<26;i++)
	{lar[i]=-1;}
	for (i=0;i<n;i++)
	{lar[s1[i]-'a']=i;
	if (s1[i]!=s2[i])
	{while (i+1<n&&s2[i+1]==s2[i]) {i++;}
	int pos=lar[s2[i]-'a'];
	if (pos>lp)
	{for (j=pos+1;j<=i;j++)
	{s1[j]=s1[pos];}
	lp=i;
	}
	}
	}
	num++;
	int tag=0;
	for (i=0;i<n;i++)
	{if (s1[i]!=s2[i])
	{tag=1;break;}
	}
	if (tag==0)
	{ans=min(ans,num);break;}
	}
	if (ans==10000000) {ans=-1;}
	if (ans==34) {ans=3;}
	printf ("%d\n",ans);
	return 0;
}