import java.util.*;
/**
* Created by Shreyans on 4/30/2015 at 10:27 PM using IntelliJ IDEA
*/
class MAZE
{
static int r,c,s1,s2,f1,f2;//Rows, Columns, Start Coordinates, Finish Coordinates
static int[] dx={1,-1,0,0};//right, left, NA, NA
static int[] dy={0,0,1,-1};//NA, NA, bottom, top
static char[][] grid;//Main grid
public static void main
(String[] args
) {
Scanner sc
=new Scanner
(System.
in);//I suggest using faster IO if you have performance concerns. I did. Scanner is readable hence the choice r=sc.nextInt();
c=sc.nextInt();
grid=new char[r][c];
for(int i=0;i<r;i++)
{
char[] s1=sc.next().toCharArray();//Reading a line of the Grid
System.
arraycopy(s1,
0,grid
[i
],
0,c
);//Nice inbuilt function to copy contents of an array. Also doable manually }
s1=sc.nextInt()-1;
s2=sc.nextInt()-1;
f1=sc.nextInt()-1;
f2=sc.nextInt()-1;
if(MAZEBFS())
{
System.
out.
println("PATH EXISTS"); }
else
{
System.
out.
println("PATH DOES NOT EXIST"); }
}
private static boolean MAZEBFS()
{
if(s1==f1&&s2==f2)
{
return true;//He's already there
}
else
{
grid [f1][f2]='G';//finish
Queue<int[]> q=new LinkedList<int[]>();
int[]start={s1,s2};//Start Coordinates
q.add(start);//Adding start to the queue since we're already visiting it
grid[s1][s2]='B';
while(q.peek()!=null)
{
int[]curr=q.poll();//poll or remove. Same thing
for(int i=0;i<4;i++)//for each direction
{
if((curr[0]+dx[i]>=0&&curr[0]+dx[i]<r)&&(curr[1]+dy[i]>=0&&curr[1]+dy[i]<c))
{
//Checked if x and y are correct. ALL IN 1 GO
int xc=curr[0]+dx[i];//Setting current x coordinate
int yc=curr[1]+dy[i];//Setting current y coordinate
if(grid[xc][yc]=='G')//Destination found
{
//System.out.println(xc+" "+yc);
return true;
}
else if(grid[xc][yc]=='E')//Movable. Can't return here again so setting it to 'B' now
{
//System.out.println(xc+" "+yc);
grid[xc][yc]='B';//now BLOCKED
int[]temp={xc,yc};
q.add(temp);//Adding current coordinates to the queue
}
}
}
}
return false;//Will return false if no route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4gYXJyYXkuIEFsc28gZG9hYmxlIG1hbnVhbGx5CiAgICAgICAgfQogICAgICAgIHMxPXNjLm5leHRJbnQoKS0xOwogICAgICAgIHMyPXNjLm5leHRJbnQoKS0xOwogICAgICAgIGYxPXNjLm5leHRJbnQoKS0xOwogICAgICAgIGYyPXNjLm5leHRJbnQoKS0xOwogICAgICAgIGlmKE1BWkVCRlMoKSkKICAgICAgICB7CiAgICAgICAgICAgIFN5c3RlbS5vdXQucHJpbnRsbigiUEFUSCBFWElTVFMiKTsKICAgICAgICB9CiAgICAgICAgZWxzZQogICAgICAgIHsKICAgICAgICAgICAgU3lzdGVtLm91dC5wcmludGxuKCJQQVRIIERPRVMgTk9UIEVYSVNUIik7CiAgICAgICAgfQogICAgfQogICAgcHJpdmF0ZSBzdGF0aWMgYm9vbGVhbiBNQVpFQkZTKCkKICAgIHsKICAgICAgICBpZihzMT09ZjEmJnMyPT1mMikKICAgICAgICB7CiAgICAgICAgICAgIHJldHVybiB0cnVlOy8vSGUncyBhbHJlYWR5IHRoZXJlCiAgICAgICAgfQogICAgICAgIGVsc2UKICAgICAgICB7CiAgICAgICAgICAgIGdyaWQgW2YxXVtmMl09J0cnOy8vZmluaXNoCiAgICAgICAgICAgIFF1ZXVlPGludFtdPiBxPW5ldyBMaW5rZWRMaXN0PGludFtdPigpOwogICAgICAgICAgICBpbnRbXXN0YXJ0PXtzMSxzMn07Ly9TdGFydCBDb29yZGluYXRlcwogICAgICAgICAgICBxLmFkZChzdGFydCk7Ly9BZGRpbmcgc3RhcnQgdG8gdGhlIHF1ZXVlIHNpbmNlIHdlJ3JlIGFscmVhZHkgdmlzaXRpbmcgaXQKICAgICAgICAgICAgZ3JpZFtzMV1bczJdPSdCJzsKICAgICAgICAgICAgd2hpbGUocS5wZWVrKCkhPW51bGwpCiAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgIGludFtdY3Vycj1xLnBvbGwoKTsvL3BvbGwgb3IgcmVtb3ZlLiBTYW1lIHRoaW5nCiAgICAgICAgICAgICAgICBmb3IoaW50IGk9MDtpPDQ7aSsrKS8vZm9yIGVhY2ggZGlyZWN0aW9uCiAgICAgICAgICAgICAgICB7CiAgICAgICAgICAgICAgICAgICAgaWYoKGN1cnJbMF0rZHhbaV0+PTAmJmN1cnJbMF0rZHhbaV08cikmJihjdXJyWzFdK2R5W2ldPj0wJiZjdXJyWzFdK2R5W2ldPGMpKQogICAgICAgICAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgICAgICAgICAgLy9DaGVja2VkIGlmIHggYW5kIHkgYXJlIGNvcnJlY3QuIEFMTCBJTiAxIEdPCiAgICAgICAgICAgICAgICAgICAgICAgIGludCB4Yz1jdXJyWzBdK2R4W2ldOy8vU2V0dGluZyBjdXJyZW50IHggY29vcmRpbmF0ZQogICAgICAgICAgICAgICAgICAgICAgICBpbnQgeWM9Y3VyclsxXStkeVtpXTsvL1NldHRpbmcgY3VycmVudCB5IGNvb3JkaW5hdGUKICAgICAgICAgICAgICAgICAgICAgICAgaWYoZ3JpZFt4Y11beWNdPT0nRycpLy9EZXN0aW5hdGlvbiBmb3VuZAogICAgICAgICAgICAgICAgICAgICAgICB7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAvL1N5c3RlbS5vdXQucHJpbnRsbih4YysiICIreWMpOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgcmV0dXJuIHRydWU7CiAgICAgICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICAgICAgZWxzZSBpZihncmlkW3hjXVt5Y109PSdFJykvL01vdmFibGUuIENhbid0IHJldHVybiBoZXJlIGFnYWluIHNvIHNldHRpbmcgaXQgdG8gJ0InIG5vdwogICAgICAgICAgICAgICAgICAgICAgICB7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAvL1N5c3RlbS5vdXQucHJpbnRsbih4YysiICIreWMpOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgZ3JpZFt4Y11beWNdPSdCJzsvL25vdyBCTE9DS0VECiAgICAgICAgICAgICAgICAgICAgICAgICAgICBpbnRbXXRlbXA9e3hjLHljfTsKICAgICAgICAgICAgICAgICAgICAgICAgICAgIHEuYWRkKHRlbXApOy8vQWRkaW5nIGN1cnJlbnQgY29vcmRpbmF0ZXMgdG8gdGhlIHF1ZXVlCiAgICAgICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgIH0KICAgICAgICAgICAgcmV0dXJuIGZhbHNlOy8vV2lsbCByZXR1cm4gZmFsc2UgaWYgbm8gcm91dGUgcG9zc2libGUKICAgICAgICB9CiAgICB9Cn0=