Language: C++

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<ll,ll> pii;
#define rep(i,n) for(ll i=0;i<(ll)(n);i++)
#define all(a)  (a).begin(),(a).end()
#define pb push_back
#define INF (int)(999999999/2)
#define eps 1e-9
#define MAX_N 5000
int w,h;
int fie[30][30];
string data[30];


void bfs(int yy,int xx){
    int dx[]={0,1,0,-1};
    int dy[]={1,0,-1,0};
    queue<pii> que;
    que.push(pii(yy,xx));
    
    bool used[30][30]={};
    while(que.size()){
        int y=que.front().first,x=que.front().second;
        que.pop();
        used[y][x]=true;
        rep(i,4){
            int ddy=y+dy[i],ddx=x+dx[i];
            if(ddy<0||ddx<0||ddy>=h||ddx>=w||data[ddy][ddx]=='x'||used[ddy][ddx])continue;
            fie[ddy][ddx]=fie[y][x]+1;
            que.push(pii(ddy,ddx));
        }
    }
}

int main(){
    while(cin>>w>>h&&w&&h){
        int v=0;
        rep(i,h){
            cin>>data[i];
        }
        
        vector<pii> p;
        rep(i,h)rep(j,w)if(data[i][j]=='o'){p.pb(pii(i,j));v++;}
        rep(i,h)rep(j,w)if(data[i][j]=='*'){p.pb(pii(i,j));v++;}

        map<  pair<pii,pii> , int  > pass;
        rep(i,p.size()){
            rep(i,30)rep(j,30)fie[i][j]=INF;
            fie[p[i].first][p[i].second]=0;
            bfs(p[i].first,p[i].second);
            rep(j,p.size()){
                pass[make_pair(p[i],p[j])]=fie[p[j].first][p[j].second];
            }
        }
        
        vector<int> num;
        rep(i,v)num.pb(i);
        sort(all(num));
        int mini=INF;
        do{
            if(num[0]!=0)continue;
            int sum=0;
            rep(i,v-1){
                sum+=pass[  make_pair(p[num[i]],p[num[i+1]])  ];
            }
            mini=min(mini,sum);
        }while(next_permutation(all(num)));
        if(mini==INF)cout<<"-1"<<endl;
        else cout<<mini<<endl;
    }
}

Compiler output:
a.cc: In function 'void bfs(int, int)':
a.cc:29:46: error: reference to 'data' is ambiguous
   29 |             if(ddy<0||ddx<0||ddy>=h||ddx>=w||data[ddy][ddx]=='x'||used[ddy][ddx])continue;
      |                                              ^~~~
In file included from /usr/include/c++/14/string:53,
                 from /usr/include/c++/14/bitset:52,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:52,
                 from a.cc:1:
/usr/include/c++/14/bits/range_access.h:344:5: note: candidates are: 'template<class _Tp> constexpr const _Tp* std::data(initializer_list<_Tp>)'
  344 |     data(initializer_list<_Tp> __il) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:334:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr _Tp* std::data(_Tp (&)[_Nm])'
  334 |     data(_Tp (&__array)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:323:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(const _Container&)'
  323 |     data(const _Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:312:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(_Container&)'
  312 |     data(_Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
a.cc:13:8: note:                 'std::string data [30]'
   13 | string data[30];
      |        ^~~~
a.cc: In function 'int main()':
a.cc:40:18: error: reference to 'data' is ambiguous
   40 |             cin>>data[i];
      |                  ^~~~
/usr/include/c++/14/bits/range_access.h:344:5: note: candidates are: 'template<class _Tp> constexpr const _Tp* std::data(initializer_list<_Tp>)'
  344 |     data(initializer_list<_Tp> __il) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:334:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr _Tp* std::data(_Tp (&)[_Nm])'
  334 |     data(_Tp (&__array)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:323:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(const _Container&)'
  323 |     data(const _Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:312:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(_Container&)'
  312 |     data(_Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
a.cc:13:8: note:                 'std::string data [30]'
   13 | string data[30];
      |        ^~~~
a.cc:44:28: error: reference to 'data' is ambiguous
   44 |         rep(i,h)rep(j,w)if(data[i][j]=='o'){p.pb(pii(i,j));v++;}
      |                            ^~~~
/usr/include/c++/14/bits/range_access.h:344:5: note: candidates are: 'template<class _Tp> constexpr const _Tp* std::data(initializer_list<_Tp>)'
  344 |     data(initializer_list<_Tp> __il) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:334:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr _Tp* std::data(_Tp (&)[_Nm])'
  334 |     data(_Tp (&__array)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:323:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(const _Container&)'
  323 |     data(const _Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:312:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(_Container&)'
  312 |     data(_Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
a.cc:13:8: note:                 'std::string data [30]'
   13 | string data[30];
      |        ^~~~
a.cc:45:28: error: reference to 'data' is ambiguous
   45 |         rep(i,h)rep(j,w)if(data[i][j]=='*'){p.pb(pii(i,j));v++;}
      |                            ^~~~
/usr/include/c++/14/bits/range_access.h:344:5: note: candidates are: 'template<class _Tp> constexpr const _Tp* std::data(initializer_list<_Tp>)'
  344 |     data(initializer_list<_Tp> __il) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:334:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr _Tp* std::data(_Tp (&)[_Nm])'
  334 |     data(_Tp (&__array)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:323:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(const _Container&)'
  323 |     data(const _Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:312:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(_Container&)'
  312 |     data(_Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
a.cc:13:8: note:                 'std::string data [30]'
   13 | string data[30];
      |        ^~~~