#include #include #include #include #include #include #include using namespace std; const int DIR[] = {0, 1, 1,0,0,-1,-1,0}; const char KABE = 'x'; int main() { int width, height; int case_count = 0; while (cin >> width >> height && !(width == 0 && height == 0)){ char table[30][30]; for (int x = 0; x < 30; ++x){ for (int y = 0; y < 30; ++y){ table[x][y] = KABE; } } pair target[20]; int dirty_count = 0; for (int y = 0; y < height; ++y){ string in; cin >> in; for (int x = 0; x < width; ++x){ char c = in[x]; table[x + 1][y + 1] = c; if (c == 'o'){ target[0] = make_pair(x + 1, y + 1); } else if (c == '*'){ target[++dirty_count] = make_pair(x + 1, y + 1); } } } int dist[20][20]; for (int i = 0; i < 20; ++i){ for (int j = 0; j < 20; ++j){ dist[i][j] = INT_MAX; } } for (int index = 0; index < dirty_count + 1; ++index){ int counter = 0; set > set_open; set_open.insert(target[index]); set > set_close; while (!set_open.empty()){ set > set_next; for (set >::iterator it = set_open.begin(); it != set_open.end(); ++it){ int x = it->first; int y = it->second; for (int to = 0; to < dirty_count + 1; ++to){ if (target[to] == make_pair(x, y)){ dist[index][to] = counter; } } set_close.insert(*it); for (int dir = 0; dir < 4; ++dir){ int nx = x + DIR[dir * 2 + 0]; int ny = y + DIR[dir * 2 + 1]; if (table[nx][ny] != KABE && set_close.find(make_pair(nx, ny)) == set_close.end()){ set_next.insert(make_pair(nx, ny)); } } } ++counter; set_open = set_next; } } /* cout << "case : " << case_count + 1 << endl; for (int i = 0; i < dirty_count + 1; ++i){ for (int j = 0; j < dirty_count + 1; ++j){ cout << " " << dist[j][i]; } cout << endl; ++case_count; } */ bool error = false; for (int i = 0; i < dirty_count + 1; ++i){ for (int j = 0; j < dirty_count + 1; ++j){ if (dist[i][j] == INT_MAX){ error = true; } } if (error){ break; } } if (error){ cout << -1 << endl; continue; } vector per; for (int i = 0; i < dirty_count + 1; ++i){ per.push_back(i); } int answer = INT_MAX; do { int temp = 0; for (int i = 0; i < dirty_count; ++i){ temp += dist[per[i]][per[i + 1]]; } if (answer > temp){ answer = temp; } } while (next_permutation(per.begin() + 1, per.end())); cout << answer << endl; } return 0; }