#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>
#include<iomanip>
#include<stack>
#include<map>
#include<set>
#include<cmath>
#define debug(x) cerr<<#x<<"="<<x<<endl
#define INF 0x7f7f7f7f
#define llINF 0x7ffffffffffll
using namespace std;
typedef pair<int,int> pii;
typedef long long ll;
inline int init()
{
int now=0,ju=1;char c;bool flag=false;
while(1)
{
c=getchar();
if(ju==‘-‘)ju=-1;
else if(c>=‘0‘&&c<=‘9‘)
{
now=now*10+c-‘0‘;
flag=true;
}
else if(flag)return now*ju;
}
}
ll dis[151][151][1005];
int n,m;
ll ans=llINF;
bool vis[151][151][1005];
int A[151][151];
int B[151][151];
struct node
{
int x,y,z;
ll dis;
};
bool operator < (node a,node b)
{
if(a.dis!=b.dis)
{
return a.dis>b.dis;
}
}
int topt;
char where;
#ifdef unix
#define LLD "%lld"
#else
#define LLD "%I64d"
#endif
int xx,xy,yx,yy,zx,zy;
priority_queue<node> q;
int dx[5]={1,-1,0,0,0};
int dy[5]={0,0,1,-1,0};
void dijkstra(int x,int y)
{
node now;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
for(int k=0;k<=topt;k++)
{
dis[i][j][k]=llINF;
vis[i][j][k]=false;
}
}
}
dis[x][y][0]=0;
now.x=x;now.y=y;now.z=0;now.dis=0;
q.push(now);
while(!q.empty())
{
now=q.top();q.pop();
if(vis[now.x][now.y][now.z])continue;
if(vis[xx][xy][0]+vis[yx][yy][0]+vis[zx][zy][0]==3)break;
vis[now.x][now.y][now.z]=1;
if(now.z>0)
{
for(int i=0;i<5;i++)
{
if(!vis[now.x+dx[i]][now.y+dy[i]][now.z-1]&&now.x+dx[i]>=1&&now.x+dx[i]<=n&&now.y+dy[i]>=1&&now.y+dy[i]<=m)
{
if(dis[now.x+dx[i]][now.y+dy[i]][now.z-1]>dis[now.x][now.y][now.z])
{
dis[now.x+dx[i]][now.y+dy[i]][now.z-1]=dis[now.x][now.y][now.z];
q.push((node){now.x+dx[i],now.y+dy[i],now.z-1,dis[now.x+dx[i]][now.y+dy[i]][now.z-1]});
}
}
}
}
else
{
if(!vis[now.x][now.y][B[now.x][now.y]])
{
if(dis[now.x][now.y][B[now.x][now.y]]>dis[now.x][now.y][0]+A[now.x][now.y])
{
dis[now.x][now.y][B[now.x][now.y]]=dis[now.x][now.y][0]+A[now.x][now.y];
q.push((node){now.x,now.y,B[now.x][now.y],dis[now.x][now.y][B[now.x][now.y]]});
}
}
}
}
while(!q.empty())q.pop();
}
ll xty,xtz,ytx,ytz,ztx,zty;
int main()
{
n=init();m=init();
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
B[i][j]=init();
B[i][j]=min(B[i][j],n+m+1);
topt=max(B[i][j],topt);
}
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
A[i][j]=init();
}
}
xx=init();xy=init();yx=init();yy=init();zx=init();zy=init();
dijkstra(xx,xy);
xty=dis[yx][yy][0];
xtz=dis[zx][zy][0];
dijkstra(yx,yy);
ytx=dis[xx][xy][0];
ytz=dis[zx][zy][0];
dijkstra(zx,zy);
ztx=dis[xx][xy][0];
zty=dis[yx][yy][0];
if(ytx+ztx<ans)
{
where=‘X‘;
ans=ytx+ztx;
}
if(xty+zty<ans)
{
where=‘Y‘;
ans=xty+zty;
}
if(xtz+ytz<ans)
{
where=‘Z‘;
ans=xtz+ytz;
}
if(ans==llINF)
{
printf("NO\n");
}
else printf("%c\n"LLD,where,ans);
return 0;
}
/*
srO xudyh davidlee1999WTK linkct1999 zlser Orz
*/