#include <bits/stdc++.h>

using namespace std;

#define pb push_back
#define mp make_pair
#define fs first
#define sc second
#define sz(a) ((int) (a).size())
#define all(x) (x).begin(), (x).end()

const long double eps = 1e-9;
const int inf = (1 << 30) - 1;
const long long inf64 = ((long long)1 << 62) - 1;
const long double pi = acos(-1);

template <class T> T sqr (T x) {return x * x;}
template <class T> T abs (T x) {return x < 0 ? -x : x;}

template <class T>
vector<T> ReadVector(int n) {
    vector<T> a(n);
    for (int i = 0; i < n; ++i) {
        cin >> a[i];
    }
    return a;
}

template <class T>
void WriteVector(const vector<T>& a) {
    for (int i = 0; i < sz(a); ++i) {
        cout << a[i] << (i + 1 < sz(a) ? ' ' : '\n');
    }
}

void DFS(const vector<vector<int>>& a, int v, vector<int>& used) {
    used[v] = true;
    for (int i = 0; i < sz(a[v]); ++i) {
        if (!used[i] && a[v][i]) {
            DFS(a, i, used);
        }
    }
}

void Solve() {
    int n;
    cin >> n;

    vector<vector<int>> a(n, vector<int>(n));
    for (int i = 0; i < n; ++i) {
        string s;
        cin >> s;
        for (int j = 0; j < n; ++j) {
            a[i][j] = (s[j] == '1');
        }
    }

    vector<vector<int>> a_rev(n, vector<int>(n));
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
            a_rev[i][j] = a[j][i];
        }
    }

    double res = 0;
    for (int i = 0; i < n; ++i) {
        vector<int> used(n);
        DFS(a_rev, i, used);

        int cnt = 0;
        for (int j = 0; j < n; ++j) {
            cnt += used[j];
        }

        res += 1.0 / cnt;
    }

    cout.precision(20);
    cout << res << "\n";
}

int main () {
    ios_base::sync_with_stdio(0);
    std::cin.tie(nullptr);
    
    // clock_t start_clock = clock();
    
    // freopen("_input.txt", "rt", stdin);
    // freopen("_output.txt", "wt", stdout);

    int tc = 1;
    // cin >> tc;
    for (int ti = 0; ti < tc; ++ti) {
        Solve();
    }
    
    // double total_seconds = (double) (clock() - start_clock) / CLOCKS_PER_SEC;
    // cerr << "Working time: " << total_seconds << "s." << endl;

    return 0;
}

#include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(ll i=0;i<n;i++)
#define repl(i,l,r) for(ll i=(l);i<(r);i++)
#define per(i,n) for(ll i=n-1;i>=0;i--)
#define perl(i,r,l) for(ll i=r-1;i>=l;i--)
#define fi first
#define se second
#define pb push_back
#define mkp make_pair
#define ins insert
#define pqueue(x) priority_queue<x,vector<x>,greater<x>>
#define all(x) (x).begin(),(x).end()
#define CST(x) cout<<fixed<<setprecision(x)
#define vtpl(x,y,z) vector<tuple<x,y,z>>
#define rev(x) reverse(x);
#define lin(x) ll x; cin>>x;
#define stin(x) string x; cin>>x;
#define yn(x)  if(x) {  cout<<"Yes"<<endl;   }    else {    cout<<"No"<<endl;   }
#define YN(x)  if(x) {  cout<<"YES"<<endl;   }    else {    cout<<"NO"<<endl;   }
#define co(x) cout<<x<<endl;
using ll=long long;
using ld=long double;
using vl=vector<ll>;
using vvl=vector<vector<ll>>;
using pl=pair<ll,ll>;
using vpl=vector<pl>;
using vvpl=vector<vpl>;
const ll mod=1000000007;
const ll MOD9=998244353;
const int inf=1e9+10;
const ll INF=4e18;
const ll dy[8]={1,0,-1,0,1,1,-1,-1};
const ll dx[8]={0,-1,0,1,1,-1,1,-1};
template <typename T> inline bool chmax(T &a, T b) {
    return ((a < b) ? (a = b, true) : (false));
}
template <typename T> inline bool chmin(T &a, T b) {
    return ((a > b) ? (a = b, true) : (false));
}
ll powmod(ll n, ll k, ll m) {
ll r=1;
 for(; k>0; k >>=1) {
   if(k&1) r=(r*n)%m;
   n=(n*n)%m;
 }
  return r;
}

ll fact(ll n) {
ll a=1;
rep(i,n) {
a=a*(n-i);
}
return a;
}

ll pow(ll a, ll b) {
ll x=1;
rep(i,b) {
x=x*a;
}
return x;
}

int main() {
  lin(N);
  lin(M);
  ll data[N][N];
  rep(i,N) rep(j,N) data[i][j]=inf;
  
  
  rep(i,M) {
lin(a); lin(b); lin(c);
    chmin(data[a-1][b-1],c);
  }
  
  //Warshal-floyd neu
 rep(k,N) {
   rep(i,N) {
     if(data[i][k]==inf) {
       continue;
     }
     
     rep(j,N) {//rep1
       if(data[k][j]==inf) {
         continue;
       }
       else {
         chmin(data[i][j],data[i][k]+data[k][j]);
       }
     }//rep1
     
   }
 }
  
  
  
  
  rep(i,N) {
    if(data[i][i]==inf) {
      co(-1);
    }
    else {
      co(data[i][i]);
    }
    
    
    
  }
  
  
  
}
