1 /**************************************************************
2 Problem: 1632
3 User: HansBug
4 Language: Pascal
5 Result: Accepted
6 Time:0 ms
7 Memory:376 kb
8 ****************************************************************/
9
10 const
11 xt:array[1..8] of longint=(1,1,-1,-1,2,2,-2,-2);
12 yt:array[1..8] of longint=(2,-2,2,-2,1,-1,1,-1);
13 var
14 i,j,k,l,m,n,ans,f,r,ex,ey:longint;
15 x,y:array[0..10010] of longint;
16 a,b,c,d:array[0..60,0..60] of longint;
17 e:array[0..40,0..40] of int64;
18 procedure bfs;inline;
19 var nx,ny,ty,tx,i,j,k,l,ta:longint;
20 begin
21
22 while f<>r do
23 begin
24 nx:=x[f];ny:=y[f];inc(f);
25 for k:=1 to 8 do
26 begin
27 tx:=nx+xt[k];ty:=ny+yt[k];
28 if (tx<1) or (ty<1) or (tx>n) or (ty>m) or (a[tx,ty]=2) then continue;
29 ta:=b[nx,ny]+longint(a[tx,ty]=0);
30 if ta<b[tx,ty] then
31 begin
32 b[tx,ty]:=ta;
33 c[tx,ty]:=c[nx,ny]+1;
34 e[tx,ty]:=e[nx,ny];
35 if d[tx,ty]<>0 then continue;
36 d[tx,ty]:=1;x[r]:=tx;
37 y[r]:=ty;inc(r);
38 end
39 else
40 begin
41 if ta=b[tx,ty] then
42 begin
43 if (c[nx,ny]+1)<c[tx,ty] then
44 begin
45 c[tx,ty]:=c[nx,ny]+1;
46 e[tx,ty]:=e[nx,ny];
47 if d[tx,ty]<>0 then continue;
48 d[tx,ty]:=1;
49 x[r]:=tx;y[r]:=ty;
50 inc(r);
51 end
52 else
53 begin
54 if (c[nx,ny]+1)=c[tx,ty] then
55 begin
56 inc(e[tx,ty],e[nx,ny]);
57 if d[tx,ty]<>0 then continue;
58 d[tx,ty]:=1;
59 x[r]:=tx;y[r]:=ty;inc(r);
60 end;
61 end;
62 end;
63 end;
64 end;
65 d[nx,ny]:=0;
66 end;
67 end;
68 begin
69 f:=0;r:=1;
70 readln(n,m);
71 for i:=1 to n do
72 for j:=1 to m do
73 begin
74 read(a[i,j]);
75 if j=m then readln;
76 b[i,j]:=maxlongint;c[i,j]:=maxlongint;
77 if a[i,j]=3 then
78 begin
79 d[i,j]:=1;
80 x[0]:=i;y[0]:=j;b[i,j]:=0;
81 c[i,j]:=0;
82 e[i,j]:=1;
83 end
84 else
85 if a[i,j]=4 then
86 begin
87 ex:=i;ey:=j;
88 end;
89 end;
90 bfs;
91 if b[ex,ey]=maxlongint then
92 begin
93 writeln(-1);
94 halt;
95 end
96 else
97 begin
98 writeln(b[ex,ey]);
99 writeln(c[ex,ey]);
100 writeln(e[ex,ey]);
101 end;
102 end.