#include <stdio.h>
#define MAX 21
#define MAXMASK 1025
#define INF 100000
int r, c, dirty;
char s[MAX][MAX];
int dp[MAX][MAX][MAXMASK];
int getMin(int a, int b) {return (a < b ? a : b);}
int bfs(int x, int y, int mask) {
if (s[x][y] == 'x') return INF;
if (s[x][y] >= 'a' && s[x][y] <= 'n') {
int dig = s[x][y]-'a';
mask |= (1<<dig);
}
if (mask == dirty) return 0;
if (dp[x][y][mask] >= 0) return dp[x][y][mask];
if (dp[x][y][mask] == -2) return INF;
int min = INF;
dp[x][y][mask] = -2;
if (x > 0) min = getMin(min, bfs(x-1, y, mask));
if (y > 0) min = getMin(min, bfs(x, y-1, mask));
if (x < r-1) min = getMin(min, bfs(x+1, y, mask));
if (y < c-1) min = getMin(min, bfs(x, y+1, mask));
if (min != INF) min++;
dp[x][y][mask] = min;
return min;
}
int main(void) {
int ans, sx, sy, i, j, k;
while (1) {
if (c == 0) break;
dirty = 0;
for (i
=0; i
<r
; i
++) scanf("%s", s
[i
]); for (i=0; i<r; i++) {
for (j=0; j<c; j++) {
if (s[i][j] == '*') {
s[i][j] = 'a'+dirty;
dirty++;
} else if (s[i][j] == 'o') sx = i, sy = j;
}
}
dirty = (1<<dirty) - 1;
for (i=0; i<MAX; i++) {
for (j=0; j<MAX; j++) {
for (k=0; k<MAXMASK; k++) dp[i][j][k] = -1;
}
}
ans = bfs(sx, sy, 0);
if (ans
>= INF
) printf("-1\n"); }
return 0;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CgojZGVmaW5lIE1BWCAyMQojZGVmaW5lIE1BWE1BU0sgMTAyNQojZGVmaW5lIElORiAxMDAwMDAKCmludCByLCBjLCBkaXJ0eTsKY2hhciBzW01BWF1bTUFYXTsKaW50IGRwW01BWF1bTUFYXVtNQVhNQVNLXTsKCmludCBnZXRNaW4oaW50IGEsIGludCBiKSB7cmV0dXJuIChhIDwgYiA/IGEgOiBiKTt9CgppbnQgYmZzKGludCB4LCBpbnQgeSwgaW50IG1hc2spIHsKICAgIGlmIChzW3hdW3ldID09ICd4JykgcmV0dXJuIElORjsKICAgIGlmIChzW3hdW3ldID49ICdhJyAmJiBzW3hdW3ldIDw9ICduJykgewogICAgICAgIGludCBkaWcgPSBzW3hdW3ldLSdhJzsKICAgICAgICBtYXNrIHw9ICgxPDxkaWcpOwogICAgfQogICAgaWYgKG1hc2sgPT0gZGlydHkpIHJldHVybiAwOwogICAgaWYgKGRwW3hdW3ldW21hc2tdID49IDApIHJldHVybiBkcFt4XVt5XVttYXNrXTsKICAgIGlmIChkcFt4XVt5XVttYXNrXSA9PSAtMikgcmV0dXJuIElORjsKICAgIGludCBtaW4gPSBJTkY7CiAgICBkcFt4XVt5XVttYXNrXSA9IC0yOwogICAgaWYgKHggPiAwKSBtaW4gPSBnZXRNaW4obWluLCBiZnMoeC0xLCB5LCBtYXNrKSk7CiAgICBpZiAoeSA+IDApIG1pbiA9IGdldE1pbihtaW4sIGJmcyh4LCB5LTEsIG1hc2spKTsKICAgIGlmICh4IDwgci0xKSBtaW4gPSBnZXRNaW4obWluLCBiZnMoeCsxLCB5LCBtYXNrKSk7CiAgICBpZiAoeSA8IGMtMSkgbWluID0gZ2V0TWluKG1pbiwgYmZzKHgsIHkrMSwgbWFzaykpOwogICAgaWYgKG1pbiAhPSBJTkYpIG1pbisrOwogICAgZHBbeF1beV1bbWFza10gPSBtaW47CiAgICByZXR1cm4gbWluOwp9CgppbnQgbWFpbih2b2lkKSB7CiAgICBpbnQgYW5zLCBzeCwgc3ksIGksIGosIGs7CiAgICB3aGlsZSAoMSkgewogICAgICAgIHNjYW5mKCIlZCAlZCIsICZjLCAmcik7CiAgICAgICAgaWYgKGMgPT0gMCkgYnJlYWs7CiAgICAgICAgZGlydHkgPSAwOwogICAgICAgIGZvciAoaT0wOyBpPHI7IGkrKykgc2NhbmYoIiVzIiwgc1tpXSk7CiAgICAgICAgZm9yIChpPTA7IGk8cjsgaSsrKSB7CiAgICAgICAgICAgIGZvciAoaj0wOyBqPGM7IGorKykgewogICAgICAgICAgICAgICAgaWYgKHNbaV1bal0gPT0gJyonKSB7CiAgICAgICAgICAgICAgICAgICAgc1tpXVtqXSA9ICdhJytkaXJ0eTsKICAgICAgICAgICAgICAgICAgICBkaXJ0eSsrOwogICAgICAgICAgICAgICAgfSBlbHNlIGlmIChzW2ldW2pdID09ICdvJykgc3ggPSBpLCBzeSA9IGo7CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgZGlydHkgPSAoMTw8ZGlydHkpIC0gMTsKICAgICAgICBmb3IgKGk9MDsgaTxNQVg7IGkrKykgewogICAgICAgICAgICBmb3IgKGo9MDsgajxNQVg7IGorKykgewogICAgICAgICAgICAgICAgZm9yIChrPTA7IGs8TUFYTUFTSzsgaysrKSBkcFtpXVtqXVtrXSA9IC0xOwogICAgICAgICAgICB9CiAgICAgICAgfQogICAgICAgIGFucyA9IGJmcyhzeCwgc3ksIDApOwogICAgICAgIGlmIChhbnMgPj0gSU5GKSBwcmludGYoIi0xXG4iKTsKICAgICAgICBlbHNlIHByaW50ZigiJWRcbiIsIGFucyk7CiAgICB9CiAgICByZXR1cm4gMDsKfQ==
NyA1Ci4uLi4uLi4KLm8uLi4qLgouLi4uLi4uCi4qLi4uKi4KLi4uLi4uLgoxNSAxMwouLi4uLi4ueC4uLi4uLi4KLi4uby4uLnguLi4uKi4uCi4uLi4uLi54Li4uLi4uLgouLi4uLi4ueC4uLi4uLi4KLi4uLi4uLnguLi4uLi4uCi4uLi4uLi4uLi4uLi4uLgp4eHh4eC4uLi4ueHh4eHgKLi4uLi4uLi4uLi4uLi4uCi4uLi4uLi54Li4uLi4uLgouLi4uLi4ueC4uLi4uLi4KLi4uLi4uLnguLi4uLi4uCi4uKi4uLi54Li4uLiouLgouLi4uLi4ueC4uLi4uLi4KMTAgMTAKLi4uLi4uLi4uLgouLm8uLi4uLi4uCi4uLi4uLi4uLi4KLi4uLi4uLi4uLgouLi4uLi4uLi4uCi4uLi4ueHh4eHgKLi4uLi54Li4uLgouLi4uLnguKi4uCi4uLi4ueC4uLi4KLi4uLi54Li4uLgowIDA=
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