Problem:
Problem F: Cleaning Robot

Here, we want to solve path planning for a mobile robot cleaning a
rectangular room floor with furniture.

Consider the room floor paved with square tiles whose size fits
the cleaning robot (1 × 1).
There are 'clean tiles' and 'dirty tiles', and the robot can change
a 'dirty tile' to a 'clean tile' by visiting the tile.
Also there may be some obstacles (furniture) whose size fits a
tile in the room. If there is an obstacle on a tile, the robot cannot visit
it.
The robot moves to an adjacent tile with one move.
The tile onto which the robot moves must be one of four tiles
(i.e., east, west, north or south) adjacent to the tile where the
robot is present. The robot may visit a tile twice or more.

Your task is to write a program which computes the minimum number of
moves for the robot to change all 'dirty tiles' to 'clean tiles',
if ever possible.

Input

The input consists of multiple maps, each representing the size
and arrangement of the room. A map is given in the following format.

w h

c11 c12 c13 ... c1w

c21 c22 c23 ... c2w

...

ch1 ch2 ch3 ... chw

The integers w and h are the lengths of the two sides
of the floor of the room in terms of widths of floor tiles. w and h
are less than or equal to 20. The character cyx represents
what is initially on the tile with coordinates (x, y) as follows.

'.' : a clean tile

'*' : a dirty tile

'x' : a piece of furniture (obstacle)

'o' : the robot (initial position)

In the map the number of 'dirty tiles' does not exceed 10.
There is only one 'robot'.

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

Output

For each map, your program should output a line containing the minimum
number of moves. If the map includes 'dirty tiles' which the robot cannot reach,
your program should output -1.

Sample Input

7 5
.......
.o...*.
.......
.*...*.
.......
15 13
.......x.......
...o...x....*..
.......x.......
.......x.......
.......x.......
...............
xxxxx.....xxxxx
...............
.......x.......
.......x.......
.......x.......
..*....x....*..
.......x.......
10 10
..........
..o.......
..........
..........
..........
.....xxxxx
.....x....
.....x.*..
.....x....
.....x....
0 0

Output for the Sample Input

8
49
-1

Solution in JAVA (judge verdict: Wrong Answer)

import java.util.*;
import java.util.regex.*;
import static java.lang.Math.*;
import static java.lang.System.out;

// AOJ4
public class Main {
	final Scanner sc=new Scanner(System.in);
	final int[] vx={0,1,0,-1,-1,1,1,-1}, vy={-1,0,1,0,-1,-1,1,1};
	final int INF=1<<24;
	static class Point{
		int x, y;
		Point(int x, int y){	this.x=x;	this.y=y;}
		Point(){	this.x=0; this.y=0;}
		@Override public String toString(){	return "("+this.x+","+this.y+")";}
		static boolean ok(int x,int y,int X,int Y,int min){	return (min<=x&&x<X)&&(min<=y&&y<Y);}
		static boolean ok(int x,int y,int X,int Y){	return ok(x,y,X,Y,0);}
	}
	void disp(char[][] c){
		for(int y=0; y<c[0].length; y++){
			for(int x=0; x<c.length; x++)	out.print(c[x][y]);
			out.println();
		}
	}
	public static void main(String[] args) {
		//long s=System.currentTimeMillis();
		new Main().AOJ1140();
		//out.println((System.currentTimeMillis()-s)+"msec");
	}
	
	void AOJ1140(){
		while(sc.hasNext()){
			int W=sc.nextInt(),H=sc.nextInt(),sx=0,sy=0,k=0;
			if((W|H)==0)	break;
			char[][] c=new char[W][H];
			int[][] kk=new int[W][H];
			for(int y=0; y<H; y++){
				String line=sc.next();
				for(int x=0; x<W; x++){
					c[x][y]=line.charAt(x);
					if(c[x][y]=='*'){
						kk[x][y]=k;
						k++;
					}else if(c[x][y]=='o'){
						sx=x;	sy=y;
					}
				}
			}
			//disp(c);
			PriorityQueue<C1140> open=new PriorityQueue<C1140>();
			int[][][][] close=new int[W][H][k+1][(1<<k)];
			for(int i=0; i<W; i++){
				for(int j=0; j<H; j++){
					for(int l=0; l<=k; l++){
						for(int m=0; m<(1<<k); m++)	close[i][j][l][m]=INF;
					}
				}
			}
			open.add(new C1140(sx,sy,0,k,(1<<k)-1,dcopy(c)));
			close[sx][sy][k][(1<<k)-1]=0;
			int ans=INF;
			while(!open.isEmpty()){
				C1140 now=open.poll();
				for(int i=0; i<4; i++){
					boolean flag=false;
					int xx=now.x+vx[i],yy=now.y+vy[i],r=now.r,m=now.m;
					if(!Point.ok(xx, yy, W, H))	continue;
					if(c[xx][yy]=='x')	continue;
					if(now.c[xx][yy]=='*'){
						r--;
						m=now.m^(1<<kk[xx][yy]);
					}
					if(close[xx][yy][r][m]<=now.s+1)	continue;
					if(r==0){
						//out.println("ANS:"+now);
						ans=min(ans,now.s+1);
						//continue;
						open.clear();
						break;
					}
					if(now.c[xx][yy]=='*'){
						now.c[xx][yy]='.';
						flag=true;
						//out.println("DL"+xx+","+yy+" R"+r+" Bit"+Integer.bitCount(m));
						//disp(now.c);
					}
					open.add(new C1140(xx,yy,now.s+1,r,m,dcopy(now.c)));
					close[xx][yy][r][m]=now.s+1;
					if(flag)	now.c[xx][yy]='*';
				}
			}
			out.println((ans>=INF?-1:ans));
		}
	}
	class C1140 implements Comparable<C1140>{
		int x,y,s,r,m;
		char[][] c;
		C1140(int x,int y,int s,int r,int m,char[][] c){	this.x=x; this.y=y; this.s=s; this.r=r; this.m=m; this.c=c;}
		@Override public int compareTo(C1140 o) {
			if(this.r<o.r)	return -1;
			if(this.r>o.r)	return 1;
			if(this.s<o.s)	return -1;
			if(this.s>o.s)	return 1;
			return 0;
		}
		@Override public String toString() {
			return "("+x+","+y+") S:"+s+" R:"+r;
		}
	}
	char[][] dcopy(char[][] c){
		char[][] r=new char[c.length][c[0].length];
		for(int i=0; i<c.length; i++){
			for(int j=0; j<c[0].length; j++)	r[i][j]=c[i][j];
		}
		return r;
	}
}