#include <bits/stdc++.h>
#define rep(i, a, n) for(int i = a; i < (n); i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;
const int INF = 1001001001;
const ll LINF = 1001002003004005006ll;
const int mod = 1000000007;
//const int mod = 998244353;

int main()
{
    int n;
    cin >> n;
    vector<int> a(n), b(n);
    rep(i, 0, n) cin >> a[i];
    rep(i, 0, n) cin >> b[i];
    vector<bool> c(10005, true);
    rep(i, 0, n){
        rep(j, 0, a[i]) c[j] = false;
        rep(j, b[i] + 1, 10005) c[j] = false;
    }
    int ans = 0;
    rep(i, 0, 10005) if(c[i]) ans++;
    cout << ans << endl;
    return 0;
}

#include <bits/stdc++.h>

using namespace std;


#define ll long long int
#define llu unsigned long long int
#define ld long double
#define mod 998244353
#define big 1e18 + 10000000
#define small -big
#define pb push_back
#define endl "\n"




template <typename T>

void prarr(T a)
{
     cerr << "[ ";

     for(auto i : a){
         cerr << i << " ";
     }

     cerr << "]" << endl;
}


#define trace(...) _er(#__VA_ARGS__, __VA_ARGS__)

template <typename Arg1>

void _er(const char* name, Arg1&& arg1){
	cerr << "[" << name << " : " << arg1 << "]" << endl;
}

template <typename Arg1, typename... Args>

void _er(const char* names, Arg1&& arg1, Args&&... args){

	const char* comma = strchr(names + 1, ',');
	cerr << "[" ; cerr.write(names, comma - names) << " : " << arg1 << "] ";
	_er(comma+2, args...);
}

ll n;
vector<ll> A[102];
ll cou[102] = {0};
bool visited[102];

void dfs(ll v) {
        if(visited[v]) {
                return;
        }

        visited[v] = true;

        for(auto it : A[v]) {
                if(!visited[it]) {
                        dfs(it);
                }
        }

}

void solve() {

                cin >> n;

                for(ll i = 1;  i <= n; i++) {
                        string s;
                        cin >> s;
                        for(ll j = 0; j < n; j++) {
                                if(s[j] - 48) {
                                    A[i].pb(j + 1);
                                }
                        }
                }

                for(ll i = 1; i <= n; i++) {
                        memset(visited, false, sizeof(visited));

                        dfs(i);

                        for(ll j = 1; j <= n; j++) {
                                if(visited[j]) {
                                        cou[j]++;
                                }
                        }
                }

                ld ans = 0.0;

                for(ll i = 1; i <= n; i++) {
                        ld curr = cou[i];
                        ans += (1.0 / curr * 1.0);
                }

                cout << fixed << setprecision(11) << ans << endl;




}


int main() {


        ios_base::sync_with_stdio(false);
        cin.tie(NULL);


        int t = 1;
        //cin >> t;

        for(int i = 1; i <= t; i++) {
                solve();
        }


        return 0;

}
