#include <bits/stdc++.h>
using namespace std;
int n, kol, used[6000], got;
char c[5010][5010];
vector <int> vec;
vector <int> edge[6000];
void dfs(int v, int pred = -1)
{
	used[v] = 1;
	for(int i = 0; i < edge[v].size(); ++i)
	{
		int to = edge[v][i];
		if(used[to] == 2 || pred == to) continue;
		if(used[to] == 1)
		{
			got = 1;
			used[v] = 2;
			used[to] = 2;
			vec.push_back(v);
			return;
		}
		dfs(to, v);
		if(got)
		{
			vec.push_back(v);
			if(vec.size() >= 3 && c[vec[vec.size() - 3]][v] == '1')
			{
				cout << v << " " << vec[vec.size() - 2] << " " << vec[vec.size() - 3] << endl;
				exit(0);
			}
			return;
		}
	}
	used[v] = 2;
}
int main()
{
	cin >> n;
	for(int i = 1; i <= n; ++i)
	{
		for(int j = 1; j <= n; ++j)
		{
			cin >> c[i][j];
			if(i != j)
			{
				if(c[i][j] == '1')
				{
					edge[i].push_back(j);
				}
			}
		}
	}
	for(int i = 1; i <= n; ++i)
	{
		if(used[i]) continue;
		dfs(i);
	}
	cout << -1;
}


#include <bits/stdc++.h>
using namespace std;
int n, kol, used[6000], got;
char c[5010][5010];
vector <int> vec;
vector <int> edge[6000];
void dfs(int v, int pred = -1)
{
	used[v] = 1;
	for(int i = 0; i < edge[v].size(); ++i)
	{
		int to = edge[v][i];
		if(used[to] == 2 || pred == to) continue;
		if(used[to] == 1)
		{
			got = 1;
			used[v] = 2;
			used[to] = 2;
			vec.push_back(v);
			return;
		}
		dfs(to, v);
		if(got)
		{
			vec.push_back(v);
			used[v] = 2;
			if(vec.size() >= 3 && c[vec[vec.size() - 3]][v] == '1')
			{
				cout << v << " " << vec[vec.size() - 2] << " " << vec[vec.size() - 3] << endl;
				exit(0);
			}
			return;
		}
	}
	used[v] = 2;
}
int main()
{
	cin >> n;
	for(int i = 1; i <= n; ++i)
	{
		for(int j = 1; j <= n; ++j)
		{
			cin >> c[i][j];
			if(i != j)
			{
				if(c[i][j] == '1')
				{
					edge[i].push_back(j);
				}
			}
		}
	}
	for(int i = 1; i <= n; ++i)
	{
		if(used[i]) continue;
		dfs(i);
	}
	cout << -1;
}


#include <bits/stdc++.h>

using namespace std;
#define int long long
int n;
vector<vector<char>> g;
vector<int> used;

void dfs(int v, int p) {
    used[v] = 1;
    for (int j = 0; j < n; ++j) {
        if (g[v][j] == '1') {
            if (!used[j]) dfs(j, v);
            else {
                if (g[j][p] == '1') {
                    cout << p + 1 << " " << v + 1 << " " << j + 1;
                    exit(0);
                }
            }
        }
    }
}

signed main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin >> n;
    g.resize(n, vector<char>(n));
    used.resize(n);
    for (auto &i:g)
        for (auto &j:i)
            cin >> j;
    for(int i = 0; i < n; ++i){
        dfs(i, -1);
    }
    cout << "-1";
}


#include <bits/stdc++.h>

using namespace std;
#define int long long
int n;
vector<vector<char>> g;
vector<int> used;

void dfs(int v, int p) {
    used[v] = 1;
    for (int j = 0; j < n; ++j) {
        if (g[v][j] == '1') {
            if (g[j][p] == '1') {
                cout << p + 1 << " " << v + 1 << " " << j + 1;
                exit(0);
            }
            if(!used[j]) dfs(j, v);
        }
    }
}

signed main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin >> n;
    g.resize(n, vector<char>(n));
    used.resize(n);
    for (auto &i:g)
        for (auto &j:i)
            cin >> j;
    for (int i = 0; i < n; ++i) {
        dfs(i, -1);
    }
    cout << "-1";
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=5e3+5;
int n,m,num[maxn],flag=0;
char s[maxn][maxn];
void dfs(int u,int fa)
{
	num[u]=1;
	for(int i=1;i<=n;++i)
	{
		if(s[u][i]=='1')
		{
			if(!flag&&s[i][fa]=='1') {
				printf("%d %d %d",fa,u,i);
				flag=1;
				return ;
			}
			if(!num[i]) dfs(i,u);
		}
	}
}
int main()
{
	ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    scanf("%d",&n);
    for(int i=1;i<=n;++i)
    {
    	for(int j=1;j<=n;++j)
    	cin>>s[i][j];
	}
	for(int i=1;i<=n;++i)
	if(!num[i]) dfs(i,0);
	if(!flag) puts("-1");
 }


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=5e3+5;
int n,m,num[maxn],flag=0;
char str[maxn],s[maxn][maxn];
void dfs(int u,int fa)
{
	num[u]=1;
	for(int i=1;i<=n;++i)
	{
		if(s[u][i]=='1')
		{
			if(!flag&&s[i][fa]=='1') {
				printf("%d %d %d",fa,u,i);
				flag=1;
				return ;
			}
			if(!num[i]) dfs(i,u);
		}
	}
}
int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;++i)
    {
    	scanf("%s",str+1);
    	for(int j=1;j<=n;++j)
    	s[i][j]=str[j];
	}
	for(int i=1;i<=n;++i)
	if(!num[i]) dfs(i,0);
	if(!flag) puts("-1");
 }


#include <bits/stdc++.h>
#define ll long long
#define F first
#define S second
#define fort(i,a,b) for(ll i=(a);i<=(b);++i)
#define ford(i,a,b) for(ll i=(a);i>=(b);--i)
#define N 5005

using namespace std;

ll n,vis[N],ok;
char a[N][N];

void lui(ll u,ll p){
    vis[u]=1;
    if (ok) return;
    fort(v,1,n) if (a[u][v]=='1'){
        if (!ok && a[v][p]=='1'){
            cout<<p<<" "<<u<<" "<<v;
            ok=1;
            return;
        }
        if (!vis[v]){
                lui(v,u);
        }
    }
}

int main()
{

    cin>>n;
    fort(i,1,n) fort(j,1,n)
        cin>>a[i][j];

    fort(u,1,n) if (!vis[u]) lui(u,0);
    if (!ok) cout<<-1;

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=5e3+5;
int n,m,num[maxn],flag=0;
char s[maxn][maxn];
void dfs(int u,int fa)
{
	num[u]=1;
	for(int i=1;i<=n;++i)
	{
		if(s[u][i]=='1')
		{
			if(!flag&&s[i][fa]=='1') {
				printf("%d %d %d",fa,u,i);
				flag=1;
				return ;
			}
			if(!num[i]) dfs(i,u);
		}
	}
}
int main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
//   freopen("t.inp","r",stdin);
 //   freopen("t.out","w",stdout);

    scanf("%d",&n);
    for(int i=1;i<=n;++i)
    {
    	for(int j=1;j<=n;++j)
    	cin>>s[i][j];
	}
	for(int i=1;i<=n;++i)
	if(!num[i]) dfs(i,0);
	if(!flag) puts("-1");
 }


#include<bits/stdc++.h>
using namespace std;

int g[5001][5001];
vector<int> adj[5001];
int main(){
    ios_base::sync_with_stdio(false); cin.tie(0);
    int n; cin >> n;
    for(int i = 1; i <= n; ++i){
        string s; cin >> s;
        for(int j = 1; j <= n; ++j){
            if(s[j-1] == '1'){
                g[i][j] = 1;
                adj[i].push_back(j);
            }
        }
    }
    for(int i = 1; i <= n; ++i){
        for(auto u: adj[i]){
            for(auto v: adj[u]){
                if(g[v][i]){
                    cout << i << ' ' << u  << ' ' << v << '\n';
                    return 0;
                }
            }
        }
    }
    cout << -1;
}


#include<bits/stdc++.h>
using namespace std;

int g[5001][5001];
vector<int> adj[5001];
int cnt[5001];
int main(){
    ios_base::sync_with_stdio(false); cin.tie(0);
    int n; cin >> n;
    for(int i = 1; i <= n; ++i){
        string s; cin >> s;
        for(int j = 1; j <= n; ++j){
            if(s[j-1] == '1'){
                g[i][j] = 1;
                adj[i].push_back(j);
                cnt[i]++;
            }
        }
    }
    for(int i = 1; i <= n; ++i){
        for(auto u: adj[i]){
            if(cnt[u] >= cnt[i]){
                for(auto v: adj[u]){
                    if(g[v][i]){
                        cout << i << ' ' << u  << ' ' << v << '\n';
                        return 0;
                    }
                }
            }
        }
    }
    cout << -1;
}


#include <bits/stdc++.h>

using namespace std;

int n, ansnode = -1;
vector< bitset<5001> > b;
bool found = false;

vector<int> ans;
vector<bool> vis;

void dfs(int v, int h) {
    if (h > 3)
        return;
    vis[v] = true;
    for (int i = 0; i < n; i++) {
        if (b[v][i] == 1 && !vis[i]) {
            dfs(i, h + 1);
            if (found) {
                ans.push_back(v + 1);
                return;
            }
        }
        if (b[v][i] == 1 && i == ansnode) {
            found = true;
            ans.push_back(v + 1);
            return;
        }
    }
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cin >> n;
    b.resize(n);
    string s;
    for (int i = 0; i < n; i++) {
        cin >> s;
        for (int j = 0; j < n; j++)
            b[i][j] = ((s[j] - '0') ? 1 : 0);
    }

    for (int i = 0; i < n && ansnode == -1; i++) {
        bitset<5001> b2;
        for (int j = 0; j < n; j++) {
            if (b[i][j] == 1)
                b2 |= b[j];
        }
        for (int j = 0; j < n; j++) {
            if (b2[j] == 1 && b[j][i] == 1)
                ansnode = i;
        }
    }

    if (ansnode == -1)
        cout << -1;
    else {
        vis.resize(n, false);
        dfs(ansnode, 1);
        cout << ans[2] << " " << ans[1] << " " << ans[0];
    }

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int n, ansnode = -1;
vector< bitset<5001> > b;
bool found = false;

vector<int> ans;
vector<bool> vis;

void dfs(int v, int h) {
    if (h > 3)
        return;
    vis[v] = true;
    for (int i = 0; i < n && !found; i++) {
        if (i == v)
            continue;
        if (b[v][i] == 1 && !vis[i]) {
            dfs(i, h + 1);
            if (found) {
                ans.push_back(v + 1);
                return;
            }
        }
        if (b[v][i] == 1 && i == ansnode) {
            found = true;
            ans.push_back(v + 1);
            return;
        }
    }
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cin >> n;
    b.resize(n);
    string s;
    for (int i = 0; i < n; i++) {
        cin >> s;
        for (int j = 0; j < n; j++)
            b[i][j] = ((s[j] - '0') ? 1 : 0);
    }

    for (int i = 0; i < n && ansnode == -1; i++) {
        bitset<5001> b2;
        for (int j = 0; j < n; j++) {
            if (b[i][j] == 1)
                b2 |= b[j];
        }
        for (int j = 0; j < n; j++) {
            if (b2[j] == 1 && b[j][i] == 1)
                ansnode = i;
        }
    }

    if (ansnode == -1)
        cout << -1;
    else {
        vis.resize(n, false);
        dfs(ansnode, 1);
        cout << ans[2] << " " << ans[1] << " " << ans[0];
    }

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int n, ansnode = -1;
vector< bitset<5001> > b;
bool found = false;

vector<int> ans;
vector<bool> vis;

void dfs(int v, int h) {
    if (h > 3)
        return;
    vis[v] = true;
    for (int i = 0; i < n && !found; i++) {
        if (i == v)
            continue;
        if (b[v][i] == 1 && !vis[i]) {
            dfs(i, h + 1);
            if (found) {
                ans.push_back(v + 1);
                return;
            }
        }
        if (b[v][i] == 1 && i == ansnode) {
            found = true;
            ans.push_back(v + 1);
            return;
        }
    }
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cin >> n;
    b.resize(n);
    string s;
    for (int i = 0; i < n; i++) {
        cin >> s;
        for (int j = 0; j < n; j++)
            b[i][j] = ((s[j] - '0') ? 1 : 0);
    }

    for (int i = 0; i < n && ansnode == -1; i++) {
        bitset<5001> b2(0);
        for (int j = 0; j < n; j++) {
            if (b[i][j] == 1)
                b2 |= b[j];
        }
        for (int j = 0; j < n; j++) {
            if (b2[j] == 1 && b[j][i] == 1) {
                cout << i + 1 << " ";
                for (int y = 0; y < n; y++) {
                    if (b[i][y] == 1 && b[y][j] == 1) {
                        cout << y + 1 << " ";
                        break;
                    }
                }
                cout << j + 1;
                return 0;
            }
        }
    }

    cout << -1;

    return 0;
}


//agrawal117
//chahatagrawal117
#include<bits/stdc++.h>
#define endl '\n'
#define mod 1000000007
typedef long long int ll;
using namespace std;
#define MAX 5005
char arr[MAX][MAX];
vector<int> v[MAX];
int vis[MAX];
vector<int> nodes;
void answer(int end, int start)
{
    vector<int> temp;
    for(int i=0;i<nodes.size();i++){
        if(nodes[i]==start){
            for(int j=i;j<nodes.size();j++) temp.push_back(nodes[j]);
            break;
        }
    }
    while(temp.size()>3){
        int idx=temp.size()-2;
        if(arr[temp[0]][temp[idx]]=='1') { cout<<temp[0]<<" "<<temp[idx]<<" "<<temp[idx+1]; exit(0);}
        temp.pop_back();
    }
    cout<<temp[0]<<" "<<temp[1]<<" "<<temp[2];
    exit(0);

}
void dfs(int node)
{
    vis[node]=0;
    nodes.push_back(node);
    for(auto i: v[node]){

        if(vis[i]==-1) {
            dfs(i);
        }
        else if(vis[i]==0){
            answer(node,i);
        }
    }
    vis[node]=1;
}
int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int n; cin>>n;
    for(int i=1;i<=n;i++) {
        for(int j=1;j<=n;j++) {
            cin>>arr[i][j];
            if(arr[i][j]=='1') v[i].push_back(j);
        }
    }
    memset(vis,-1,sizeof(vis));
    for(int i=1;i<=n;i++){
        if(vis[i]==-1)  nodes.clear() , dfs(i);
    }
    cout<<-1<<endl;
}


//agrawal117
//chahatagrawal117
#include<bits/stdc++.h>
#define endl '\n'
#define mod 1000000007
typedef long long int ll;
using namespace std;
#define MAX 5005
char arr[MAX][MAX];
vector<int> v[MAX];
int vis[MAX];
vector<int> nodes;
void answer(int end, int start)
{
    vector<int> temp;
    for(int i=0;i<nodes.size();i++){
        if(nodes[i]==start){
            for(int j=i;j<nodes.size();j++) temp.push_back(nodes[j]);
            break;
        }
    }
    // for(int i=0;i<temp.size();i++) cout<<temp[i]<<" ";
    // cout<<endl;
    while(temp.size()>3){
        int idx=temp.size()-2;
        if(arr[temp[0]][temp[idx]]=='1') { cout<<temp[0]<<" "<<temp[idx]<<" "<<temp[idx+1]; exit(0);}
        temp.pop_back();
    }
    cout<<temp[0]<<" "<<temp[1]<<" "<<temp[2];
    exit(0);

}
void dfs(int node)
{
    vis[node]=0;
    nodes.push_back(node);
    // cout<<node<<endl;
    for(auto i: v[node]){

        if(vis[i]==-1) {
            dfs(i);
        }
        else if(vis[i]==0){
            answer(node,i);
        }
    }
    vis[node]=1;
    nodes.pop_back();
}
int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int n; cin>>n;
    for(int i=1;i<=n;i++) {
        for(int j=1;j<=n;j++) {
            cin>>arr[i][j];
            if(arr[i][j]=='1') v[i].push_back(j);
        }
    }
    memset(vis,-1,sizeof(vis));
    for(int i=1;i<=n;i++){
        if(vis[i]==-1)  nodes.clear() , dfs(i);
    }
    cout<<-1<<endl;
}


#include<bits/stdc++.h>
using namespace std;
char str[5003][5003];
bool flag;
int par[5003], vis[5003];
bool run[5003];
vector<int>vec;
void dfs(int x, int n)
{
    vis[x]= 1;
    run[x]= 1;
    for(int i=1; i<=n && !flag; i++)
    if(i!=x && str[x][i]=='1')
    {
        if(!vis[i])par[i]= x, dfs(i, n);
        else if(run[x])
        {
            flag= 1;
            int p= x;
            while(p!=i)
            {
                vec.push_back(p);
                p= par[p];
            }
            vec.push_back(i);
            reverse(vec.begin(), vec.end());
        }
    }
    run[x]= 0;
    return;
}
int main()
{
    int n;
    scanf("%d", &n);
    for(int i=1; i<=n; i++)scanf("%s", str[i]+1);
    for(int i=1; i<=n && !flag; i++)
    if(!vis[i])dfs(i, n);
    if(!flag)puts("-1");
    else
    {
        vec.push_back(vec[1]);
        for(int i=3; i<vec.size(); i++)
        {
            int x= vec[0];
            int y= vec[2];
            int z= vec[i];
            if(str[x][z]=='1' && str[z][y]=='1' && str[y][x]=='1')
            {
                printf("%d %d %d\n", x, y, z);
                return 0;
            }
        }
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
char str[5003][5003];
bool flag;
int par[5003], vis[5003];
bool run[5003];
vector<int>vec;
void dfs(int x, int n)
{
    vis[x]= 1;
    run[x]= 1;
    for(int i=1; i<=n && !flag; i++)
    if(str[x][i]=='1')
    {
        if(!vis[i])par[i]= x, dfs(i, n);
        else if(run[i])
        {
            flag= 1;
            int p= x;
            while(p!=i)
            {
                vec.push_back(p);
                p= par[p];
            }
            vec.push_back(i);
            reverse(vec.begin(), vec.end());
        }
    }
    run[x]= 0;
    return;
}
int main()
{
    int n;
    scanf("%d", &n);
    for(int i=1; i<=n; i++)scanf("%s", str[i]+1);
    for(int i=1; i<=n && !flag; i++)
    if(!vis[i])dfs(i, n);
    if(!flag)puts("-1");
    else
    {
        vec.push_back(vec[1]);
        for(int i=3; i<vec.size(); i++)
        {
            int x= vec[0];
            int y= vec[2];
            int z= vec[i];
            if(str[x][z]=='1' && str[z][y]=='1' && str[y][x]=='1')
            {
                printf("%d %d %d\n", x, z, y);
                return 0;
            }
        }
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
char str[5003][5003];
bool flag;
int par[5003], vis[5003];
bool run[5003];
vector<int>vec;
void dfs(int x, int n)
{
    vis[x]= 1;
    run[x]= 1;
    for(int i=1; i<=n && !flag; i++)
    if(i!=x && str[x][i]=='1')
    {
        if(!vis[i])par[i]= x, dfs(i, n);
        else if(run[x])
        {
            flag= 1;
            int p= x;
            while(p!=i)
            {
                vec.push_back(p);
                p= par[p];
            }
            vec.push_back(i);
            reverse(vec.begin(), vec.end());
            for(int i=0; i<vec.size(); i++)cerr<<vec[i]<<endl;
        }
    }
    run[x]= 0;
    return;
}
int main()
{
    int n;
    scanf("%d", &n);
    for(int i=1; i<=n; i++)scanf("%s", str[i]+1);
    for(int i=1; i<=n && !flag; i++)
    if(!vis[i])dfs(i, n);
    if(!flag)puts("-1");
    else
    {
        vec.push_back(vec[1]);
        for(int i=3; i<vec.size(); i++)
        {
            int x= vec[0];
            int y= vec[2];
            int z= vec[i];
            if(str[x][z]=='1' && str[z][y]=='1' && str[y][x]=='1')
            {
                printf("%d %d %d\n", x, z, y);
                return 0;
            }
        }
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
char str[5003][5003];
bool flag;
int par[5003], vis[5003];
bool run[5003];
vector<int>vec;
void dfs(int x, int n)
{
    vis[x]= 1;
    run[x]= 1;
    for(int i=1; i<=n && !flag; i++)
    if(str[x][i]=='1')
    {
        if(!vis[i])par[i]= x, dfs(i, n);
        else if(run[i])
        {
            flag= 1;
            int p= x;
            while(p!=i)
            {
                vec.push_back(p);
                p= par[p];
            }
            vec.push_back(i);
            reverse(vec.begin(), vec.end());
        }
    }
    run[x]= 0;
    return;
}
int main()
{
    int n;
    scanf("%d", &n);
    for(int i=1; i<=n; i++)scanf("%s", str[i]+1);
    for(int i=1; i<=n && !flag; i++)
    if(!vis[i])dfs(i, n);
    if(!flag)puts("-1");
    else
    {
        vec.push_back(vec[0]);
        vec.push_back(vec[1]);
        for(int i=0; i<vec.size()-2; i++)
        {
            int x= vec[i];
            int y= vec[i+2];
            for(int z=1; z<=n; z++)
            if(z!=x && z!=y && str[x][z]=='1' && str[z][y]=='1' && str[y][x]=='1')
            {
                printf("%d %d %d\n", x, z, y);
                return 0;
            }
        }
    }
    return 0;
}


#include <iostream>
#include <vector>
#include <cmath>
#include <set>
#include <algorithm>
#include <cassert>
#include <map>
#include <queue>
#include <bitset>
#define X first
#define Y second
#define int long long
#define pii pair<int, int>
#define vi vector<int>
#define vvi vector<vi>
#define pb push_back
#define mp make_pair
#define all(a) a.begin(), a.end()
using namespace std;
const int N = 5000;

vi g[N];
int a[N][N], deep[N], p[N];
set<pii> q;
vi used;

void dfs(int v, int gl, int pr, int c) {
	used[v] = c;
	deep[v] = gl;
	p[v] = pr;
	for (int to : g[v])
		if (!used[to])
			dfs(to, gl + 1, v, c), q.erase(mp(to, v)), q.erase(mp(v, to));
}

void solve() {
	int n;
	cin >> n;
	for (int i = 0; i < n; ++i) {
		string s;
		cin >> s;
		for (int j = 0; j < n; ++j)
			if (s[j] == '1') {
				g[i].pb(j);
				g[j].pb(i);
				a[i][j] = 1;
				q.insert(mp(i, j));
			}
	}
	used.assign(n, 0);
	int col = 1;
	for (int i = 0; i < n; ++i)
		if (!used[i])
			dfs(i, 0, i, col++);
	for (auto to : q)
		if (used[to.X] == used[to.Y] && abs(deep[to.X] - deep[to.Y]) == 2) {
			if (deep[to.X] - deep[to.Y] == 2) {
				vi v = {to.X, to.Y, p[to.X]};
				sort(all(v));
				do {
					if (a[v[0]][v[1]] && a[v[1]][v[2]] && a[v[2]][v[0]]) {
						cout << v[0] + 1 << " " << v[1] + 1 << " " << v[2] + 1;
						return;
					}
				} while (next_permutation(all(v)));
			}
		}
	cout << -1;
}

int32_t main() {
	ios_base::sync_with_stdio(NULL);
	cin.tie(0); cout.tie(0);
	int t = 1;
//	cin >> t;
	while (t--)
		solve();
}


#include <iostream>
#include <vector>
#include <cmath>
#include <set>
#include <algorithm>
#include <cassert>
#include <map>
#include <queue>
#include <bitset>
#define X first
#define Y second
#define int long long
#define pii pair<int, int>
#define vi vector<int>
#define vvi vector<vi>
#define pb push_back
#define mp make_pair
#define all(a) a.begin(), a.end()
using namespace std;
const int N = 5000;

vi g[N];
int a[N][N];
vi used, path, u2;

void obr() {
	while (path.size() > 3) {
		int n = path.size();
		if (a[n - 1][path[n - 3]]) {
			cout << path[n - 3] + 1 << " " << path[n - 2] + 1 << " " << path[n - 1] + 1 << "\n";
			exit(0);
		}
		int x = path[n - 1];
		path.pop_back();
		path.pop_back();
		path.pb(x);
	}
	cout << path[0] + 1 << " " << path[1] + 1 << " " << path[2] + 1 << "\n";
	exit(0);
}

void dfs(int v, int p) {
	used[v] = 1;
	u2[v] = 1;
	path.pb(v);
	for (int to : g[v])
		if (u2[to] && to != p)
			obr();
	for (int to : g[v])
		if (!used[to])
			dfs(to, v);
	path.pop_back();
	u2[v] = 0;
}

void solve() {
	int n;
	cin >> n;
	for (int i = 0; i < n; ++i) {
		string s;
		cin >> s;
		for (int j = 0; j < n; ++j)
			if (s[j] == '1') {
				g[i].pb(j);
				a[i][j] = 1;
			}
	}
	used.assign(n, 0);
	u2.assign(n, 0);
	for (int i = 0; i < n; ++i)
		if (!used[i])
			dfs(i, -1);
	cout << -1;
}

int32_t main() {
	ios_base::sync_with_stdio(NULL);
	cin.tie(0); cout.tie(0);
	int t = 1;
//	cin >> t;
	while (t--)
		solve();
}


#include <iostream>
#include <vector>
#include <cmath>
#include <set>
#include <algorithm>
#include <cassert>
#include <map>
#include <queue>
#include <bitset>
#define X first
#define Y second
#define int long long
#define pii pair<int, int>
#define vi vector<int>
#define vvi vector<vi>
#define pb push_back
#define mp make_pair
#define all(a) a.begin(), a.end()
using namespace std;
const int N = 5000;

vi g[N];
int a[N][N];
vi used, path, u2;

void obr() {
	while (path.size() > 3) {
		int n = path.size();
		if (a[path[n - 1]][path[n - 3]]) {
			cout << path[n - 3] + 1 << " " << path[n - 2] + 1 << " " << path[n - 1] + 1 << "\n";
			exit(0);
		}
		int x = path[n - 1];
		path.pop_back();
		path.pop_back();
		path.pb(x);
	}
	cout << path[0] + 1 << " " << path[1] + 1 << " " << path[2] + 1 << "\n";
	exit(0);
}

void dfs(int v, int p) {
	used[v] = 1;
	u2[v] = 1;
	path.pb(v);
	for (int to : g[v])
		if (u2[to] && to != p) {
			vi p2;
			bool ok = 0;
			for (int x : path) {
				if (x == to)
					ok = 1;
				if (ok)
					p2.pb(x);
			}
			path = p2;
			obr();
		}
	for (int to : g[v])
		if (!used[to])
			dfs(to, v);
	path.pop_back();
	u2[v] = 0;
}

void solve() {
	int n;
	cin >> n;
	for (int i = 0; i < n; ++i) {
		string s;
		cin >> s;
		for (int j = 0; j < n; ++j)
			if (s[j] == '1') {
				g[i].pb(j);
				a[i][j] = 1;
			}
	}
	used.assign(n, 0);
	u2.assign(n, 0);
	for (int i = 0; i < n; ++i)
		if (!used[i])
			dfs(i, -1);
	cout << -1;
}

int32_t main() {
	ios_base::sync_with_stdio(NULL);
	cin.tie(0); cout.tie(0);
	int t = 1;
//	cin >> t;
	while (t--)
		solve();
}


#include <iostream>
#include <vector>
#include <cmath>
#include <set>
#include <algorithm>
#include <cassert>
#include <map>
#include <queue>
#include <bitset>
#define X first
#define Y second
#define pii pair<int, int>
#define vi vector<int>
#define vvi vector<vi>
#define pb push_back
#define mp make_pair
#define all(a) a.begin(), a.end()
using namespace std;
const int N = 5000;

vi g[N];
bool a[N][N];
vi used, path, u2;

void obr() {
	while (path.size() > 3) {
		int n = path.size();
		if (a[path[n - 1]][path[n - 3]]) {
			cout << path[n - 3] + 1 << " " << path[n - 2] + 1 << " " << path[n - 1] + 1 << "\n";
			exit(0);
		}
		int x = path[n - 1];
		path.pop_back();
		path.pop_back();
		path.pb(x);
	}
	cout << path[0] + 1 << " " << path[1] + 1 << " " << path[2] + 1 << "\n";
	exit(0);
}

void dfs(int v, int p) {
	used[v] = 1;
	u2[v] = 1;
	path.pb(v);
	for (int to : g[v])
		if (u2[to] && to != p) {
			vi p2;
			bool ok = 0;
			for (int x : path) {
				if (x == to)
					ok = 1;
				if (ok)
					p2.pb(x);
			}
			path = p2;
			obr();
		}
	for (int to : g[v])
		if (!used[to])
			dfs(to, v);
	path.pop_back();
	u2[v] = 0;
}

void solve() {
	int n;
	cin >> n;
	for (int i = 0; i < n; ++i) {
		string s;
		cin >> s;
		for (int j = 0; j < n; ++j)
			if (s[j] == '1') {
				g[i].pb(j);
				a[i][j] = 1;
			}
	}
	used.assign(n, 0);
	u2.assign(n, 0);
	for (int i = 0; i < n; ++i)
		if (!used[i])
			dfs(i, -1);
	cout << -1;
}

int32_t main() {
	ios_base::sync_with_stdio(NULL);
	cin.tie(0); cout.tie(0);
	int t = 1;
//	cin >> t;
	while (t--)
		solve();
}


/* Manish Pant - insider_pants */

#include <bits/stdc++.h>
using namespace std;

#define F first
#define S second
#define endl "\n"
#define pb push_back

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

template <class T> ostream& operator << (ostream &os, const vector<T> &v) { for (T i : v) os << i << ", "; return os; }
template <class T> ostream& operator << (ostream &os, const set<T> &v) { for (T i : v) os << i << " "; return os; }
template <class T> ostream& operator << (ostream &os, const multiset<T> &v) { for (T i : v) os << i << " "; return os; }
template <class T, class S> ostream& operator << (ostream &os, const pair<T, S> &v) { os << v.F << ' ' << v.S; return os; }
template <class T, class S> ostream& operator << (ostream &os, const map<T, S> &v) { for (auto i : v) os << '(' << i.F << " => " << i.S << ')' << ' '; return os; }

void fast()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
}

const int N = 5e3 + 5;
vector<string> v;
vector<bool> vis(N, 0);
int n;

void dfs(int a, int prev)
{
    if(vis[a]) return;
    vis[a] = 1;
    for(int i = 0; i < n; i++)
    {
        if(v[a][i] == '0') continue;
        if(prev != -1 && v[i][prev] == '1')
        {
            cout<<i + 1<<' '<<prev + 1<<" "<<a + 1<<endl;
            exit(0);
        }
        dfs(i, a);
    }
}

void solve()
{
    for(int i = 0; i < n; i++)
    {
        if(vis[i]) continue;
        dfs(i, -1);
    }
    cout<<-1<<endl;
}

int main()
{
    fast();
    cin>>n;
    for(int i = 1; i <= n; i++)
    {
        string s; cin>>s;
        v.pb(s);
    }
    solve();
}


/* Manish Pant - insider_pants */

#include <bits/stdc++.h>
using namespace std;

#define F first
#define S second
#define endl "\n"
#define pb push_back

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

template <class T> ostream& operator << (ostream &os, const vector<T> &v) { for (T i : v) os << i << ", "; return os; }
template <class T> ostream& operator << (ostream &os, const set<T> &v) { for (T i : v) os << i << " "; return os; }
template <class T> ostream& operator << (ostream &os, const multiset<T> &v) { for (T i : v) os << i << " "; return os; }
template <class T, class S> ostream& operator << (ostream &os, const pair<T, S> &v) { os << v.F << ' ' << v.S; return os; }
template <class T, class S> ostream& operator << (ostream &os, const map<T, S> &v) { for (auto i : v) os << '(' << i.F << " => " << i.S << ')' << ' '; return os; }

void fast()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
}

const int N = 5e3 + 5;
array<set<int>, N> v;
int n;

void solve()
{
    for(int i = 1; i <= n; i++)
    {
        for(int j:v[i])
        {
            if(j == i) continue;
            for(int k:v[j])
            {
                if(k == j) continue;
                if(v[k].find(i) != v[k].end())
                {
                    cout<<i<<' '<<j<<' '<<k<<endl;
                    return;
                }
            }
        }
    }
    cout<<-1<<endl;
}

int main()
{
    fast();
    cin>>n;
    for(int i = 1; i <= n; i++)
    {
        string s; cin>>s;
        for(int j = 1; j <= n; j++)
        {
            if(s[j - 1] == '1') v[i].insert(j);
        }
    }
    solve();
}


/* Manish Pant - insider_pants */

#include <bits/stdc++.h>
using namespace std;

#define F first
#define S second
#define endl "\n"
#define pb push_back

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

template <class T> ostream& operator << (ostream &os, const vector<T> &v) { for (T i : v) os << i << ", "; return os; }
template <class T> ostream& operator << (ostream &os, const set<T> &v) { for (T i : v) os << i << " "; return os; }
template <class T> ostream& operator << (ostream &os, const multiset<T> &v) { for (T i : v) os << i << " "; return os; }
template <class T, class S> ostream& operator << (ostream &os, const pair<T, S> &v) { os << v.F << ' ' << v.S; return os; }
template <class T, class S> ostream& operator << (ostream &os, const map<T, S> &v) { for (auto i : v) os << '(' << i.F << " => " << i.S << ')' << ' '; return os; }

void fast()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
}

const int N = 5e3 + 5;
array<set<int>> v(N);
int n;

void solve()
{
    for(int i = 1; i <= n; i++)
    {
        for(int j:v[i])
        {
            if(j == i) continue;
            for(int k:v[j])
            {
                if(k == j) continue;
                if(v[k].find(i) != v[k].end())
                {
                    cout<<i<<' '<<j<<' '<<k<<endl;
                    return;
                }
            }
        }
    }
    cout<<-1<<endl;
}

int main()
{
    fast();
    cin>>n;
    for(int i = 1; i <= n; i++)
    {
        string s; cin>>s;
        for(int j = 1; j <= n; j++)
        {
            if(s[j - 1] == '1') v[i].insert(j);
        }
    }
    solve();
}


/* Manish Pant - insider_pants */

#include <bits/stdc++.h>
using namespace std;

#define F first
#define S second
#define endl "\n"
#define pb push_back

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

template <class T> ostream& operator << (ostream &os, const vector<T> &v) { for (T i : v) os << i << ", "; return os; }
template <class T> ostream& operator << (ostream &os, const set<T> &v) { for (T i : v) os << i << " "; return os; }
template <class T> ostream& operator << (ostream &os, const multiset<T> &v) { for (T i : v) os << i << " "; return os; }
template <class T, class S> ostream& operator << (ostream &os, const pair<T, S> &v) { os << v.F << ' ' << v.S; return os; }
template <class T, class S> ostream& operator << (ostream &os, const map<T, S> &v) { for (auto i : v) os << '(' << i.F << " => " << i.S << ')' << ' '; return os; }

void fast()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
}

const int N = 5e3 + 5;
vector<string> v;
int n;

void solve()
{
    if(n < 3)
    {
        cout<<-1<<endl;
        return;
    }

    for(int i = 0; i < n; i++)
    {
        for(int j = 0; j < n; j++)
        {
            if(v[i][j] == '0') continue;
            for(int k = 0; k < n; j++)
            {
                if(v[j][k] == '0') continue;
                if(v[k][i] == '1')
                {
                    cout<<i + 1<<" "<<j + 1<<" "<<k + 1<<endl;
                    return;
                }
            }
        }
    }
    cout<<-1<<endl;
}

int main()
{
    fast();
    cin>>n;
    for(int i = 1; i <= n; i++)
    {
        string s; cin>>s;
        v.pb(s);
    }
    solve();
}


/* Manish Pant - insider_pants */

#include <bits/stdc++.h>
using namespace std;

#define F first
#define S second
#define endl "\n"
#define pb push_back

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

template <class T> ostream& operator << (ostream &os, const vector<T> &v) { for (T i : v) os << i << ", "; return os; }
template <class T> ostream& operator << (ostream &os, const set<T> &v) { for (T i : v) os << i << " "; return os; }
template <class T> ostream& operator << (ostream &os, const multiset<T> &v) { for (T i : v) os << i << " "; return os; }
template <class T, class S> ostream& operator << (ostream &os, const pair<T, S> &v) { os << v.F << ' ' << v.S; return os; }
template <class T, class S> ostream& operator << (ostream &os, const map<T, S> &v) { for (auto i : v) os << '(' << i.F << " => " << i.S << ')' << ' '; return os; }

void fast()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
}

const int N = 5e3 + 5;
vector<string> v;
int n;

void solve()
{
    if(n < 3)
    {
        cout<<-1<<endl;
        return;
    }

    for(int i = 0; i < n; i++)
    {
        for(int j = 0; j < n; j++)
        {
            if(v[i][j] == '0') continue;
            for(int k = 0; k < n; k++)
            {
                if(v[j][k] == '0') continue;
                if(v[k][i] == '1')
                {
                    cout<<i + 1<<" "<<j + 1<<" "<<k + 1<<endl;
                    return;
                }
            }
        }
    }
    cout<<-1<<endl;
}

int main()
{
    fast();
    cin>>n;
    for(int i = 1; i <= n; i++)
    {
        string s; cin>>s;
        v.pb(s);
    }
    solve();
}


/* Manish Pant - insider_pants */

#include <bits/stdc++.h>
using namespace std;

#define F first
#define S second
#define endl "\n"
#define pb push_back

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

template <class T> ostream& operator << (ostream &os, const vector<T> &v) { for (T i : v) os << i << ", "; return os; }
template <class T> ostream& operator << (ostream &os, const set<T> &v) { for (T i : v) os << i << " "; return os; }
template <class T> ostream& operator << (ostream &os, const multiset<T> &v) { for (T i : v) os << i << " "; return os; }
template <class T, class S> ostream& operator << (ostream &os, const pair<T, S> &v) { os << v.F << ' ' << v.S; return os; }
template <class T, class S> ostream& operator << (ostream &os, const map<T, S> &v) { for (auto i : v) os << '(' << i.F << " => " << i.S << ')' << ' '; return os; }

void fast()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
}

const int N = 5e3 + 5;
vector<set<int>> v(N);
int n;

void solve()
{
    for(int i = 1; i <= n; i++)
    {
        for(int j:v[i])
        {
            if(j == i) continue;
            for(int k:v[j])
            {
                if(k == j) continue;
                if(v[k].find(i) != v[k].end())
                {
                    cout<<i<<' '<<j<<' '<<k<<endl;
                    return;
                }
            }
        }
    }
    cout<<-1<<endl;
}

int main()
{
    fast();
    cin>>n;
    for(int i = 1; i <= n; i++)
    {
        string s; cin>>s;
        for(int j = 1; j <= n; j++)
        {
            if(s[j - 1] == '1') v[i].insert(j);
        }
    }
    solve();
}


/* Manish Pant - insider_pants */

#include <bits/stdc++.h>
using namespace std;

#define F first
#define S second
#define endl "\n"
#define pb push_back

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

template <class T> ostream& operator << (ostream &os, const vector<T> &v) { for (T i : v) os << i << ", "; return os; }
template <class T> ostream& operator << (ostream &os, const set<T> &v) { for (T i : v) os << i << " "; return os; }
template <class T> ostream& operator << (ostream &os, const multiset<T> &v) { for (T i : v) os << i << " "; return os; }
template <class T, class S> ostream& operator << (ostream &os, const pair<T, S> &v) { os << v.F << ' ' << v.S; return os; }
template <class T, class S> ostream& operator << (ostream &os, const map<T, S> &v) { for (auto i : v) os << '(' << i.F << " => " << i.S << ')' << ' '; return os; }

void fast()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
}

const int N = 5e3 + 5;
vector<string> v;
vector<bool> vis(N, 0);
int n;

void dfs(int a, int prev)
{
    if(vis[a]) return;
    vis[a] = 1;
    for(int i = 0; i < n; i++)
    {
        if(v[a][i] == '0') continue;
        if(prev != -1 && v[i][prev] == '1')
        {
            cout<<i + 1<<' '<<a + 1<<" "<<prev + 1<<endl;
            exit(0);
        }
        dfs(i, a);
    }
}

void solve()
{
    for(int i = 0; i < n; i++)
    {
        if(vis[i]) continue;
        dfs(i, -1);
    }
    cout<<-1<<endl;
}

int main()
{
    fast();
    cin>>n;
    for(int i = 1; i <= n; i++)
    {
        string s; cin>>s;
        v.pb(s);
    }
    solve();
}


#pragma GCC optimize("O3")
#pragma GCC target("sse4")

#include <iostream>
#include <vector>
#include <algorithm>

#define pb push_back
#define mp make_pair

typedef long long ll;

using namespace std;

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);

	int N;
	cin >> N;

	vector< pair< int, int > > edges;
	int adj[N][N];
	for(int i = 0; i < N; i++) {
		string in;
		cin >> in;
		for(int j = 0; j < N; j++) {
			int connectivity = in.at(j) - '0';
			if(connectivity == 1) {
				edges.pb(mp(i, j));
			}
			adj[i][j] = connectivity;
		}
	}

	for(int i = 0; i < N; i++) {
		for(int j = 0; j < edges.size(); j++) {
			int f = edges[j].first;
			int s = edges[j].second;

			if(adj[i][f] == 1 && adj[s][i] == 1) {
				cout << i + 1 << ' ' << f + 1 << ' ' << s + 1<< endl;
				return 0;
			}
		}
	}
	cout << -1 << endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
int n ;
char arr[5050][5050];
bool visited[5050] ;
vector<vector<int>>adj(5050);
void dfs(int v , int p)
{
   visited[v] = true;
   for(int j = 0; j < n ; j++)
   {
       if (arr[v][j] == '1')
       {
           if (!visited[j]){
               dfs(j,v);}
           else if (arr[j][p] == '1')
           {
               printf("%d %d %d",p+1,v+1,j+1);
               exit(0);
           }
       }
   }
}
int main(){
    scanf("%d",&n);
    for (int i = 0;i<n;i++)
        scanf("%s",arr[i]);
    for(int i = 0;i<n;i++){
        if (!visited[i])
            dfs(i,-1);
    }
    printf("-1");
    return 0;
}


#pragma GCC optimize("O3")
#pragma GCC target("sse4")

#include <iostream>
#include <vector>
#include <algorithm>

#define pb push_back
#define mp make_pair

typedef long long ll;

using namespace std;

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);

	int N;
	cin >> N;

	int adj[N][N];
	for(int i = 0; i < N; i++) {
		string in;
		cin >> in;
		for(int j = 0; j < N; j++) {
			adj[i][j] = in.at(j) - '0';
		}
	}

	for(int i = 0; i < N; i++) {
		for(int j = 0; j < N; j++) {
			for(int k = 0; k < N; k++) {
				if(adj[i][j] == 1 && adj[j][k] == 1 && adj[k][i] == 1) {
					cout << i+1 << ' ' << j+1 << ' ' << k+1 << endl;
					return 0;
				}
			}
		}
	}
	cout << -1 << endl;
	return 0;
}


#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define mod 1000000009
using namespace std;

string edges[5001];
bool visited[5001];
bool found = false;
ll n;

void dfs(ll node, ll parent){
    visited[node] = true;
    for(ll i = 0; i < n; i++){
        if(edges[node][i] == '1'){
            if(edges[i][parent] == '1' && !found){
                cout << parent + 1 << " " << node + 1 << " " << i + 1 << "\n";
                found = true;
                break;
            }
            if(!visited[i] && !found)
                dfs(i, node);
        }
    }
}

int main() {
    ios_base :: sync_with_stdio(false);
    cin.tie(NULL);

    ll t = 1;
    // cin >> t;
    while(t--){
        memset(visited, false, sizeof(visited));
        cin >> n;

        for(ll i = 0; i < n; i++){
            cin >> edges[i];
        }

        for(ll i = 0; i < n; i++){
            if(!found)
                dfs(i, 0);
        }

        if(!found) cout << "-1\n";
    }
    return 0;
}


#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define mod 1000000009
using namespace std;

string edges[5001];
bool visited[5001];
bool found = false;
ll n;

void dfs(ll node, ll parent){
    visited[node] = true;
    for(ll i = 0; i < n; i++){
        if(edges[node][i] == '1'){
            if(edges[i][parent] == '1' && !found){
                cout << parent + 1 << " " << node + 1 << " " << i + 1 << "\n";
                found = true;
                break;
            }
            if(!visited[i] && !found)
                dfs(i, node);
        }
    }
}

int main() {
    ios_base :: sync_with_stdio(false);
    cin.tie(NULL);

    ll t = 1;
    // cin >> t;
    while(t--){
        memset(visited, false, sizeof(visited));
        cin >> n;

        for(ll i = 0; i < n; i++){
            cin >> edges[i];
        }

        for(ll i = 0; i < n; i++){
            if(!found)
                dfs(i, i);
        }

        if(!found) cout << "-1\n";
    }
    return 0;
}


#include<iostream>
#include<vector>
#include<algorithm>
#include<set>
#include<map>
#include<queue>
#include<cmath>
#include<iomanip>
#include<cstring>
#include<complex>
#include<cstdio>
#define initdp(a,b) for(int i=0;i<=a;i++)for(int j=0;j<=b;j++)dp[i][j]=-1;
#define fi first
#define se second
#define pb push_back
#define pii pair<int,int>
#define ppi pair<pii,int>
#define pip pair<int,pii>
#define ll long long
#define pll pair<ll,ll>
#define rep(i,n) for(int i=0;i<n;i++)
#define repd(i,n) for(int i=n-1;i>=0;i--)
#define inf 1000000001
#define inf1 1000000000000000001
#define mod 1000000007
#define pie 3.14159265358979323846
#define N 1000005
#define mid(l,r) l+(r-l)/2
using namespace std;
int dx[4]={1,0,-1,0},dy[4]={0,1,0,-1};
int ddx[8]={1,1,0,-1,-1,-1,0,1},ddy[8]={0,1,1,1,0,-1,-1,-1};
void mad(ll &a,ll b){a=(a+b)%mod;if(a<0)a+=mod;}
ll gcd(ll a,ll b){ if(!a)return b;return gcd(b%a,a);}
int vis[5005];
vector<int>ed[5005],ed1[5005];
int f=0,a,b,c;
void dfs(int u,int d){
	vis[u]=d;
	for(int v:ed[u]){
		if(vis[v]){
			if(vis[u]-vis[v]==2){
				//cout<<u<<" "<<v<<"\n";
				a=u;
				b=v;
				f=1;
				return;
			}
			continue;
		}
		if(f)return;
		dfs(v,d+1);
	}
}
void solve(){
	int n;
	cin>>n;
	for(int i=1;i<=n;i++){
		for(int j=1;j<=n;j++){
			char x;
			cin>>x;
			if(x=='1'){ed[i].pb(j);ed1[j].pb(i);}
		}
	}
	int d=1;
	for(int i=1;i<=n;i++){
		if(vis[i])continue;
		dfs(i,d);
		d+=n;
	}
	if(a==0){cout<<-1<<"\n";return;}
	//cout<<a<<' '<<b<<"\n";
	for(int u:ed1[a]){
		//cout<<u<<" ";
		for(int v:ed[b]){
			if(u==v){
				cout<<a<<" "<<b<<" "<<u<<"\n";
				return;
			}
		}
	}
	cout<<-1<<"\n";
}
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	int t;
	//cin>>t;
	t=1;
	while(t--){
		solve();
	}
}


#include<iostream>
#include<vector>
#include<algorithm>
#include<set>
#include<map>
#include<queue>
#include<cmath>
#include<iomanip>
#include<cstring>
#include<complex>
#include<cstdio>
#define initdp(a,b) for(int i=0;i<=a;i++)for(int j=0;j<=b;j++)dp[i][j]=-1;
#define fi first
#define se second
#define pb push_back
#define pii pair<int,int>
#define ppi pair<pii,int>
#define pip pair<int,pii>
#define ll long long
#define pll pair<ll,ll>
#define rep(i,n) for(int i=0;i<n;i++)
#define repd(i,n) for(int i=n-1;i>=0;i--)
#define inf 1000000001
#define inf1 1000000000000000001
#define mod 1000000007
#define pie 3.14159265358979323846
#define N 1000005
#define mid(l,r) l+(r-l)/2
using namespace std;
int dx[4]={1,0,-1,0},dy[4]={0,1,0,-1};
int ddx[8]={1,1,0,-1,-1,-1,0,1},ddy[8]={0,1,1,1,0,-1,-1,-1};
void mad(ll &a,ll b){a=(a+b)%mod;if(a<0)a+=mod;}
ll gcd(ll a,ll b){ if(!a)return b;return gcd(b%a,a);}
int vis[5005];
vector<int>ed[5005],ed1[5005];
int f=0,a,b,c;
void dfs(int u,int d){
	vis[u]=d;
	for(int v:ed[u]){
		if(vis[v]){
			if(vis[u]-vis[v]==2){
				//acout<<u<<" "<<v<<"\n";
				a=u;
				b=v;
				f=1;
				return;
			}
			continue;
		}
		if(f)return;
		dfs(v,d+1);
	}
}
void solve(){
	int n;
	cin>>n;
	for(int i=1;i<=n;i++){
		for(int j=1;j<=n;j++){
			char x;
			cin>>x;
			if(x=='1'){ed[i].pb(j);ed1[j].pb(i);}
		}
	}
	for(int i=1;i<=n;i++){
		if(vis[i])continue;
		dfs(i,1);
	}
	if(a==0){cout<<-1<<"\n";return;}
	for(int u:ed1[a]){
		//cout<<u<<" ";
		for(int v:ed[b]){
			if(u==v){
				cout<<a<<" "<<b<<" "<<u<<"\n";
				return;
			}
		}
	}
	cout<<-1<<"\n";
}
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	int t;
	//cin>>t;
	t=1;
	while(t--){
		solve();
	}
}


#include<bits/stdc++.h>
using namespace std;

int fin(int parent[],int u){
    if(parent[u] == u)
        return u;
    parent[u]=fin(parent,parent[u]);
    return parent[u];
}
void unio(int cost[],int parent[],int u,int v){
    int uu=fin(parent,u);
    int vv=fin(parent,v);
    if(cost[uu]<cost[vv]){
        parent[vv]=uu;
    }
    else{
        parent[uu]=vv;
    }
}
int main()
{
    int n,m;
    cin>>n>>m;
    int cost[n+1];
    int parent[n+1];
    for(int i=1;i<n+1;i++){
        cin>>cost[i];
    }
    for(int i=0;i<n+1;i++){
        parent[i]=i;
    }
    for(int i=0;i<m;i++){
        int u,v;
        cin>>u>>v;
        unio(cost,parent,u,v);
    }
    long long int res=0;

    for(int i=1;i<n+1;i++){
            res+=cost[fin(parent,i)];
            cost[fin(parent,i)]=0;
    }
    cout<<res;
}


#include<bits/stdc++.h>
using namespace std;
const int N=100007;
int cost[N];
int parent[N];
int vis[N];
int fin(int u){
    if(parent[u] == u)
        return u;
    parent[u]=fin(parent[u]);
    return parent[u];
}
void unio(int u,int v){
    int uu=fin(u);
    int vv=fin(v);
    if(cost[uu]<cost[vv]){
        parent[vv]=uu;
    }
    else{
        parent[uu]=vv;
    }
}
int main()
{
    int n,m;
    cin>>n>>m;
    for(int i=1;i<n+1;i++){
        cin>>cost[i];
    }
    for(int i=0;i<n+1;i++){
        parent[i]=i;
    }
    for(int i=0;i<m;i++){
        int u,v;
        cin>>u>>v;
        unio(u,v);
    }
    long long int res=0;

    for(int i=1;i<n+1;i++){
        if(!vis[fin(i)]){
            res+=cost[fin(i)];
            vis[fin(i)]=true;
        }
    }
    cout<<res;
}


#include<bits/stdc++.h>
using namespace std;
int fin(vector<long long int>parent,long long int u){
    if(parent[u]==u)
        return u;
    else
        return parent[u]=fin(parent,parent[u]);
}
void unio(vector<long long int>cost,vector<long long int> &parent,long long int uu,long long int vv){
    if(cost[uu]<cost[vv]){
        parent[vv]=uu;
    }
    else{
        parent[uu]=vv;
    }
}
int main()
{
    long long int n,m;
    cin>>n>>m;
    vector<long long int> cost(n+1);
    for(long long int i=1;i<n+1;i++){
        cin>>cost[i];
    }
    vector<long long int > parent(n+1);
    for(int i=1;i<n+1;i++){
        parent[i]=i;
    }
    for(long long int i=0;i<m;i++){
        long long int u,v;
        cin>>u>>v;
        int uu=fin(parent,u);
        int vv=fin(parent,v);
        if(uu!=vv)
            unio(cost,parent,uu,vv);
    }
    long long int res=0;
    for(int i=1;i<n+1;i++){
        res+=cost[parent[i]];
        cost[parent[i]]=0;
    }
    cout<<res;
}


#include<bits/stdc++.h>
using namespace std;
int fin(vector<long long int>parent,long long int u){
    if(parent[u]==u)
        return u;
    else
        return parent[u]=fin(parent,parent[u]);
}
void unio(vector<long long int>&rank,vector<long long int> &parent,long long int u,long long int v){
    int uu=fin(parent,u);
    int vv=fin(parent,v);
    if(rank[uu]>rank[vv]){
        parent[vv]=uu;
    }
    else if(rank[uu]<rank[vv]){
        parent[uu]=vv;
    }
    else{
        parent[uu]=vv;
        rank[vv]++;
    }
}
int main()
{
    long long int n,m;
    cin>>n>>m;
    vector<long long int> cost(n+1);
    for(long long int i=1;i<n+1;i++){
        cin>>cost[i];
    }
    vector<long long int > parent(n+1);
    vector<long long int > rank(n+1,0);
    iota(parent.begin(),parent.end(),0);
    for(long long int i=0;i<m;i++){
        long long int u,v;
        cin>>u>>v;
        int uu=fin(parent,u);
        int vv=fin(parent,v);
        cost[u]  = cost[v] = min(cost[u],cost[v]);
        if(uu!=vv)
            unio(rank,parent,u,v);
    }
    int res=0;
    for(int i=1;i<n+1;i++){
        res+=cost[parent[i]];
        cost[parent[i]]=0;
    }
    cout<<res;
}


#include<bits/stdc++.h>
using namespace std;

int fin(vector <int> &parent,int u){
    if(parent[u] == u)
        return u;
    parent[u]=fin(parent,parent[u]);
    return parent[u];
}
void unio(vector<int> cost,vector <int> &parent,int u,int v){
    int uu=fin(parent,u);
    int vv=fin(parent,v);
    if(cost[uu]<cost[vv]){
        parent[vv]=uu;
    }
    else{
        parent[uu]=vv;
    }
}
int main()
{
    int n,m;
    cin>>n>>m;
    vector<int> cost;
    vector<int> parent;
    cost.push_back(-1);
    for(int i=1;i<n+1;i++){
        int c;
        cin>>c;
        cost.push_back(c);
    }
    for(int i=0;i<n+1;i++){
        parent.push_back(i);
    }
    for(int i=0;i<m;i++){
        int u,v;
        cin>>u>>v;
        unio(cost,parent,u,v);
    }
    long long int res=0;

    for(int i=1;i<n+1;i++){
            res+=cost[fin(parent,i)];
            cost[fin(parent,i)]=0;
    }
    cout<<res;
}


#include <iostream>
#include<bits/stdc++.h>
#define pb push_back
#define VI vector<int>
#define VL vector<long long>
#define VS vector<string>
#define  print  cout<<
#define exit return 0
#define all(v) (v).begin() , (v).end()
typedef long long ll ;
using namespace std  ;
void fast()
{
    ios::sync_with_stdio(false),cin.tie( 0 ) ;
}
const int OO =  0x3f3f3f3f ;
int dx[]= { -1 , 0 , 1 , 0 } ;
int dy[]= { 0  , 1 , 0 , -1} ;
//freopen("input.in", "rt", stdin);
//freopen("output.out", "wt", stdout);
const int Sizee = 1e5+5;
int head[Sizee];
int nxt[Sizee];
int to[Sizee];
int ne ;
void init()
{
    memset(head,-1,sizeof head);
    ne = 0 ;
}
void add_edge(int f ,int t )
{
    nxt[ne]=head[f];
    to[ne]=t;
    head[f]=ne++;
}
void by_edge(int a ,int b )
{
    add_edge(a,b);
    add_edge(b,a);
}
int n , k ;
bool vis[Sizee];
void solve( int U )
{
    if(vis[U]){return ; }
    vis[U]=true;
    for(int e=head[U],v;~e;e=nxt[e])
    {
        v=to[e];
        if(!vis[v])
        {
            solve(v);
        }
    }
}
int main()
{
    fast() ;
    init();
    cin >> n >> k ;
    vector < pair < long long , int > > Nodes(n+1) ;
    ll ans = 0 ;
    for(int i =  1 ; i <= n ;  i++ )
    {
        cin >> Nodes[i].first ;
        Nodes[i].second = i ;
    }
    sort( Nodes.begin() , Nodes.end()) ;
    int a , b;
    while(k--)
    {
        cin >> a >> b ;
        by_edge(a,b);
    }
    for(int i = 1 ; i <= n ; i ++  )
    {
        if(!vis[Nodes[i].second])
        {
            ans+=Nodes[i].first ;
            solve(Nodes[i].second );
        }
    }
    print ans  << endl;
    exit ;
}


#include <iostream>
#include<bits/stdc++.h>
#define pb push_back
#define VI vector<int>
#define VL vector<long long>
#define VS vector<string>
#define  print  cout<<
#define exit return 0
#define all(v) (v).begin() , (v).end()
typedef long long ll ;
using namespace std  ;
void fast()
{
    ios::sync_with_stdio(false),cin.tie( 0 ) ;
}
const int OO =  0x3f3f3f3f ;
int dx[]= { -1 , 0 , 1 , 0 } ;
int dy[]= { 0  , 1 , 0 , -1} ;
//freopen("input.in", "rt", stdin);
//freopen("output.out", "wt", stdout);
const int Sizee = 1e5+5;
int head[Sizee];
int nxt[Sizee];
int to[Sizee];
int ne ;
int n , k ;
void init()
{
    memset(head,-1,sizeof head);
    ne = 0 ;
}
void add_edge(int f ,int t )
{
    nxt[ne]=head[f];
    to[ne]=t;
    head[f]=ne++;
}
void by_edge(int a ,int b )
{
    add_edge(a,b);
    add_edge(b,a);
}
bool vis[Sizee];
void solve( int U )
{
    if(vis[U]){return;}
    vis[U]=true;
    for(int e=head[U],v;~e;e=nxt[e])
    {
        v=to[e];
        solve(v);
    }
    return;
}
int main()
{
    fast() ;
    init() ;
    cin >> n >> k ;
    vector < pair < long long , int > > Nodes(n+5) ;
    ll ans = 0 ;
    for(int i =  1 ; i <= n ;  i++ )
    {
        cin >> Nodes[i].first ;
        Nodes[i].second = i ;
    }
    sort( Nodes.begin() , Nodes.end()) ;
    int a , b ;
    while(k--)
    {
        cin >> a >> b    ;
        by_edge( a , b ) ;
    }
    for(int i = 1 ; i <= n ; i ++  )
    {
        if( !vis[Nodes[i].second] )
        {
            ans+=Nodes[i].first ;
            solve(Nodes[i].second );
        }
    }
    print ans  << endl;
    exit ;
}


#include <iostream>
#include<bits/stdc++.h>
#define pb push_back
#define VI vector<int>
#define VL vector<long long>
#define VS vector<string>
#define  print  cout<<
#define exit return 0
#define all(v) (v).begin() , (v).end()
typedef long long ll ;
using namespace std  ;
void fast()
{
    ios::sync_with_stdio(false),cin.tie( 0 ) ;
}
const int OO =  0x3f3f3f3f3f3f ;
int dx[]= { -1 , 0 , 1 , 0 } ;
int dy[]= { 0  , 1 , 0 , -1} ;
//freopen("input.in", "rt", stdin);
//freopen("output.out", "wt", stdout);

int n , k ;
const int Sizee = 1e5+5;
int head[Sizee];
int nxt[Sizee];
int to[Sizee];
ll gold[Sizee];
int ne ;
void init()
{
    memset(head,-1,sizeof head);
    ne = 0 ;
}
void add_edge(int f ,int t )
{
    nxt[ne]=head[f];
    to[ne]=t;
    head[f]=ne++;
}
void by_edge(int a ,int b )
{
    add_edge(a,b);
    add_edge(b,a);
}
bool vis[Sizee];
ll minn = 0 ;
void solve( int U )
{
    vis[U]=true;
    for(int e=head[U],v;~e;e=nxt[e])
    {
        v=to[e];
        if(!vis[v])
            {
                minn=min(minn,gold[U]);
                solve(v);
            }
    }
    return;
}
int main()
{
    fast() ;
    init() ;
    cin >> n >> k ;
    ll ans = 0 ;
    for(int i =  1 ; i <= n ;  i++ )
    {
        cin >>gold[i] ;
    }
    int a , b ;
    while(k--)
    {
        cin >> a >> b    ;
        by_edge( a , b ) ;
    }
    for(int i = 1 ; i <= n ; i ++  )
    {
        if( !vis[i] )
        {
            minn = gold[i] ;
            solve(i);
            ans += minn ;
        }
    }
    print ans  << endl;
    exit ;
}


#include <iostream>
#include<bits/stdc++.h>
#define pb push_back
#define VI vector<int>
#define VL vector<long long>
#define VS vector<string>
#define  print  cout<<
#define exit return 0
#define all(v) (v).begin() , (v).end()
typedef long long ll ;
using namespace std  ;
void fast()
{
    ios::sync_with_stdio(false),cin.tie( 0 ) ;
}
const int OO =  0x3f3f3f3f3f ;
int dx[]= { -1 , 0 , 1 , 0 } ;
int dy[]= { 0  , 1 , 0 , -1} ;
//freopen("input.in", "rt", stdin);
//freopen("output.out", "wt", stdout);

int n , k ;
const int Sizee = 1e5+5;
const int Sizee2 = 1e7 + 5 ;
int head[Sizee];
int nxt[Sizee2];
int to[Sizee2];
ll gold[Sizee];
int ne ;
void init()
{
    memset( head , -1 , sizeof head ) ;
    ne = 0 ;
}
void add_edge(int f ,int t )
{
    nxt[ne]=head[f];
    to[ne]=t;
    head[f]=ne++;
}
void by_edge(int a ,int b )
{
    add_edge(a,b);
    add_edge(b,a);
}
bool vis[Sizee] ;
ll minn = 0 ;
void solve( int U )
{
    vis[U]=true;
    for(int e=head[U],v;~e;e=nxt[e])
    {
        v=to[e];
        if(!vis[v])
            {
                minn=min(minn,gold[U]);
                solve(v);
            }
    }
    return;
}
int main()
{
    fast() ;
    init() ;
    cin >> n >> k ;
    ll ans = 0 ;
    for(int i =  1 ; i <= n ;  i++ )
    {
        cin >> gold[i] ;
    }
    int a , b ;
    while( k-- )
    {
        cin >> a >> b    ;
        by_edge( a , b ) ;
    }
    for(int i = 1 ; i <= n ; i ++  )
    {
        if( !vis[i] )
        {
            minn = gold[i] ;
            solve(i)       ;
            ans += minn    ;
        }
    }
    print ans  << endl ;
    exit ;
}


#include <iostream>
#include<bits/stdc++.h>
#define pb push_back
#define VI vector<int>
#define VL vector<long long>
#define VS vector<string>
#define  print  cout<<
#define exit return 0
#define all(v) (v).begin() , (v).end()
typedef long long ll ;
using namespace std  ;
void fast()
{
    ios::sync_with_stdio(false),cin.tie( 0 ) ;
}
const int OO =  0x3f3f3f3f ;
int dx[]= { -1 , 0 , 1 , 0 } ;
int dy[]= { 0  , 1 , 0 , -1} ;
//freopen("input.in", "rt", stdin);
//freopen("output.out", "wt", stdout);
const int Sizee = 1e5+5;
int head[Sizee];
int nxt[Sizee];
int to[Sizee];
int ne ;
void init()
{
    memset(head,-1,sizeof head);
    ne = 0 ;
}
void add_edge(int f ,int t )
{
    nxt[ne]=head[f];
    to[ne]=t;
    head[f]=ne++;
}
void by_edge(int a ,int b )
{
    add_edge(a,b);
    add_edge(b,a);
}
int n , k ;
bool vis[Sizee];
void solve( int U )
{
    vis[U]=true;
    for(int e=head[U],v;~e;e=nxt[e])
    {
        v=to[e];
        if(!vis[v])
        {
            solve(v);
        }
    }
}
int main()
{
    fast() ;
    init();
    cin >> n >> k ;
    vector < pair < long long , int > > Nodes(n+1) ;
    ll ans = 0 ;
    for(int i =  1 ; i <= n ;  i++ )
    {
        cin >> Nodes[i].first ;
        Nodes[i].second = i ;
    }
    sort( Nodes.begin() , Nodes.end()) ;
    int a , b;
    while(k--)
    {
        cin >> a >> b ;
        by_edge(a,b);
    }
    for(int i = 1 ; i <= n ; i ++  )
    {
        if(!vis[Nodes[i].second])
        {
            ans+=Nodes[i].first ;
            solve(Nodes[i].second );
        }
    }
    print ans  << endl;
    exit ;
}


#include <iostream>
#include<bits/stdc++.h>
#define pb push_back
#define VI vector<int>
#define VL vector<long long>
#define VS vector<string>
#define  print  cout<<
#define exit return 0
#define all(v) (v).begin() , (v).end()
typedef long long ll ;
using namespace std  ;
void fast()
{
    ios::sync_with_stdio(false),cin.tie( 0 ) ;
}
const int OO =  0x3f3f3f3f ;
int dx[]= { -1 , 0 , 1 , 0 } ;
int dy[]= { 0  , 1 , 0 , -1} ;
//freopen("input.in", "rt", stdin);
//freopen("output.out", "wt", stdout);
const int Sizee = 1e5+5;
int head[Sizee];
int nxt[Sizee];
int to[Sizee];
int ne ;
int n , k ;
void init()
{
    memset(head,-1,sizeof head);
    ne = 0 ;
}
void add_edge(int f ,int t )
{
    nxt[ne]=head[f];
    to[ne]=t;
    head[f]=ne++;
}
void by_edge(int a ,int b )
{
    add_edge(a,b);
    add_edge(b,a);
}
bool vis[Sizee];
void solve( int U )
{
    //if(vis[U]){return;}
    vis[U]=true;
    for(int e=head[U],v;~e;e=nxt[e])
    {
        v=to[e];
        if(!vis[v]){solve(v);}
    }
    return;
}
int main()
{
    fast() ;
    init() ;
    cin >> n >> k ;
    vector < pair < long long , int > > Nodes(n+1) ;
    ll ans = 0 ;
    for(int i =  1 ; i <= n ;  i++ )
    {
        cin >> Nodes[i].first ;
        Nodes[i].second = i ;
    }
    sort( Nodes.begin() , Nodes.end()) ;
    int a , b ;
    while(k--)
    {
        cin >> a >> b    ;
        by_edge( a , b ) ;
    }
    for(int i = 1 ; i <= n ; i ++  )
    {
        if( !vis[Nodes[i].second] )
        {
            ans+=Nodes[i].first ;
            solve(Nodes[i].second );
        }
    }
    print ans  << endl;
    exit ;
}


#include <iostream>
#include<bits/stdc++.h>
#define pb push_back
#define VI vector<int>
#define VL vector<long long>
#define VS vector<string>
#define  print  cout<<
#define exit return 0
#define all(v) (v).begin() , (v).end()
typedef long long ll ;
using namespace std  ;
void fast()
{
    ios::sync_with_stdio(false),cin.tie( 0 ) ;
}
const int OO =  0x3f3f3f3f3f ;
int dx[]= { -1 , 0 , 1 , 0 } ;
int dy[]= { 0  , 1 , 0 , -1} ;
//freopen("input.in", "rt", stdin);
//freopen("output.out", "wt", stdout);

int n , k ;
const int Sizee = 1e5+5;
const int Sizee2 = 1e7 + 5 ;
int head[Sizee];
int nxt[Sizee2];
int to[Sizee2];
ll gold[Sizee];
int ne ;
void init()
{
    memset( head , -1 , sizeof head ) ;
    ne = 0 ;
}
void add_edge(int f ,int t )
{
    nxt[ne]=head[f];
    to[ne]=t;
    head[f]=ne++;
}
void by_edge(int a ,int b )
{
    add_edge(a,b);
    add_edge(b,a);
}
bool vis[Sizee] ;
ll minn = 0 ;
void solve( int U )
{
    vis[U]=true;
    for(int e=head[U],v;~e;e=nxt[e])
    {
        v=to[e];
        if(!vis[v])
            {
                minn=min(minn,gold[v]);
                solve(v);
            }
    }
    return;
}
int main()
{
    fast() ;
    init() ;
    cin >> n >> k ;
    ll ans = 0 ;
    for(int i =  1 ; i <= n ;  i++ )
    {
        cin >> gold[i] ;
    }
    int a , b ;
    while( k-- )
    {
        cin >> a >> b    ;
        by_edge( a , b ) ;
    }
    for(int i = 1 ; i <= n ; i ++  )
    {
        if( !vis[i] )
        {
            minn = gold[i] ;
            solve(i)       ;
            ans += minn    ;
        }
    }
    print ans  << endl ;
    exit ;
}


#include <iostream>
#include<bits/stdc++.h>
#define pb push_back
#define VI vector<int>
#define VL vector<long long>
#define VS vector<string>
#define  print  cout<<
#define exit return 0
#define all(v) (v).begin() , (v).end()
typedef long long ll ;
using namespace std  ;
void fast()
{
    ios::sync_with_stdio(false),cin.tie( 0 ) ;
}
const int OO =  0x3f3f3f3f ;
int dx[]= { -1 , 0 , 1 , 0 } ;
int dy[]= { 0  , 1 , 0 , -1} ;
//freopen("input.in", "rt", stdin);
//freopen("output.out", "wt", stdout);
const int Sizee = 1e5+5;
int head[Sizee];
int nxt[Sizee];
int to[Sizee];
int ne ;
int n , k ;
void init()
{
    memset(head,-1,sizeof head);
    ne = 0 ;
}
void add_edge(int f ,int t )
{
    nxt[ne]=head[f];
    to[ne]=t;
    head[f]=ne++;
}
void by_edge(int a ,int b )
{
    add_edge(a,b);
    add_edge(b,a);
}
bool vis[Sizee];
void solve( int U )
{
    if(vis[U]){return;}
    vis[U]=true;
    for(int e=head[U],v;~e;e=nxt[e])
    {
        v=to[e];
        solve(v);
    }
    return;
}
int main()
{
    fast() ;
    init() ;
    cin >> n >> k ;
    vector < pair < long long , int > > Nodes(n+1) ;
    ll ans = 0 ;
    for(int i =  1 ; i <= n ;  i++ )
    {
        cin >> Nodes[i].first ;
        Nodes[i].second = i ;
    }
    sort( Nodes.begin() , Nodes.end()) ;
    int a , b ;
    while(k--)
    {
        cin >> a >> b    ;
        by_edge( a , b ) ;
    }
    for(int i = 1 ; i <= n ; i ++  )
    {
        if(!vis[Nodes[i].second])
        {
            ans+=Nodes[i].first ;
            solve(Nodes[i].second );
        }
    }
    print ans  << endl;
    exit ;
}


#include <iostream>
#include<bits/stdc++.h>
#define pb push_back
#define VI vector<int>
#define VL vector<long long>
#define VS vector<string>
#define  print  cout<<
#define exit return 0
#define all(v) (v).begin() , (v).end()
typedef long long ll ;
using namespace std  ;
void fast()
{
    ios::sync_with_stdio(false),cin.tie( 0 ) ;
}
const int OO =  0x3f3f3f3f3f ;
int dx[]= { -1 , 0 , 1 , 0 } ;
int dy[]= { 0  , 1 , 0 , -1} ;
//freopen("input.in", "rt", stdin);
//freopen("output.out", "wt", stdout);

int n , k ;
const int Sizee = 1e5+5;
const int Sizee2 = 1e8 + 5 ;
int head[Sizee];
int nxt[Sizee2];
int to[Sizee2];
ll gold[Sizee];
int ne ;
void init()
{
    memset( head , -1 , sizeof head ) ;
    ne = 0 ;
}
void add_edge(int f ,int t )
{
    nxt[ne]=head[f];
    to[ne]=t;
    head[f]=ne++;
}
void by_edge(int a ,int b )
{
    add_edge(a,b);
    add_edge(b,a);
}
bool vis[Sizee] ;
ll minn = 0 ;
void solve( int U )
{
    vis[U]=true;
    for(int e=head[U],v;~e;e=nxt[e])
    {
        v=to[e];
        if(!vis[v])
            {
                minn=min(minn,gold[U]);
                solve(v);
            }
    }
    return;
}
int main()
{
    fast() ;
    init() ;
    cin >> n >> k ;
    ll ans = 0 ;
    for(int i =  1 ; i <= n ;  i++ )
    {
        cin >> gold[i] ;
    }
    int a , b ;
    while( k-- )
    {
        cin >> a >> b    ;
        by_edge( a , b ) ;
    }
    for(int i = 1 ; i <= n ; i ++  )
    {
        if( !vis[i] )
        {
            minn = gold[i] ;
            solve(i)       ;
            ans += minn    ;
        }
    }
    print ans  << endl ;
    exit ;
}


#include <iostream>
#include <vector>

#define FOR(i, n) for (int i = 0; i < (n); i++)
#define FORi(i, a, n) for (int i = (a); i < (n); i++)
#define REV(i, n) for (int i = (n)-1; i >= 0; i--)
#define pb push_back
#define sz(a) int(a).size
#define all(c) (c).begin(), (c).end()

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;

int ldr[100005];

int find(int a) {
	if (ldr[a] == a) return a;
	return ldr[a] = find(ldr[a]);
}

int main() {
	int n, m, man[100005][2], ans = 0;

	cin >> n >> m;
	FORi(i, 1, n+1) {
		cin >> man[i][0];
		man[i][1] = 0, ldr[i] = i;
	}
	FOR(i, m) {
		int a, b;
		cin >> a >> b;
		ldr[find(b)] = find(a);
	}

	FORi(i, 1, n+1) {
		int u = find(i);
		if (man[u][1]) ans = min(ans, ans-man[u][0]+man[i][0]);
		else ans += man[i][0], man[u][0] = man[i][0];
		man[u][1] = 1;
	}

	cout << ans << endl;

	return 0;
}


#include <iostream>
#include <vector>

#define FOR(i, n) for (int i = 0; i < (n); i++)
#define FORi(i, a, n) for (int i = (a); i < (n); i++)
#define REV(i, n) for (int i = (n)-1; i >= 0; i--)
#define pb push_back
#define sz(a) int(a).size
#define all(c) (c).begin(), (c).end()

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;

ll ldr[100005];

ll find(ll a) {
	if (ldr[a] == a) return a;
	return ldr[a] = find(ldr[a]);
}

int main() {
	ll n, m, man[100005][2], ans = 0;

	cin >> n >> m;
	FORi(i, 1, n+1) {
		cin >> man[i][0];
		man[i][1] = 0, ldr[i] = i;
	}
	FOR(i, m) {
		ll a, b;
		cin >> a >> b;
		ldr[find(b)] = find(a);
	}

	FORi(i, 1, n+1) {
		ll u = find(i);

		if (u == i) ans += man[u][0], man[u][1] = 1;
		else if (man[u][0] > man[i][0]) {
			man[u][0] = man[i][0];
			if (man[u][1]) ans -= man[u][0] - man[i][0];
		}
	}

	cout << ans << endl;

	return 0;
}


#include <iostream>
#include <vector>

#define FOR(i, n) for (int i = 0; i < (n); i++)
#define FORi(i, a, n) for (int i = (a); i < (n); i++)
#define REV(i, n) for (int i = (n)-1; i >= 0; i--)
#define pb push_back
#define sz(a) int(a).size
#define all(c) (c).begin(), (c).end()

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;

int ldr[100005];

int find(int a) {
	if (ldr[a] == a) return a;
	return ldr[a] = find(ldr[a]);
}

int main() {
	int n, m, man[100005][2], ans = 0;

	cin >> n >> m;
	FORi(i, 1, n+1) {
		cin >> man[i][0];
		man[i][1] = 0, ldr[i] = i;
	}
	FOR(i, m) {
		int a, b;
		cin >> a >> b;
		ldr[find(b)] = find(a);
	}

	FORi(i, 1, n+1) {
		int u = find(i);
		if (man[u][1]) ans = min(ans, ans-man[u][0]+man[i][0]);
		else ans += man[u][0] = man[i][0];
		man[i][1] = 1;
	}

	cout << ans << endl;

	return 0;
}


#include <iostream>
#include <vector>

#define FOR(i, n) for (int i = 0; i < (n); i++)
#define FORi(i, a, n) for (int i = (a); i < (n); i++)
#define REV(i, n) for (int i = (n)-1; i >= 0; i--)
#define pb push_back
#define sz(a) int(a).size
#define all(c) (c).begin(), (c).end()

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;

ll ldr[100005];

ll find(ll a) {
	if (ldr[a] == a) return a;
	return ldr[a] = find(ldr[a]);
}

int main() {
	ll n, m, man[100005][2], ans = 0;

	cin >> n >> m;
	FORi(i, 1, n+1) {
		cin >> man[i][0];
		man[i][1] = 0, ldr[i] = i;
	}
	FOR(i, m) {
		ll a, b;
		cin >> a >> b;
		ldr[find(b)] = find(a);
	}

	FORi(i, 1, n+1) {
		ll u = find(i);

		if (u == i) { ans += man[u][0]; man[u][1] = 1; }
		else if (man[u][0] > man[i][0]) {
			man[u][0] = man[i][0];
			if (man[u][1]) ans -= man[u][0] - man[i][0];
		}
	}

	cout << ans << endl;

	return 0;
}


#include <iostream>
#include <vector>

#define FOR(i, n) for (int i = 0; i < (n); i++)
#define FORi(i, a, n) for (int i = (a); i < (n); i++)
#define REV(i, n) for (int i = (n)-1; i >= 0; i--)
#define pb push_back
#define sz(a) int(a).size
#define all(c) (c).begin(), (c).end()

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;

int ldr[100005];

int find(int a) {
	if (ldr[a] == a) return a;
	return ldr[a] = find(ldr[a]);
}

int main() {
	int n, m, man[100005][2], ans = 0;

	cin >> n >> m;
	FORi(i, 1, n+1) {
		cin >> man[i][0];
		man[i][1] = 0, ldr[i] = i;
	}
	FOR(i, m) {
		int a, b;
		cin >> a >> b;
		ldr[find(b)] = find(a);
	}

	FORi(i, 1, n+1) {
		int u = find(i);
		if (man[u][1]) ans = min(ans, ans-man[u][0]+man[i][0]);
		else ans += man[i][0], man[u][0] = man[i][0];
		man[i][1] = 1;
	}

	cout << ans << endl;

	return 0;
}


#include <iostream>
#include <vector>

#define FOR(i, n) for (int i = 0; i < (n); i++)
#define FORi(i, a, n) for (int i = (a); i < (n); i++)
#define REV(i, n) for (int i = (n)-1; i >= 0; i--)
#define pb push_back
#define sz(a) int(a).size
#define all(c) (c).begin(), (c).end()

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;

ll ldr[100005];

ll find(ll a) {
	if (ldr[a] == a) return a;
	return ldr[a] = find(ldr[a]);
}

int main() {
	ll n, m, man[100005][2], ans = 0;

	cin >> n >> m;
	FORi(i, 1, n+1) {
		cin >> man[i][0];
		man[i][1] = 0, ldr[i] = i;
	}
	FOR(i, m) {
		ll a, b;
		cin >> a >> b;
		ldr[find(b)] = find(a);
	}

	FORi(i, 1, n+1) {
		ll u = find(i);

		if (u == i) { ans += man[u][0]; man[u][1] = 1; }
		else if (man[u][0] > man[i][0]) {
			if (man[u][1]) ans -= man[u][0] - man[i][0];
			man[u][0] = man[i][0];
		}
	}

	cout << ans << endl;

	return 0;
}


#include <iostream>
#include <vector>

#define FOR(i, n) for (int i = 0; i < (n); i++)
#define FORi(i, a, n) for (int i = (a); i < (n); i++)
#define REV(i, n) for (int i = (n)-1; i >= 0; i--)
#define pb push_back
#define sz(a) int(a).size
#define all(c) (c).begin(), (c).end()

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;

ll ldr[100005];

ll find(ll a) {
	if (ldr[a] == a) return a;
	return ldr[a] = find(ldr[a]);
}

int main() {
	ll n, m, man[100005][2], ans = 0;

	cin >> n >> m;
	FORi(i, 1, n+1) {
		cin >> man[i][0];
		man[i][1] = 0, ldr[i] = i;
	}
	FOR(i, m) {
		ll a, b;
		cin >> a >> b;
		ldr[find(b)] = find(a);
	}

	FORi(i, 1, n+1) {
		ll u = find(i);
		if (man[u][1]) ans = min(ans, ans-man[u][0]+man[i][0]);
		else ans += man[i][0], man[u][0] = man[i][0];
		man[u][1] = 1;
	}

	cout << ans << endl;

	return 0;
}


#include <iostream>
#include <vector>

#define FOR(i, n) for (int i = 0; i < (n); i++)
#define FORi(i, a, n) for (int i = (a); i < (n); i++)
#define REV(i, n) for (int i = (n)-1; i >= 0; i--)
#define pb push_back
#define sz(a) int(a).size
#define all(c) (c).begin(), (c).end()

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;

ll ldr[100005];

ll find(ll a) {
	if (ldr[a] == a) return a;
	return ldr[a] = find(ldr[a]);
}

int main() {
	ll n, m, man[100005][2], ans = 0;

	cin >> n >> m;
	FORi(i, 1, n+1) {
		cin >> man[i][0];
		man[i][1] = 0, ldr[i] = i;
	}
	FOR(i, m) {
		ll a, b;
		cin >> a >> b;
		ldr[find(b)] = find(a);
	}

	FORi(i, 1, n+1) {
		ll u = find(i);
		if (man[i][0] <= man[u][0]) {
			if (man[i][1]) ans -= man[u][0];
			man[u][0] = man[i][0];
			ans += man[u][0];
			man[u][1] = 1;
		}
	}

	cout << ans << endl;

	return 0;
}


#include <iostream>
#include <vector>

#define FOR(i, n) for (int i = 0; i < (n); i++)
#define FORi(i, a, n) for (int i = (a); i < (n); i++)
#define REV(i, n) for (int i = (n)-1; i >= 0; i--)
#define pb push_back
#define sz(a) int(a).size
#define all(c) (c).begin(), (c).end()

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;

ll ldr[100005];

ll find(ll a) {
	if (ldr[a] == a) return a;
	return ldr[a] = find(ldr[a]);
}

int main() {
	ll n, m, man[100005][2], ans = 0;

	cin >> n >> m;
	FORi(i, 1, n+1) {
		cin >> man[i][0];
		man[i][1] = 0, ldr[i] = i;
	}
	FOR(i, m) {
		ll a, b;
		cin >> a >> b;
		ldr[find(b)] = find(a);
	}

	FORi(i, 1, n+1) {
		ll u = find(i);
		if (man[i][0] < man[u][0]) {
			if (man[i][1]) ans -= man[u][0];
			man[u][0] = man[i][0];
			ans += man[u][0];
			man[u][1] = 1;
		} else if (man[u][1] == 0) ans += man[u][0], man[u][1] = 1;
	}

	cout << ans << endl;

	return 0;
}


#include <bits/stdc++.h>
#define mp make_pair
using namespace std;

const int N = 100005;
int n,m;
int size[N],id[N];
bool seen[N];
long long cost[N];
void __init(){
	for(int i=1;i<=n;i++){
		id[i] = i;
		seen[i] = false;
		size[i] = 1;
	}
}
int root(int a){
	while(id[a]!=a){
		a = id[a];
	}
	return a;
}
long  long Min(long long a,long long b){
	if(a<b) return a;
	else return b;
}
void Union(int a,int b){
	int p = root(a),q = root(b);
	if(size[p]<size[q]){
		id[p] = q;
		size[p]+=size[q];
	}
	else{
		id[q] = p;
		size[q]+=size[p];
	}
}

int main(){
//	freopen("i.in","r",stdin);
	int u,v;
	scanf("%d %d",&n,&m);

	for(int i=1;i<=n;i++){
		scanf("%I64d",&cost[i]);
	}
	__init();
	long long  ans = 0;
	for(int i=0;i<m;i++){
		scanf("%d %d",&u,&v);
		if(root(u)!=root(v)){
			Union(u,v);
		}
	}
	for(int i=1;i<=n;i++){
		int p = root(i);
		cost[p] = Min(cost[i],cost[p]);
	}
	for(int i=1;i<=n;i++){
		int p = root(i);
		if(!seen[p]){
			seen[p] = true;
			ans += cost[p];
		}
	}
	printf("%I64d\n",ans);
	return 0;
}


#include <bits/stdc++.h>
#define mp make_pair
using namespace std;

const int N = 100005;
int n,m;
int size[N],id[N];
bool seen[N];
long long cost[N];
void __init(){
	for(int i=1;i<=n;i++){
		id[i] = i;
		seen[i] = false;
		size[i] = 1;
	}
}
int root(int a){
	while(a!=id[a]){
		id[a] = id[id[a]];
		a = id[a];
	}
	return a;
}
long  long Min(long long a,long long b){
	if(a<b) return a;
	else return b;
}
void Union(int a,int b){
	int p = root(a),q = root(b);
	if(size[p]>size[q]){
		id[p] = q;
		size[p]+=size[q];
	}
	else{
		id[q] = p;
		size[q]+=size[p];
	}
}

int main(){
//	freopen("i.in","r",stdin);
	int u,v;
	scanf("%d %d",&n,&m);

	for(int i=1;i<=n;i++){
		scanf("%I64d",&cost[i]);
	}
	__init();
	long long  ans = 0;
	for(int i=0;i<m;i++){
		scanf("%d %d",&u,&v);
		if(root(u)!=root(v)){
			Union(u,v);
		}
	}
	for(int i=1;i<=n;i++){
		int p = root(i);
		cost[p] = Min(cost[i],cost[p]);
	}
	for(int i=1;i<=n;i++){
		int p = root(i);
		if(!seen[p]){
			seen[p] = true;
			ans += cost[p];
		}
	}
	printf("%I64d\n",ans);
	return 0;
}


#include <bits/stdc++.h>
#define mp make_pair
using namespace std;

const int N = 100005;
int n,m;
int size[N],id[N];
bool seen[N];
long long cost[N];
void __init(){
	for(int i=1;i<=n;i++){
		id[i] = i;
		seen[i] = false;
		size[i] = 1;
	}
}
int root(int a){
	while(a!=id[a]){
		a = id[a];
	}
	return a;
}
long  long Min(long long a,long long b){
	if(a<b) return a;
	else return b;
}
void Union(int a,int b){
	int p = root(a),q = root(b);
	if(size[p]>size[q]){
		id[p] = q;
		size[p]+=size[q];
	}
	else{
		id[q] = p;
		size[q]+=size[p];
	}
}

int main(){
//	freopen("i.in","r",stdin);
	int u,v;
	scanf("%d %d",&n,&m);

	for(int i=1;i<=n;i++){
		scanf("%I64d",&cost[i]);
	}
	__init();
	long long  ans = 0;
	for(int i=0;i<m;i++){
		scanf("%d %d",&u,&v);
		if(root(u)!=root(v)){
			Union(u,v);
		}
	}
	for(int i=1;i<=n;i++){
		int p = root(i);
		cost[p] = Min(cost[i],cost[p]);
	}
	for(int i=1;i<=n;i++){
		int p = root(i);
		if(!seen[p]){
			seen[p] = true;
			ans += cost[p];
		}
	}
	printf("%I64d\n",ans);
	return 0;
}


#include <bits/stdc++.h>
#define mp make_pair
using namespace std;

const int N = 100005;
int n,m;
int size[N],id[N];
bool seen[N];
long long cost[N];
void __init(){
	for(int i=1;i<=n;i++){
		id[i] = i;
		seen[i] = false;
		size[i] = 1;
	}
}
int root(int a){
	while(id[a]!=a){
		a = id[a];
	}
	return a;
}
long  long Min(long long a,long long b){
	if(a<b) return a;
	else return b;
}
void Union(int a,int b){
	int p = root(a),q = root(b);
	if(size[p]>size[q]){
		id[p] = q;
		size[p]+=size[q];
	}
	else{
		id[q] = p;
		size[q]+=size[p];
	}
}

int main(){
//	freopen("i.in","r",stdin);
	int u,v;
	scanf("%d %d",&n,&m);

	for(int i=1;i<=n;i++){
		scanf("%I64d",&cost[i]);
	}
	__init();
	long long  ans = 0;
	for(int i=0;i<m;i++){
		scanf("%d %d",&u,&v);
		if(root(u)!=root(v)){
			Union(u,v);
		}
	}
	for(int i=1;i<=n;i++){
		int p = root(i);
		cost[p] = Min(cost[i],cost[p]);
	}
	for(int i=1;i<=n;i++){
		int p = root(i);
		if(!seen[p]){
			seen[p] = true;
			ans += cost[p];
		}
	}
	printf("%I64d\n",ans);
	return 0;
}


#include <bits/stdc++.h>
#define mp make_pair
using namespace std;
typedef pair<int,int> pi;
const int N = 100005;
int n,m;
int size[N],id[N];
bool seen[N];
long long cost[N];
void __init(){
	for(int i=1;i<=n;i++){
		id[i] = i;
		seen[i] = false;
		size[i] = 1;
	}
}
int root(int a){
	while(id[a]!=a){
		a = id[a];
	}
	return a;
}
long  long Min(long long a,long long b){
	if(a<b) return a;
	else return b;
}
void Union(int a,int b){
	int p = root(a),q = root(b);
	if(size[p]<size[q]){
		id[p] = q;
		cost[p] = Min(cost[q],Min(cost[p],Min(cost[a],cost[b])));
		size[p]+=size[q];
	}
	else{
		id[q] = p;
		cost[q] = Min(cost[q],Min(cost[p],Min(cost[a],cost[b])));
		size[q]+=size[p];
	}
}

int main(){
//	freopen("i.in","r",stdin);
	int u,v;
	scanf("%d %d",&n,&m);
	__init();
	for(int i=1;i<=n;i++){
		scanf("%I64d",&cost[i]);
	}
	long long  ans = 0;
	for(int i=0;i<m;i++){
		scanf("%d %d",&u,&v);
		if(root(u)!=root(v)){
			Union(u,v);
		}
	}
	for(int i=1;i<=n;i++){
		int p = root(i);
		if(!seen[p]){
			seen[p] = true;
//			cout<<cost[p]<<endl;
			ans += cost[p];
		}
	}
	printf("%I64d\n",ans);
	return 0;
}


#include <bits/stdc++.h>
#define mp make_pair
using namespace std;
typedef pair<int,int> pi;
const int N = 100005;
int n,m;
int size[N],id[N];
bool seen[N];
long long cost[N];
void __init(){
	for(int i=1;i<=n;i++){
		id[i] = i;
		seen[i] = false;
		size[i] = 1;
	}
}
int root(int a){
	while(id[a]!=a){
		a = id[a];
	}
	return a;
}
long  long Min(long long a,long long b){
	if(a<b) return a;
	else return b;
}
void Union(int a,int b){
	int p = root(a),q = root(b);
	if(size[p]<size[q]){
		id[p] = q;
//		cost[p] = Min(cost[q],Min(cost[p],Min(cost[a],cost[b])));
		cost[p] = Min(cost[p],cost[q]);
		size[p]+=size[q];
	}
	else{
		id[q] = p;
		cost[q] = Min(cost[p],cost[q]);
//		cost[q] = Min(cost[q],Min(cost[p],Min(cost[a],cost[b])));
		size[q]+=size[p];
	}
}

int main(){
//	freopen("i.in","r",stdin);
	int u,v;
	scanf("%d %d",&n,&m);
	__init();
	for(int i=1;i<=n;i++){
		scanf("%I64d",&cost[i]);
	}
	long long  ans = 0;
	for(int i=0;i<m;i++){
		scanf("%d %d",&u,&v);
		if(root(u)!=root(v)){
			Union(u,v);
		}
	}
	for(int i=1;i<=n;i++){
		int p = root(i);
		if(!seen[p]){
			seen[p] = true;
//			cout<<cost[p]<<endl;
			ans += cost[p];
		}
	}
	printf("%I64d\n",ans);
	return 0;
}


#include <bits/stdc++.h>
#define mp make_pair
using namespace std;
typedef pair<int,int> pi;
const int N = 100005;
int n,m;
int size[N],id[N];
bool seen[N];
long long cost[N];
void __init(){
	for(int i=1;i<=n;i++){
		id[i] = i;
		seen[i] = false;
		size[i] = 1;
	}
}
int root(int a){
	while(id[a]!=a){
		a = id[a];
	}
	return a;
}
long  long Min(long long a,long long b){
	if(a<b) return a;
	else return b;
}
void Union(int a,int b){
	int p = root(a),q = root(b);
	if(size[p]<size[q]){
		id[p] = q;
		cost[p] = Min(cost[p],Min(cost[a],cost[b]));
		size[p]+=size[q];
	}
	else{
		id[q] = p;
		cost[q] = Min(cost[q],Min(cost[a],cost[b]));
		size[q]+=size[p];
	}
}

int main(){
//	freopen("i.in","r",stdin);
	int u,v;
	scanf("%d %d",&n,&m);
	__init();
	for(int i=1;i<=n;i++){
		scanf("%I64d",&cost[i]);
	}
	long long  ans = 0;
	for(int i=0;i<m;i++){
		scanf("%d %d",&u,&v);
		if(root(u)!=root(v)){
			Union(u,v);
		}
	}
	for(int i=1;i<=n;i++){
		int p = root(i);
		if(!seen[p]){
			seen[p] = true;
//			cout<<cost[p]<<endl;
			ans += cost[p];
		}
	}
	printf("%I64d\n",ans);
	return 0;
}


#include <bits/stdc++.h>
#define mp make_pair
using namespace std;
typedef pair<int,int> pi;
const int N = 100005;
int n,m;
int size[N],id[N];
bool seen[N];
long long cost[N];
void __init(){
	for(int i=1;i<=n;i++){
		id[i] = i;
		seen[i] = false;
		size[i] = 1;
	}
}
int root(int a){
	while(id[a]!=a){
		a = id[a];
	}
	return a;
}
long  long Min(long long a,long long b){
	if(a<b) return a;
	else return b;
}
void Union(int a,int b){
	int p = root(a),q = root(b);
	if(size[p]<size[q]){
		id[p] = q;
		size[p]+=size[q];
	}
	else{
		id[q] = p;
		size[q]+=size[p];
	}
}

int main(){
//	freopen("i.in","r",stdin);
	int u,v;
	scanf("%d %d",&n,&m);
	__init();
	for(int i=1;i<=n;i++){
		scanf("%I64d",&cost[i]);
	}
	long long  ans = 0;
	for(int i=0;i<m;i++){
		scanf("%d %d",&u,&v);
		if(root(u)!=root(v)){
			Union(u,v);
		}
	}
	for(int i=1;i<=n;i++){
		int p = root(i);
		cost[p] = Min(cost[i],cost[p]);
	}
	for(int i=1;i<=n;i++){
		int p = root(i);
		if(!seen[p]){
			seen[p] = true;
//			cout<<cost[p]<<endl;
			ans += cost[p];
		}
	}
	printf("%I64d\n",ans);
	return 0;
}


#include <bits/stdc++.h>
#define mp make_pair
using namespace std;

const int N = 100005;
int n,m;
int size[N],id[N];
bool seen[N];
long long cost[N];
void __init(){
	for(int i=1;i<=n;i++){
		id[i] = i;
		seen[i] = false;
		size[i] = 1;
	}
}
int root(int a){
	while(id[a]!=a){
		a = id[a];
	}
	return a;
}
long  long Min(long long a,long long b){
	if(a<b) return a;
	else return b;
}
void Union(int a,int b){
	int p = root(a),q = root(b);
	if(size[p]>size[q]){
		id[p] = q;
		size[p]+=size[q];
	}
	else{
		id[q] = p;
		size[q]+=size[p];
	}
}

int main(){
//	freopen("i.in","r",stdin);
	int u,v;
	scanf("%d %d",&n,&m);
	__init();
	for(int i=1;i<=n;i++){
		scanf("%I64d",&cost[i]);
	}
	long long  ans = 0;
	for(int i=0;i<m;i++){
		scanf("%d %d",&u,&v);
		if(root(u)!=root(v)){
			Union(u,v);
		}
	}
	for(int i=1;i<=n;i++){
		int p = root(i);
		cost[p] = Min(cost[i],cost[p]);
	}
	for(int i=1;i<=n;i++){
		int p = root(i);
		if(!seen[p]){
			seen[p] = true;
//			cout<<cost[p]<<endl;
			ans += cost[p];
		}
	}
	printf("%I64d\n",ans);
	return 0;
}


#include <bits/stdc++.h>
#define mp make_pair
using namespace std;
typedef pair<int,int> pi;
const int N = 100005;
int n,m;
int cost[N],size[N];
bool seen[N];
int id[N];
void __init(){
	for(int i=1;i<=n;i++){
		id[i] = i;
		seen[i] = false;
		size[i] = 1;
	}
}
int root(int a){
	while(id[a]!=a){
		a = id[a];
	}
	return a;
}
void Union(int a,int b){
	int p = root(a),q = root(b);
	if(size[p]<size[q]){
		id[p] = q;
		cost[p] = min(cost[a],cost[b]);
		size[p]+=size[q];
	}
	else{
		id[q] = p;
		cost[q] = min(cost[a],cost[b]);
		size[q]+=size[p];
	}
}
int main(){
//	freopen("i.in","r",stdin);
	int u,v;
	scanf("%d %d",&n,&m);
	__init();
	for(int i=1;i<=n;i++){
		scanf("%d",&cost[i]);
	}
	long long  ans = 0;
	for(int i=0;i<m;i++){
		scanf("%d %d",&u,&v);
		if(root(u)!=root(v)){
			Union(u,v);
		}
	}
	for(int i=1;i<=n;i++){
		int p = root(i);
		if(!seen[p]){
			seen[p] = true;
//			cout<<cost[p]<<endl;
			ans += cost[p];
		}
	}
	printf("%I64d\n",ans);
	return 0;
}


#include <bits/stdc++.h>
#define mp make_pair
using namespace std;

const int N = 100005;
int n,m;
int size[N],id[N];
bool seen[N];
long long cost[N];
void __init(){
	for(int i=1;i<=n;i++){
		id[i] = i;
		seen[i] = false;
		size[i] = 1;
	}
}
int root(int a){
	while(id[a]!=a){
		a = id[a];
	}
	return a;
}
long  long Min(long long a,long long b){
	if(a<b) return a;
	else return b;
}
void Union(int a,int b){
	int p = root(a),q = root(b);
	if(size[p]>size[q]){
		id[p] = q;
		size[p]+=size[q];
	}
	else{
		id[q] = p;
		size[q]+=size[p];
	}
}

int main(){
//	freopen("i.in","r",stdin);
	int u,v;
	scanf("%d %d",&n,&m);
	__init();
	for(int i=1;i<=n;i++){
		scanf("%I64d",&cost[i]);
	}
	long long  ans = 0;
	for(int i=0;i<m;i++){
		scanf("%d %d",&u,&v);
		if(root(u)!=root(v)){
			Union(u,v);
		}
	}
	for(int i=1;i<=n;i++){
		int p = root(i);
		cost[p] = Min(cost[i],cost[p]);
	}
	for(int i=1;i<=n;i++){
		int p = root(i);
		if(!seen[p]){
			seen[p] = true;
			ans += cost[p];
		}
	}
	printf("%I64d\n",ans);
	return 0;
}


#include <bits/stdc++.h>
#define mp make_pair
using namespace std;
typedef pair<int,int> pi;
const int N = 100005;
int n,m;
int size[N],id[N];
bool seen[N];
long long cost[N];
void __init(){
	for(int i=1;i<=n;i++){
		id[i] = i;
		seen[i] = false;
		size[i] = 1;
	}
}
int root(int a){
	while(id[a]!=a){
		a = id[a];
	}
	return a;
}
long  long Min(long long a,long long b){
	return (a<=b) ? a:b;
}
void Union(int a,int b){
	int p = root(a),q = root(b);
	if(size[p]<size[q]){
		id[p] = q;
		cost[p] = Min(cost[a],cost[b]);
		size[p]+=size[q];
	}
	else{
		id[q] = p;
		cost[q] = Min(cost[a],cost[b]);
		size[q]+=size[p];
	}
}

int main(){
//	freopen("i.in","r",stdin);
	int u,v;
	scanf("%d %d",&n,&m);
	__init();
	for(int i=1;i<=n;i++){
		scanf("%I64d",&cost[i]);
	}
	long long  ans = 0;
	for(int i=0;i<m;i++){
		scanf("%d %d",&u,&v);
		if(root(u)!=root(v)){
			Union(u,v);
		}
	}
	for(int i=1;i<=n;i++){
		int p = root(i);
		if(!seen[p]){
			seen[p] = true;
//			cout<<cost[p]<<endl;
			ans += cost[p];
		}
	}
	printf("%I64d\n",ans);
	return 0;
}


#include<bits/stdc++.h>
#define sp (" ")
#define el  ("\n")
#define ll long long
using namespace std;
ll n,m,ans=0,ma,i,j,u,v;
vector<ll>c(100005);
vector<bool>visit(100005);
vector<ll>adj[100005];
void dfs(ll i)
{
	visit[i]=1;
	ma=min(ma,c[i]);
	for(j=0;j<adj[i].size();j++)
	{
		if(visit[adj[i][j]]==0)
		{
			dfs(adj[i][j]);
		}
	}
}
int main()
{

    ios_base::sync_with_stdio();
    cin.tie(0);
    cout.tie(0);
    cin>>n>>m;

    for(i=1;i<=n;i++)
    {
    	cin>>c[i];
	}
for(i=0;i<m;i++)
{
	cin>>u>>v;
	adj[u].push_back(v);
	adj[v].push_back(u);
}
for(i=1;i<=n;i++)
{
	ma=2e9;
	if(visit[i]==0)
	{
		dfs(i);
		ans+=ma;
	}
}
cout<<ans;
}


#include<bits/stdc++.h>
#define sp (" ")
#define el  ("\n")
#define ll long long
using namespace std;
ll n,m,ans=0,ma,i,j,u,v;
vector<ll>c(100005);
vector<bool>visit(100005);
vector<ll>adj[100005];
void dfs(ll i)
{
	visit[i]=1;
	ma=min(ma,c[i]);
	for(j=0;j<adj[i].size();j++)
	{
		if(visit[adj[i][j]]==0)
		{
			dfs(adj[i][j]);
		}
	}
}
int main()
{
    ios_base::sync_with_stdio();
    cin.tie(0);
    cout.tie(0);
    cin>>n>>m;

    for(i=1;i<=n;i++)
    {
    	cin>>c[i];
	}
for(i=0;i<m;i++)
{
	cin>>u>>v;
	adj[u].push_back(v);
	adj[v].push_back(u);
}
for(i=1;i<=n;i++)
{
	ma=2e9;
	if(visit[i]==0)
	{
		dfs(i);
		ans+=ma;
	}
}
cout<<ans;
}


#include<bits/stdc++.h>
#define sp (" ")
#define el  ("\n")
#define ll long long
using namespace std;
int main()
{
    ios_base::sync_with_stdio();
    cin.tie(0);
    cout.tie(0);
    ll n,m,i,j,t,u,ans=0;
    cin>>n>>m;
    vector<ll>a(n+1);
    vector<ll>b[n+1];
    for(i=1;i<=n;i++)
	{
		cin>>a[i];
	}
	for(i=0;i<m;i++)
	{
		cin>>t>>u;
		b[t].push_back(u);
		b[u].push_back(t);

	}
	vector<ll>visit(n+1,0);
	for(i=1;i<=n;i++)
	{
	if(visit[i]==0)
	{
		ll cost=a[i];
	  for(j=0;j<b[i].size();j++)
	  {
	  	if(visit[b[i][j]]==1)
	  	{
	  		cost=0;
	  		break;

		  }
		  cost=min(cost,a[b[i][j]]);
	  	 visit[b[i][j]]=1;
		 }
		 ans+=cost;
	}
}
cout<<ans;
}


#include<bits/stdc++.h>
#define sp (" ")
#define el  ("\n")
#define ll long long
using namespace std;
ll n,m,ans=0,ma;
vector<ll>c(100005);
vector<bool>visit(100005);
vector<ll>adj[100005];
void dfs(ll i)
{
	ll j;
	visit[i]=1;
	ma=min(ma,c[i]);
	for(j=0;j<adj[i].size();j++)
	{
		if(visit[adj[i][j]]==0)
		{
			dfs(adj[i][j]);
		}
	}
}
int main()
{
	ll i,u,v;
    ios_base::sync_with_stdio();
    cin.tie(0);
    cout.tie(0);
    cin>>n>>m;

    for(i=1;i<=n;i++)
    {
    	cin>>c[i];
	}
for(i=0;i<m;i++)
{
	cin>>u>>v;
	adj[u].push_back(v);
	adj[v].push_back(u);
}
for(i=1;i<=n;i++)
{
	ma=2e9;
	if(visit[i]==0)
	{
		dfs(i);
		ans+=ma;
	}
}
cout<<ans;
}


#include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;

vector<int> E[100005];
int n,m,c[100005],tmp;
bool b[100005];
long long ans;

void dfs(int x)
{
	b[x]=true;
	tmp=min(tmp,c[x]);
	for(int j=0;j<E[x].size();j++)
		if(!b[E[x][j]])
			dfs(E[x][j]);
}

int main()
{
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++)
		scanf("%d",&c[i]);
	for(int i=1,u,v;i<=m;i++)
		scanf("%d%d",&u,&v),E[u].push_back(v),E[v].push_back(u);
	for(int i=1;i<=n;i++)
		if(!b[i])
			tmp=2E9,dfs(i),ans+=tmp;
	printf("%I64d",ans);
	return 0;
}


#include<bits/stdc++.h>
#define sp (" ")
#define el  ("\n")
#define ll long long
using namespace std;
ll n,m,ans=0,ma;
vector<ll>c(100005);
vector<bool>visit(100005);
vector<ll>adj[100005];
void dfs(ll i)
{
	ll j;
	visit[i]=1;
	ma=min(ma,c[i]);
	for(j=0;j<adj[i].size();j++)
	{
		if(visit[adj[i][j]]==0)
		{
			dfs(adj[i][j]);
		}
	}
}
int main()
{
ll i,u,v;
    ios_base::sync_with_stdio();
    cin.tie(0);
    cout.tie(0);
    cin>>n>>m;

    for(i=1;i<=n;i++)
    {
    	cin>>c[i];
	}
for(i=0;i<m;i++)
{
	cin>>u>>v;
	adj[u].push_back(v);
	adj[v].push_back(u);
}
for(i=1;i<=n;i++)
{
	ma=2e9;
	if(visit[i]==0)
	{
		dfs(i);
		ans+=ma;
	}
}
cout<<ans;
}


#include<bits/stdc++.h>
#define sp (" ")
#define el  ("\n")
#define ll long long
using namespace std;
int main()
{
    ios_base::sync_with_stdio();
    cin.tie(0);
    cout.tie(0);
    ll n,m,i,j,t,u,ans=0;
    cin>>n>>m;
    vector<ll>a(n+1);
    vector<ll>b[n+1];
    for(i=1;i<=n;i++)
	{
		cin>>a[i];
	}
	for(i=0;i<m;i++)
	{
		cin>>t>>u;
		b[t].push_back(u);
		b[u].push_back(t);

	}
	vector<int>visit(n+1,0);
	for(i=1;i<=n;i++)
	{
	if(visit[i]==0)
	{
		ll cost=a[i];
	  for(j=0;j<b[i].size();j++)
	  {
	  	if(visit[b[i][j]]==1)
	  	{
	  		cost=0;
	  		break;

		  }
		  cost=min(cost,a[b[i][j]]);
	  	 visit[b[i][j]]=1;
		 }
		 ans+=cost;
	}
}
cout<<ans;
}


#include<bits/stdc++.h>
#define sp (" ")
#define el  ("\n")
#define ll long long
using namespace std;
ll n,m,ans=0,ma,i,j,u,v;
vector<ll>c(100001);
vector<bool>visit(100001);
vector<ll>adj[100001];
void dfs(ll i)
{
	visit[i]=1;
	ma=min(ma,c[i]);
	for(j=0;j<adj[i].size();j++)
	{
		if(visit[adj[i][j]]==0)
		{
			dfs(adj[i][j]);
		}
	}
}
int main()
{
    ios_base::sync_with_stdio();
    cin.tie(0);
    cout.tie(0);
    cin>>n>>m;

    for(i=1;i<=n;i++)
    {
    	cin>>c[i];
	}
for(i=0;i<m;i++)
{
	cin>>u>>v;
	adj[u].push_back(v);
	adj[v].push_back(u);
}
for(i=1;i<=n;i++)
{
	ma=1e9;
	if(visit[i]==0)
	{
		dfs(i);
		ans+=ma;
	}
}
cout<<ans;
}


#include<bits/stdc++.h>
#define sp (" ")
#define el  ("\n")
#define ll long long
using namespace std;
ll n,m,ans=0,ma,i,u,v,j;
vector<ll>c(100005);
vector<bool>visit(100005);
vector<ll>adj[100005];
void dfs(ll i)
{
	visit[i]=1;
	ma=min(ma,c[i]);
	for(j=0;j<adj[i].size();j++)
	{
		if(visit[adj[i][j]]==0)
		{
			dfs(adj[i][j]);
		}
	}
}
int main()
{

    ios_base::sync_with_stdio();
    cin.tie(0);
    cout.tie(0);
    cin>>n>>m;

    for(i=1;i<=n;i++)
    {
    	cin>>c[i];
	}
for(i=0;i<m;i++)
{
	cin>>u>>v;
	adj[u].push_back(v);
	adj[v].push_back(u);
}
for(i=1;i<=n;i++)
{
	ma=2e9;
	if(visit[i]==0)
	{
		dfs(i);
		ans+=ma;
	}
}
cout<<ans;
}


#include<bits/stdc++.h>
#define sp (" ")
#define el  ("\n")
#define ll long long
using namespace std;
ll n,m,ans=0,ma,i,u,v;
vector<ll>c(100005);
vector<bool>visit(100005);
vector<ll>adj[100005];
void dfs(ll i)
{
	ll j;
	visit[i]=1;
	ma=min(ma,c[i]);
	for(j=0;j<adj[i].size();j++)
	{
		if(visit[adj[i][j]]==0)
		{
			dfs(adj[i][j]);
		}
	}
}
int main()
{

    ios_base::sync_with_stdio();
    cin.tie(0);
    cout.tie(0);
    cin>>n>>m;

    for(i=1;i<=n;i++)
    {
    	cin>>c[i];
	}
for(i=0;i<m;i++)
{
	cin>>u>>v;
	adj[u].push_back(v);
	adj[v].push_back(u);
}
for(i=1;i<=n;i++)
{
	ma=2e9;
	if(visit[i]==0)
	{
		dfs(i);
		ans+=ma;
	}
}
cout<<ans;
}


#include<iostream>
#include <bits/stdc++.h>
#include<string>
#include<algorithm>
#include<vector>
using namespace std;
typedef long long int into;
#define w 1000000007;
vector<int>comp;
bool used[2000];

void dfs(int v,vector<vector<int>>adj) {
    used[v] = true ;
    comp.push_back(v);
    for (int i = 0; i < adj[v].size(); ++i) {
        int to = adj[v][i];
        if (!used[to])
            dfs(to,adj);
    }
}

void find_comps(vector<vector<int>>adj,int a[],int n) {

  int count=0;
    for (int i = 1; i < n+1 ; ++i)
        used [i] = false;

    for (int i = 1; i < n+1 ; ++i)
        {
          if (!used[i]) {
            comp.clear();
            dfs(i,adj);
            int min=a[comp[0]-1];

            for(int z=0;z<comp.size();z++)
            { if(a[comp[z]-1]<min)
              min=a[comp[z]-1];
            } /*cout<<i<<" "<<min<<endl;*/
             count+=min;

} }cout<<count<<endl;
}

void bfs(vector< vector<int> >adj,int s,int n)
 {
  queue<int>q;
  vector<bool>vis(n+1,false);
  vector<int>parent(n+1);

 parent[s]=0;
 q.push(s);

 vis[s]=true;

 while(!q.empty())
 {

   int v=q.front();
   q.pop();
   for( int u:adj[v])
   {
      if(!vis[u])
      {
         parent[u]=v;
         q.push(u);
        vis[u]=true;
         }

     }

  }   for(int i=2;i<n;i++)
      if(!vis[i])
      {
        cout<<"NO"<<endl;
        return;
      }
     cout<<"YES"<<endl;

   for(int i=2;i<=n;i++)
      cout<<parent[i]<<" "<<i<<endl;
/*if(!vis[n])
  cout<<"NO";
else
{ vector<int>path;

  for(int l=n;parent[l]!=0;l=parent[l])
    path.push_back(l);
  reverse(path.begin(),path.end());
  cout<<"YES"<<endl<<path.size()+1<<endl;
  cout<<s<<" ";
  for(int i:path)
    cout<<i<<" ";*/
}

int main() {
  /*#ifndef ONLINE_JUDGE
  freopen("input.txt","r",stdin);
  freopen("out1.txt","w",stdout);
  #endif*/

ios_base::sync_with_stdio (false);
 int n,m;
  cin>>n>>m;
  vector<vector<int>>adj(n+1);

int a[n];
for(int i=0;i<n;i++)
  cin>>a[i];
int x,y;
while(m--)
{ cin>>x>>y;
  adj[x].push_back(y);
  adj[y].push_back(x);

}
find_comps(adj,a,n);
}


#include<iostream>
#include <bits/stdc++.h>
#include<string>
#include<algorithm>
#include<vector>
using namespace std;
typedef long long int into;
#define w 1000000007;

void dfs(int v,vector<vector<int>>adj,bool used[]) {
    used[v] = true ;

    for (int i = 0; i < adj[v].size(); ++i) {
        int to = adj[v][i];
        if (!used[to])
            dfs(to,adj,used);
    }
}

void find_comps(vector<vector<int>>adj,vector<pair<int,int>>price,int n) {
  bool used[n+1];

  int count=0;
    for (int i = 0; i < n ; ++i)
        used [price[i].second] = false;

    for (int i = 0; i < n ; ++i)
        {
          if (!used[price[i].second]) {
            dfs(price[i].second,adj,used);
            count+=price[i].first;

} }cout<<count<<endl;
}

void bfs(vector< vector<int> >adj,int s,int n)
 {
  queue<int>q;
  vector<bool>vis(n+1,false);
  vector<int>parent(n+1);

 parent[s]=0;
 q.push(s);

 vis[s]=true;

 while(!q.empty())
 {

   int v=q.front();
   q.pop();
   for( int u:adj[v])
   {
      if(!vis[u])
      {
         parent[u]=v;
         q.push(u);
        vis[u]=true;
         }

     }

  }   for(int i=2;i<n;i++)
      if(!vis[i])
      {
        cout<<"NO"<<endl;
        return;
      }
     cout<<"YES"<<endl;

   for(int i=2;i<=n;i++)
      cout<<parent[i]<<" "<<i<<endl;
/*if(!vis[n])
  cout<<"NO";
else
{ vector<int>path;

  for(int l=n;parent[l]!=0;l=parent[l])
    path.push_back(l);
  reverse(path.begin(),path.end());
  cout<<"YES"<<endl<<path.size()+1<<endl;
  cout<<s<<" ";
  for(int i:path)6 5

    cout<<i<<" ";*/
}

int main() {
  /*#ifndef ONLINE_JUDGE
  freopen("input.txt","r",stdin);
  freopen("out1.txt","w",stdout);
  #endif */

ios_base::sync_with_stdio (false);
 int n,m,x,y;
  cin>>n>>m;

  vector<vector<int>>adj(n);
  vector<pair<int,int>>price;
  for (int i = 0; i < n; ++i)
  {  cin>>x;
    price.push_back(make_pair(x,i));
  }
 sort(price.begin(),price.end());
 for (int i = 0; i < n; ++i)
 {
   cout<<price[i].first<<" "<<price[i].second<<endl;
 }

while(m--)
  { cin>>x>>y;
    adj[x-1].push_back(y-1);
    adj[y-1].push_back(x-1);

}

   find_comps(adj,price,n);

     }


#include<iostream>
#include<bits/stdc++.h>
using namespace std;
typedef long long int into;
void dfs(map<into,bool>&vist,vector<vector<int>>&adj,into price[], into sour, into &count)
{ vist[sour]=true;
  count=min(count,price[sour]);
   for(int u:adj[sour])
    { if(!vist[u])
     {
       dfs(vist,adj,price,u,count);
    }

} }

  int main() {
  /*#ifndef ONLINE_JUDGE
  freopen("input.txt","r",stdin);
  freopen("out1.txt","w",stdout);
  #endif */

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

 into n,m;
 cin>>n>>m;
 vector<vector<int>>adj(n);
 into x,y;
 into price[n];
 for (into i = 0; i < n; ++i)
 {
   cin>>price[i];
 }
 while(m--)
 {  cin>>x>>y;
   adj[x-1].push_back(y-1);
   adj[y-1].push_back(x-1);
  }
    map<into,bool>vist;
    into ans=0;
    for (into i = 0; i < n; ++i)
    { into count=INT_MAX;
      if(!vist[i])
      { dfs(vist,adj,price,i,count);ans+=count;}

      }
     cout<<ans<<endl;

  }


#include<iostream>
#include <bits/stdc++.h>
#include<string>
#include<algorithm>
#include<vector>
using namespace std;
typedef long long int into;
#define w 1000000007;
vector<int>comp;
void swap(int* a, int* b)
{
    int t = *a;
    *a = *b;
    *b = t;
}

/* This function takes last element as pivot, places
the pivot element at its correct position in sorted
array, and places all smaller (smaller than pivot)
to left of pivot and all greater elements to right
of pivot */
int partition (int arr[],int brr[], int low, int high)
{
    int pivot = arr[high]; // pivot
    int i = (low - 1); // Index of smaller element

    for (int j = low; j <= high - 1; j++)
    {
        // If current element is smaller than the pivot
        if (arr[j] < pivot)
        {
            i++; // increment index of smaller element
            swap(&brr[i], &brr[j]);
        }
    }
    swap(&brr[i + 1], &brr[high]);
    return (i + 1);
}

/* The main function that implements QuickSort
arr[] --> Array to be sorted,
low --> Starting index,
high --> Ending index */
void quickSort(int arr[],int brr[], int low, int high)
{
    if (low < high)
    {
        /* pi is partitioning index, arr[p] is now
        at right place */
        int pi = partition(arr,brr, low, high);

        // Separately sort elements before
        // partition and after partition
        quickSort(arr,brr, low, pi - 1);
        quickSort(arr,brr, pi + 1, high);
    }
}

void dfs(int v,vector<vector<int>>adj,bool used[]) {
    used[v] = true ;
    comp.push_back(v);
    for (int i = 0; i < adj[v].size(); ++i) {
        int to = adj[v][i];
        if (!used[to])
            dfs(to,adj,used);
    }
}

void find_comps(vector<vector<int>>adj,int a[],int price[],int n) {
  bool used[n+1];

  int count=0;
    for (int i = 0; i < n ; ++i)
        used [a[i]] = false;

    for (int i = 0; i < n ; ++i)
        {
          if (!used[a[i]]) {
            comp.clear();
            dfs(a[i],adj,used);
            count+=price[a[i]];

} }cout<<count<<endl;
}

void bfs(vector< vector<int> >adj,int s,int n)
 {
  queue<int>q;
  vector<bool>vis(n+1,false);
  vector<int>parent(n+1);

 parent[s]=0;
 q.push(s);

 vis[s]=true;

 while(!q.empty())
 {

   int v=q.front();
   q.pop();
   for( int u:adj[v])
   {
      if(!vis[u])
      {
         parent[u]=v;
         q.push(u);
        vis[u]=true;
         }

     }

  }   for(int i=2;i<n;i++)
      if(!vis[i])
      {
        cout<<"NO"<<endl;
        return;
      }
     cout<<"YES"<<endl;

   for(int i=2;i<=n;i++)
      cout<<parent[i]<<" "<<i<<endl;
/*if(!vis[n])
  cout<<"NO";
else
{ vector<int>path;

  for(int l=n;parent[l]!=0;l=parent[l])
    path.push_back(l);
  reverse(path.begin(),path.end());
  cout<<"YES"<<endl<<path.size()+1<<endl;
  cout<<s<<" ";
  for(int i:path)6 5

    cout<<i<<" ";*/
}

int main() {
  /*#ifndef ONLINE_JUDGE
  freopen("input.txt","r",stdin);
  freopen("out1.txt","w",stdout);
  #endif */

ios_base::sync_with_stdio (false);
 int n,m;
  cin>>n>>m;
  int price[n];
  vector<vector<int>>adj(n);
  int a[n];
  for (int i = 0; i < n; ++i)
  {
    a[i]=i;
  }
  for (int i = 0; i < n; ++i)
  {
    cin>>price[i];
  } int x,y;
while(m--)
  { cin>>x>>y;
    adj[x-1].push_back(y-1);
    adj[y-1].push_back(x-1);

}
   quickSort(price,a,0,n-1);
   find_comps(adj,a,price,n);

     }