Problem: 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.


<image>

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.

Example

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

18
2
17
12
0
52
43

Solution (in JAVA):

Solution:
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

//Manhattan Wiring
public class Main{

	int h, w, ai, aj, bi, bj, res, INF = 1<<28, need, c;
	int[] dir;
	int[][] m, dist;
	int[][] d = {{-1,0},{0,1},{1,0},{0,-1}};
	
	boolean ok(int i, int j){
		return 0<=i&&i<h&&0<=j&&j<w;
	}
	
	boolean cancel(int n, int i, int j){
		if(n<3)return false;
		if(dir[n-1]==dir[n-2])return false;
		int K = dir[n-2], N = 0, M = 0;
		int k = n-2;
		while(0<=k && dir[k]==K){
			N++; k--;
		}
		while(0<=k && dir[k]==(dir[n-1]+2)%4){
			M++; k--;
		}
		if(k<0 || N < 2 || M==0)return false;
		for(int x=0;x<M;x++){
			int ni = i+x*d[dir[n-1]][0], nj = j+x*d[dir[n-1]][1];
			for(int y=0;y<N-1;y++){
				ni+=d[(K+2)%4][0]; nj+=d[(K+2)%4][1];
				if(m[ni][nj]!=0)return false;
			}
		}
		return true;
	}
	
	int bfs(){
		for(int[]a:dist)Arrays.fill(a, INF);
		dist[bi][bj] = 0;
		Queue<int[]> q = new LinkedList<int[]>();
		q.add(new int[]{bi, bj});
		while(!q.isEmpty()){
			int[] V = q.poll();
			int pi = V[0], pj = V[1];
			if((bi!=pi || bj!=pj) && m[pi][pj]==3)return dist[pi][pj];
			for(int k=0;k<4;k++){
				int ni = pi+d[k][0], nj = pj+d[k][1];
				if(ok(ni, nj)&&(m[ni][nj]==0 || m[ni][nj]==3)&&dist[ni][nj]==INF){
					dist[ni][nj] = dist[pi][pj]+1;
					q.add(new int[]{ni, nj});
				}
			}
		}
		return INF;
	}
	
	void dfs(int i, int j, int rest, int step){
		if(res<=step+need)return;
		if(rest < need)return;
		if(m[i][j]==2 && (i!=ai || j!=aj)){
			res = Math.min(res, step+bfs()); return;
		}
		if(cancel(step, i, j))return;
		int c = 0;
		for(int k=0;k<4;k++){
			int ni = i+d[k][0], nj = j+d[k][1];
			if(ok(ni, nj)&&m[ni][nj]==-1)c++;
		}
		if(2<=c)return;
		int x = m[i][j];
		m[i][j] = -1;
		for(int k=0;k<4;k++){
			int ni = i+d[k][0], nj = j+d[k][1];
			if(ok(ni, nj)&&(m[ni][nj]==0 || m[ni][nj]==2)){
				dir[step] = k;
				dfs(ni, nj, m[ni][nj]==0?(rest-1):rest, step+1);
			}
		}
		m[i][j] = x;
	}
	
	void run(){
		Scanner sc = new Scanner(System.in);
		for(;;){
			h = sc.nextInt(); w = sc.nextInt();
			if((h|w)==0)break;
			m = new int[h][w];
			dist = new int[h][w];
			dir = new int[100];
			for(int i=0;i<h;i++)for(int j=0;j<w;j++){
				m[i][j] = sc.nextInt();
				if(m[i][j]==0)c++;
				if(m[i][j]==2){
					ai = i; aj = j;
				}
				if(m[i][j]==3){
					bi = i; bj = j;
				}
			}
			res = INF;
			need = bfs();
			if(need!=INF){
				need--;
				m[ai][aj] = -1;
				dfs(ai, aj, c, 0);
			}
			System.out.println(res==INF?0:res);
		}
	}
	
	public static void main(String[] args) {
		new Main().run();
	}
}