Problem:
Problem E: Manhattan Wiring

There is a rectangular area containing n × m cells. Two cells are marked with "2", and another two with "3". Some cells are occupied by obstacles. You should connect the two "2"s and also the two "3"s with non-intersecting lines. Lines can run only vertically or horizontally connecting
centers of cells without obstacles.

Lines cannot run on a cell with an obstacle. Only one line can run on a cell at most once. Hence,
a line cannot intersect with the other line, nor with itself. Under these constraints, the total
length of the two lines should be minimized. The length of a line is defined as the number of
cell borders it passes. In particular, a line connecting cells sharing their border has length 1.

Fig. 6(a) shows an example setting. Fig. 6(b) shows two lines satisfying the constraints above
with minimum total length 18.

Figure 6: An example setting and its solution

Input

The input consists of multiple datasets, each in the following format.

n m
row1
...
rown

n is the number of rows which satisfies 2 ≤ n ≤ 9. m is the number of columns which satisfies
2 ≤ m ≤ 9. Each rowi is a sequence of m digits separated by a space. The digits mean the
following.

0: Empty

1: Occupied by an obstacle

2: Marked with "2"

3: Marked with "3"

The end of the input is indicated with a line containing two zeros separated by a space.

Output

For each dataset, one line containing the minimum total length of the two lines should be output.
If there is no pair of lines satisfying the requirement, answer "0" instead. No other characters
should be contained in the output.

Sample Input

5 5
0 0 0 0 0
0 0 0 3 0
2 0 2 0 0
1 0 1 1 1
0 0 0 0 3
2 3
2 2 0
0 3 3
6 5
2 0 0 0 0
0 3 0 0 0
0 0 0 0 0
1 1 1 0 0
0 0 0 0 0
0 0 2 3 0
5 9
0 0 0 0 0 0 0 0 0
0 0 0 0 3 0 0 0 0
0 2 0 0 0 0 0 2 0
0 0 0 0 3 0 0 0 0
0 0 0 0 0 0 0 0 0
9 9
3 0 0 0 0 0 0 0 2
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
2 0 0 0 0 0 0 0 3
9 9
0 0 0 1 0 0 0 0 0
0 2 0 1 0 0 0 0 3
0 0 0 1 0 0 0 0 2
0 0 0 1 0 0 0 0 3
0 0 0 1 1 1 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
9 9
0 0 0 0 0 0 0 0 0
0 3 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 2 3 2
0 0

Output for the Sample Input

18
2
17
12
0
52
43

Solution in C++ (judge verdict: Wrong Answer)

#include<cstdio>
#include<cstring>

#define rep(i,n) for(int i=0;i<(n);i++)

using namespace std;

const int dx[]={1,0,-1,0},dy[]={0,-1,0,1};

int h,w,B[9][9];

int x2,y2,x3,y3; // 2, 3 それぞれの出発点

int bfs(int x,int y,int tar){
	int d[9][9];
	memset(d,-1,sizeof d);
	d[y][x]=0;
	int Q[81],head=0,tail=0; Q[tail++]=y*w+x;
	while(head<tail){
		int i=Q[head]/w,j=Q[head]%w; head++;
		rep(k,4){
			int yy=i+dy[k],xx=j+dx[k];
			if(0<=yy && yy<h && 0<=xx && xx<w && d[yy][xx]==-1){
				if(B[yy][xx]==tar) return d[i][j]+1;
				if(B[yy][xx]== 0 ) Q[tail++]=yy*w+xx, d[yy][xx]=d[i][j]+1;
			}
		}
	}
	return 777;
}

int ans;
void dfs(int x,int y,int now){
	int hstar=bfs(x,y,2)+bfs(x3,y3,3);
	if(ans<now+1+hstar) return;

	// これまでのパスと接したら枝刈り
	int cnt=0;
	rep(k,4){
		int xx=x+dx[k],yy=y+dy[k];
		if(0<=xx && xx<w && 0<=yy && yy<h && B[yy][xx]==7) cnt++;
	}
	if(cnt>=2) return;

	// 777...777
	// 700...007
	// というパターン ( or 回転して一致するもの ) があれば枝刈り
	for(int L=3;L<=8;L++){
		bool ng;
		if(y>0 && x+L-1<w){
			ng=true;
			if(ng) rep(j,L) if(B[y-1][x+j]!=7) { ng=false; break; }
			if(ng) rep(j,L) if(B[y  ][x+j]!=(j==0||j==L-1?7:0)) { ng=false; break; }
			if(ng) return;

			ng=true;
			if(ng) rep(j,L) if(B[y-1][x+j]!=(j==0||j==L-1?7:0)) { ng=false; break; }
			if(ng) rep(j,L) if(B[y  ][x+j]!=7) { ng=false; break; }
			if(ng) return;
		}
		if(y>0 && x-L+1>=0){
			ng=true;
			if(ng) rep(j,L) if(B[y-1][x-j]!=7) { ng=false; break; }
			if(ng) rep(j,L) if(B[y  ][x-j]!=(j==0||j==L-1?7:0)) { ng=false; break; }
			if(ng) return;

			ng=true;
			if(ng) rep(j,L) if(B[y-1][x-j]!=(j==0||j==L-1?7:0)) { ng=false; break; }
			if(ng) rep(j,L) if(B[y  ][x-j]!=7) { ng=false; break; }
			if(ng) return;
		}
		if(y+L-1<h && x+1<w){
			ng=true;
			if(ng) rep(i,L) if(B[y+i][x  ]!=7) { ng=false; break; }
			if(ng) rep(i,L) if(B[y+i][x+1]!=(i==0||i==L-1?7:0)) { ng=false; break; }
			if(ng) return;

			ng=true;
			if(ng) rep(i,L) if(B[y+i][x  ]!=(i==0||i==L-1?7:0)) { ng=false; break; }
			if(ng) rep(i,L) if(B[y+i][x+1]!=7) { ng=false; break; }
			if(ng) return;
		}
		if(y-L+1>=0 && x+1<w){
			ng=true;
			if(ng) rep(i,L) if(B[y-i][x  ]!=7) { ng=false; break; }
			if(ng) rep(i,L) if(B[y-i][x+1]!=(i==0||i==L-1?7:0)) { ng=false; break; }
			if(ng) return;

			ng=true;
			if(ng) rep(i,L) if(B[y-i][x  ]!=(i==0||i==L-1?7:0)) { ng=false; break; }
			if(ng) rep(i,L) if(B[y-i][x+1]!=7) { ng=false; break; }
			if(ng) return;
		}
	}

	rep(k,4){
		int xx=x+dx[k],yy=y+dy[k];
		if(0<=xx && xx<w && 0<=yy && yy<h){
			if(B[yy][xx]==2){
				ans=now+1+bfs(x3,y3,3);
				return;
			}
			else if(B[yy][xx]==0){
				B[yy][xx]=7;
				dfs(xx,yy,now+1);
				B[yy][xx]=0;
			}
		}
	}
}

int main(){
	while(scanf("%d%d",&h,&w),h){
		rep(i,h) rep(j,w) scanf("%d",B[i]+j);

		rep(i,h) rep(j,w) {
			if(B[i][j]==2) x2=j, y2=i;
			if(B[i][j]==3) x3=j, y3=i;
		}
		B[y2][x2]=7; // 訪問済みのマーク

		ans=777;
		dfs(x2,y2,0);
		printf("%d\n",ans<777?ans:0);
	}

	return 0;
}