2015
05-23

A robot has been sent to explore a remote planet. To specify a path the robot should take, a program is sent each day. The program consists of a sequence of the following commands:
FORWARD X: move forward by X units.
TURN LEFT: turn left (in place) by 90 degrees.
TURN RIGHT: turn right (in place) by 90 degrees.
The robot also has sensor units which allow it to obtain a map of its surrounding area. The map is represented as a grid. Some grid points contain hazards (e.g. craters) and the program must avoid these points or risk losing the robot.
Naturally, if the initial location of the robot, the direction it is facing, and its destination position are known, it is best to send the shortest program (one consisting of the fewest commands) to move the robot to its destination (we do not care which direction it faces at the destination). You are more interested in knowing the number of different shortest programs that can move the robot to its destination. However, the number of shortest programs can be very large, so you are satisfied to compute the number as a remainder modulo 1,000,000.

There will be several test cases in the input. Each test case will begin with a line with two integers
N M
Where N is the number of rows in the grid, and M is the number of columns in the grid (2 ≤ N, M ≤ 100). The next N lines of input will have M characters each. The characters will be one of the following:
‘.’ Indicating a navigable grid point.
‘*’ Indicating a crater (i.e. a non-navigable grid point).
‘X’ Indicating the target grid point. There will be exactly one ‘X’.
‘N’, ‘E’, ‘S’, or ‘W’ Indicating the starting point and initial heading of the robot. There will be exactly one of these. Note that the directions mirror compass directions on a map: N is North (toward the top of the grid), E is East (toward the right of the grid), S is South (toward the bottom of the grid) and W is West (toward the left of the grid).
There will be no spaces and no other characters in the description of the map. The input will end with a line with two 0s.

There will be several test cases in the input. Each test case will begin with a line with two integers
N M
Where N is the number of rows in the grid, and M is the number of columns in the grid (2 ≤ N, M ≤ 100). The next N lines of input will have M characters each. The characters will be one of the following:
‘.’ Indicating a navigable grid point.
‘*’ Indicating a crater (i.e. a non-navigable grid point).
‘X’ Indicating the target grid point. There will be exactly one ‘X’.
‘N’, ‘E’, ‘S’, or ‘W’ Indicating the starting point and initial heading of the robot. There will be exactly one of these. Note that the directions mirror compass directions on a map: N is North (toward the top of the grid), E is East (toward the right of the grid), S is South (toward the bottom of the grid) and W is West (toward the left of the grid).
There will be no spaces and no other characters in the description of the map. The input will end with a line with two 0s.

5 6
*....X
.....*
.....*
.....*
N....*
6 5
....X
.****
.****
.****
.****
N****
3 3
.E.
***
.X.
0 0

6 4
3 1
0 0

/*

bfs水题，配合记忆搜索。
这两天总是犯白痴错误，想当然的写了个东西，然后就只能de了会儿bug。。警示。。。

2013-07-30
*/

#include"iostream"
#include"cstdio"
#include"queue"
#include"cstring"
using namespace std;
const int N=106;
const int mod=1000000;
const int inf=123456789;

int n,m,s_x,s_y,s_face,e_x,e_y;
int step[N][N][4],cnt[N][N][4],flag[N][N][4];
char map[N][N];
int dir[4][2]={-1,0, 0,-1, 1,0, 0,1};

struct node{
int x,y,step,face;
};
void bfs()
{
int i;
node now,next;
queue<node>q;

now.x=s_x;
now.y=s_y;
now.step=0;
now.face=s_face;
step[now.x][now.y][now.face]=0;
cnt[now.x][now.y][now.face]=1;
memset(flag,0,sizeof(flag));
q.push(now);

while(!q.empty())
{
now=q.front();
q.pop();
if(flag[now.x][now.y][now.face])    continue;
flag[now.x][now.y][now.face]=1;
for(i=0;i<4;i++)    if(step[now.x][now.y][now.step]<=step[e_x][e_y][i])    break;
if(i>=4)    return ;
//the same direction
next.x=now.x+dir[now.face][0];
next.y=now.y+dir[now.face][1];
next.step=now.step+1;
next.face=now.face;
while(0<=next.x && next.x<n && 0<=next.y && next.y<m && map[next.x][next.y]!='*')
{
if(next.step<step[next.x][next.y][next.face])
{
step[next.x][next.y][next.face]=next.step;
cnt[next.x][next.y][next.face]=cnt[now.x][now.y][now.face]%mod;
q.push(next);
}
else if(next.step==step[next.x][next.y][next.face])
{
cnt[next.x][next.y][next.face]=(cnt[next.x][next.y][next.face]+cnt[now.x][now.y][now.face])%mod;
q.push(next);
}
next.x+=dir[now.face][0];
next.y+=dir[now.face][1];
}
//turn
next.x=now.x;
next.y=now.y;
next.step=now.step+1;
next.face=(now.face+1)%4;
if(next.step<step[next.x][next.y][next.face])
{
step[next.x][next.y][next.face]=next.step;
cnt[next.x][next.y][next.face]=cnt[now.x][now.y][now.face];
q.push(next);
}
else if(next.step==step[next.x][next.y][next.face])
{
cnt[next.x][next.y][next.face]=(cnt[next.x][next.y][next.face]+cnt[now.x][now.y][now.face])%mod;
q.push(next);
}

next.step=now.step+1;
next.face=(now.face+3)%4;
if(next.step<step[next.x][next.y][next.face])
{
step[next.x][next.y][next.face]=next.step;
cnt[next.x][next.y][next.face]=cnt[now.x][now.y][now.face];
q.push(next);
}
else if(next.step==step[next.x][next.y][next.face])
{
cnt[next.x][next.y][next.face]=(cnt[next.x][next.y][next.face]+cnt[now.x][now.y][now.face])%mod;
q.push(next);
}
}
}
int main()
{
int i,l,j;
while(scanf("%d%d",&n,&m),n||m)
{
for(i=0;i<n;i++)
{
scanf("%s",map[i]);
for(l=0;l<m;l++)
{
if(map[i][l]=='X')    {e_x=i;e_y=l;}
if(map[i][l]=='N')    {s_x=i;s_y=l;s_face=0;}
else if(map[i][l]=='W')    {s_x=i;s_y=l;s_face=1;}
else if(map[i][l]=='S')    {s_x=i;s_y=l;s_face=2;}
else if(map[i][l]=='E')    {s_x=i;s_y=l;s_face=3;}
for(j=0;j<4;j++)    step[i][l][j]=inf;
}
}
map[s_x][s_y]='.';
memset(cnt,0,sizeof(cnt));
bfs();
int ans=0,minstep=inf;
for(i=0;i<4;i++)    if(step[e_x][e_y][i]<minstep)    minstep=step[e_x][e_y][i];
for(i=0;i<4;i++)    if(step[e_x][e_y][i]==minstep)    ans=(ans+cnt[e_x][e_y][i])%mod;
if(minstep==inf)    printf("0 0\n");
else    printf("%d %d\n",minstep,ans);
}
return 0;
}