Why WA? :(
Const
Value: Array [0..5,0..5] of LongInt =
(
(0,0, 0, 0, 0, 0),
(0,32768,16384,8192,4096,0),
(0,2048 ,1024 ,512 ,256 ,0),
(0,128 ,64 ,32 ,16 ,0),
(0,8 ,4 ,2 ,1 ,0),
(0,0, 0, 0, 0, 0)
);
Var
Res,N,xx,yy,Y,Start,X: LongInt;
S: String;
Mask: Array [1..3,1..3] of LongInt;
Masks: Array [1..16] of LongInt;
Function Good(X: LongInt): Boolean;
Var Num,N: LongInt;
begin
Num:=Start;
For N:=1 to 16 do
If (X and (1 shl (N-1))<>0) then
Num:=Num xor Masks[N];
Good:=(Num=0) or (Num=65536);
end;
Function NBits(X: LongInt): LongInt;
Var Res,N: LongInt;
begin
Res:=0;
N:=X;
While N>0 do
begin
Inc(Res, N and 1);
N:=N shr 1;
end;
NBits:=Res;
end;
begin
Start:=0;
For Y:=1 to 4 do
begin
ReadLn(S);
For X:=1 to 4 do
If S[X]='B' then
Start:=Start or Value[Y,X];
end;
For Y:=1 to 3 do
begin
ReadLn(S);
For X:=1 to 3 do
If s[X]='0' then
Mask[y,x]:=0
else
Mask[y,x]:=1;
end;
For YY:=1 to 4 do
for XX:=1 to 4 do
begin
N:=0;
For X:=-1 to 1 do
For Y:=-1 to 1 do
N:=N + (Mask[Y+2,x+2] * Value[yy+y,xx+x]);
Masks[yy*4-4+xx]:=N;
end;
Res:=-1;
For X:=0 to 65536 do
If ((Res=-1) or (NBits(X)<Res)) and (Good(X)) then
Res:=NBits(X);
If Res<>-1 then
WriteLn(res)
else
WriteLn('Impossible');
end.