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 C++11 (judge verdict: Accepted)

#include <bits/stdc++.h>

using namespace std;
 
using ll = long long;
using Pi = pair<int, int>;
using Pl = pair<ll, ll>;
using Ti = tuple<int, int, int>;
using Tl = tuple<ll, ll, ll>;
 
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define mt make_tuple
#define F first
#define S second
#define Get(t, i) get<(i)>((t))
#define all(v) (v).begin(), (v).end()
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define reps(i, f, n) for(int i = (int)(f); i < (int)(n); i++)
#define each(a, b) for(auto& a : b)
 
const int inf = 1 << 25;
const ll INF = 1LL << 55;

int w, h;
char c[22][22];
int d[22][22][1<<10];
int sx, sy;

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

typedef pair<Pi, Pi> PP;

int bfs(int n)
{
  memset(d, -1, sizeof(d));
  queue<PP> que;
  d[sy][sx][(1<<n)-1] = 0;
  que.push(PP(Pi(sx, sy), Pi((1<<n)-1, 0)));
  while(!que.empty()) {
    PP p = que.front(); que.pop();
    int x = p.F.F, y = p.F.S, vis = p.S.F, cost = p.S.S;
    if(vis == 0) return cost;
    rep(i, 4) {
      int nx = x + dx[i], ny = y + dy[i];
      int nvis = (isdigit(c[ny][nx]) ? vis & ~(1<<(c[ny][nx]-'0')) : vis);
      if(0 <= nx && nx < w && 0 <= ny && ny < h && c[ny][nx] != 'x' && d[ny][nx][nvis] == -1) {
	d[ny][nx][nvis] = d[y][x][vis] + 1;
	que.push(PP(Pi(nx, ny), Pi(nvis, d[ny][nx][nvis])));
      }
    }
  }
  return -1;
}

int main()
{
  while(cin >> w >> h, w) {
    int cnt = 0;
    rep(i, h) rep(j, w) {
      cin >> c[i][j];
      if(c[i][j] == '*') c[i][j] = cnt+'0', cnt++;
      if(c[i][j] == 'o') sx = j, sy = i;
    }
    cout << bfs(cnt) << endl;
  }

  return 0;
}