#include <bits/stdc++.h> using namespace std; #define p make_pair #define ll long long #define INF 0x3f3f3f3f #define MAXN 1000010 #define MAXM 1010 inline int read() { int x = 0,ff = 1;char ch = getchar(); while(!isdigit(ch)) { if(ch == '-') ff = -1; ch = getchar(); } while(isdigit(ch)) { x = (x << 1) + (x << 3) + (ch ^ 48); ch = getchar(); } return x * ff; } int n,m,sx,sy,tx,ty,sum = 0,a[MAXM][MAXM],vis[MAXM][MAXM],v[MAXM][MAXM]; int dx[8] = {2,2,-2,-2,1,1,-1,-1}; int dy[8] = {1,-1,1,-1,2,-2,2,-2}; ll f[MAXM][MAXM],dis[MAXM][MAXM],d[MAXM][MAXM]; void BFS() { memset(dis,0x3f,sizeof(dis)); memset(vis,false,sizeof(vis)); queue < pair < int , int > > q; q.push(p(sx,sy)); dis[sx][sy] = 0; vis[sx][sy] = true; while(!q.empty()) { int x = q.front().first; int y = q.front().second; q.pop(); vis[x][y] = false; // printf("x = %d y = %d\n",x,y); for(int i = 0;i <= 7;++i) { int xx = x + dx[i]; int yy = y + dy[i]; if(xx < 1 || xx > n || yy < 1 || yy > m || a[xx][yy] == 2) continue; if(dis[xx][yy] > dis[x][y] + v[xx][yy]) { dis[xx][yy] = dis[x][y] + v[xx][yy]; // printf("xx = %d yy = %d\n",xx,yy); if(xx == tx && yy == ty) continue; if(!vis[xx][yy]) { vis[xx][yy] = true; q.push(p(xx,yy)); } } } } } void BFS2() { memset(d,0x3f,sizeof(d)); memset(vis,false,sizeof(vis)); queue < pair < int , int > > q; q.push(p(sx,sy)); d[sx][sy] = 0; vis[sx][sy] = true; while(!q.empty()) { int x = q.front().first; int y = q.front().second; q.pop(); vis[x][y] = false; // printf("x = %d y = %d\n",x,y); for(int i = 0;i <= 7;++i) { int xx = x + dx[i]; int yy = y + dy[i]; if(xx < 1 || xx > n || yy < 1 || yy > m || a[xx][yy] == 2) continue; if(dis[xx][yy] != dis[x][y] + v[xx][yy]) continue; if(d[xx][yy] > d[x][y] + 1) { d[xx][yy] = d[x][y] + 1; // printf("xx = %d yy = %d\n",xx,yy); if(xx == tx && yy == ty) continue; if(!vis[xx][yy]) { vis[xx][yy] = true; q.push(p(xx,yy)); } } } } } void BFS3() { memset(vis,false,sizeof(vis)); queue < pair < int , int > > q; q.push(p(sx,sy)); vis[sx][sy] = true; f[sx][sy] = 1; while(!q.empty()) { int x = q.front().first; int y = q.front().second; q.pop(); vis[x][y] = false; for(int i = 0;i <= 7;++i) { int xx = x + dx[i]; int yy = y + dy[i]; if(xx < 1 || xx > n || yy < 1 || yy > m || a[xx][yy] == 2) continue; if(dis[xx][yy] != dis[x][y] + v[xx][yy]) continue; if(d[xx][yy] != d[x][y] + 1) continue; f[xx][yy] += f[x][y]; if(xx == tx && yy == ty) continue; if(!vis[xx][yy]) { vis[xx][yy] = true; q.push(p(xx,yy)); } } } } int main() { n = read(); m = read(); for(int i = 1;i <= n;++i) { for(int j = 1;j <= m;++j) { a[i][j] = read(); if(a[i][j] == 3) { sx = i; sy = j; v[i][j] = 0;} if(a[i][j] == 4) { tx = i; ty = j; v[i][j] = 0;} if(a[i][j] == 0) v[i][j] = 1; if(a[i][j] == 1) v[i][j] = 0; } } BFS(); if(dis[tx][ty] == dis[0][0]) { printf("-1\n"); return 0; } printf("%lld\n",dis[tx][ty]); BFS2(); printf("%lld\n",d[tx][ty]); BFS3(); printf("%lld\n",f[tx][ty]); return 0; }