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

#define pii pair<ll,ll>
#define se second
#define fi first
#define pb push_back
#define pob pop_back()
#define pf push_front
#define pof pop_front()
#define mp make_pair
//#define endl '\n'
#define fast ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define ll long long
const ll INF=15e8;

vector<pii> g[500+50];
bool mark[500+50];
int l,r;
void dfs(int v){
	mark[v]=1;
	for(pii i:g[v])
		if(!mark[i.fi]){ if( (i.se<l || i.se>r))dfs(i.fi);}
}

int main(){
	int n,m;
	cin>>n>>m;
	for(int i=0;i<m;i++){
		int t,k;
		cin>>k>>t;
		g[t].pb({k,i+1});
		g[k].pb({t,i+1});
	}
	int k;
	cin>>k;
	while(k--){
		memset(mark,0,sizeof mark);
	//	int l,r;
		cin>>l>>r;
		int ans=0;
		for(int i=1;i<=n;i++){
			if(!mark[i]){
				dfs(i);
				ans++;
			}
		}
		cout<<ans<<'\n';
	}
}


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

#define pii pair<int,int>
#define se second
#define fi first
#define pb push_back
#define pob pop_back()
#define pf push_front
#define pof pop_front()
#define mp make_pair
//#define endl '\n'
#define fast ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define ll long long
const ll INF=15e8;

vector<pii> g[500+50];
bool mark[500+50];
pair<pii,int> h[20000+100];
int l,r;
void dfs(int v){
	mark[v]=1;
	for(pii i:g[v])
		if(!mark[i.fi] && (i.se<l || i.se>r))dfs(i.fi);
}
int ans[20000+100];
int main(){
	fast;
	int n,m;
	cin>>n>>m;
	for(int i=0;i<m;i++){
		int t,k;
		cin>>k>>t;
		g[t].pb({k,i+1});
		g[k].pb({t,i+1});
	}
	int k;
	cin>>k;
	for(int i=0;i<k;i++){
		cin>>h[i].fi.fi>>&h[i].fi.se;
		h[i].se=i;
	}
	sort(h,h+k);
	int l1=-1,l2=-1,l3=-1;
	for(int i=0;i<k;i++){
		if(h[i].fi.fi==l1 && h[i].fi.se==l2){
			ans[h[i].se]=l3;
			continue;
		}
		memset(mark,0,sizeof mark);
		l=h[i].fi.fi;
		r=h[i].fi.se;
		l1=l,l2=r,l3=0;
		for(int i=1;i<=n;i++){
			if(!mark[i]){
				l3++;
				dfs(i);
			}
		}
		ans[h[i].se]=l3;
	}
	for(int i=0;i<k;i++)
		printf("%d \n",ans[i]);
}


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

#define pii pair<ll,ll>
#define se second
#define fi first
#define pb push_back
#define pob pop_back()
#define pf push_front
#define pof pop_front()
#define mp make_pair
//#define endl '\n'
#define fast ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define ll long long
const ll INF=15e8;

vector<pii> g[500+50];
bool mark[500+50];
int l,r;
void dfs(int v){
	mark[v]=1;
	for(pii i:g[v])
		if(!mark[i.fi]){ if( (i.se<l || i.se>r))dfs(i.fi);}
}

int main(){
	fast;
	int n,m;
	cin>>n>>m;
	for(int i=0;i<m;i++){
		int t,k;
		cin>>k>>t;
		g[t].pb({k,i+1});
		g[k].pb({t,i+1});
	}
	int k;
	cin>>k;
	while(k--){
		memset(mark,0,sizeof mark);
	//	int l,r;
		cin>>l>>r;
		int ans=0;
		for(int i=1;i<=n;i++){
			if(!mark[i]){
				dfs(i);
				ans++;
			}
		}
		cout<<ans<<'\n';
	}
}


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

#define pii pair<ll,ll>
#define se second
#define fi first
#define pb push_back
#define pob pop_back()
#define pf push_front
#define pof pop_front()
#define mp make_pair
//#define endl '\n'
#define fast ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define ll long long
const ll INF=15e8;

vector<pii> g[500+50];
bool mark[500+50];
pair<pii,int> h[20000+100];
int l,r;
void dfs(int v){
	mark[v]=1;
	for(pii i:g[v])
		if(!mark[i.fi] && (i.se<l || i.se>r))dfs(i.fi);
}
int ans[20000+100];
int main(){
	fast;
	int n,m;
	cin>>n>>m;
	for(int i=0;i<m;i++){
		int t,k;
		cin>>k>>t;
		g[t].pb({k,i+1});
		g[k].pb({t,i+1});
	}
	int k;
	cin>>k;
	for(int i=0;i<k;i++){
		cin>>h[i].fi.fi>>h[i].fi.se;
		h[i].se=i;
	}
	sort(h,h+k);
	int l1=-1,l2=-1,l3=-1;
	for(int i=0;i<k;i++){
		if(h[i].fi.fi==l1 && h[i].fi.se==l2){
			ans[h[i].se]=l3;
			continue;
		}
		memset(mark,0,sizeof mark);
		l=h[i].fi.fi;
		r=h[i].fi.se;
		l1=l,l2=r,l3=0;
		for(int i=1;i<=n;i++){
			if(!mark[i]){
				l3++;
				dfs(i);
			}
		}
		ans[h[i].se]=l3;
	}
	for(int i=0;i<k;i++)
		printf("%d \n",ans[i]);
}


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

#define pii pair<ll,ll>
#define se second
#define fi first
#define pb push_back
#define pob pop_back()
#define pf push_front
#define pof pop_front()
#define mp make_pair
//#define endl '\n'
#define fast ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define ll long long
const ll INF=15e8;

vector<pii> g[500+50];
bool mark[500+50];
pair<pii,int> h[20000+100];
int l,r;
void dfs(int v){
	mark[v]=1;
	for(pii i:g[v])
		if(!mark[i.fi] && (i.se<l || i.se>r))dfs(i.fi);
}
int ans[20000+100];
int main(){
	fast;
	int n,m;
	cin>>n>>m;
	for(int i=0;i<m;i++){
		int t,k;
		cin>>k>>t;
		g[t].pb({k,i+1});
		g[k].pb({t,i+1});
	}
	int k;
	cin>>k;
	for(int i=0;i<k;i++){
		cin>>h[i].fi.fi>>&h[i].fi.se;
		h[i].se=i;
	}
	sort(h,h+k);
	int l1=-1,l2=-1,l3=-1;
	for(int i=0;i<k;i++){
		if(h[i].fi.fi==l1 && h[i].fi.se==l2){
			ans[h[i].se]=l3;
			continue;
		}
		memset(mark,0,sizeof mark);
		l=h[i].fi.fi;
		r=h[i].fi.se;
		l1=l,l2=r,l3=0;
		for(int i=1;i<=n;i++){
			if(!mark[i]){
				l3++;
				dfs(i);
			}
		}
		ans[h[i].se]=l3;
	}
	for(int i=0;i<k;i++)
		printf("%d \n",ans[i]);
}


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

#define pii pair<int,int>
#define se second
#define fi first
#define pb push_back
#define pob pop_back()
#define pf push_front
#define pof pop_front()
#define mp make_pair
//#define endl '\n'
#define fast ios::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL)
#define ll long long
const ll INF=15e8;

vector<pii> g[500+50];
bool mark[500+50];
pair<pii,int> h[20000+100];
int l,r;
bool check=1;
void dfs(int v){
	mark[v]=check;
	for(int i=0;i<g[v].size();i++){
		pii u=g[v][i];
		if(mark[u.fi]!=check)if(u.se<l || u.se>r)dfs(u.fi);
	}
}
int ans[10000+100];
int main(){
	fast;
	int n,m;
	cin>>n>>m;
	for(int i=0;i<m;i++){
		int t,k;
		cin>>k>>t;
		g[t].pb({k,i+1});
		g[k].pb({t,i+1});
	}
	int k;
	cin>>k;
	int kik,jok;
	for(int i=0;i<k;i++){
		scanf("%d%d",&kik,&jok);
		h[i]=mp(mp(kik,jok),i);
	}
	sort(h,h+k);
	int l1=-1,l2=-1,l3=-1;
	for(int i=0;i<k;i++){
		if(h[i].fi.fi==l1 && h[i].fi.se==l2){
			ans[h[i].se]=l3;
			continue;
		}
	//	memset(mark,0,sizeof mark);
	//	:-(
		l=h[i].fi.fi;
		r=h[i].fi.se;
		l1=l,l2=r,l3=0;
		for(int i=1;i<=n;i++){
			if(mark[i]!=check){
				l3++;
				dfs(i);
			}
		}
		ans[h[i].se]=l3;
		check=!check;
	}
	for(int i=0;i<k;i++)
		printf("%d \n",ans[i]);
}


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

#define pii pair<int,int>
#define se second
#define fi first
#define pb push_back
#define pob pop_back()
#define pf push_front
#define pof pop_front()
#define mp make_pair
//#define endl '\n'
#define fast ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define ll long long
const ll INF=15e8;

vector<pii> g[500+50];
bool mark[500+50];
pair<pii,int> h[20000+100];
int l,r;
bool check=1;
void dfs(int v){
	mark[v]=check;
	for(int i=0;i<g[v].size();i++){
		pii u=g[v][i];
		if(mark[u.fi]!=check && (u.se<l || u.se>r))dfs(u.fi);
	}
}
int ans[20000+100];
int main(){
	fast;
	int n,m;
	cin>>n>>m;
	for(int i=0;i<m;i++){
		int t,k;
		cin>>k>>t;
		g[t].pb({k,i+1});
		g[k].pb({t,i+1});
	}
	int k;
	cin>>k;
	for(int i=0;i<k;i++){
		cin>>h[i].fi.fi>>h[i].fi.se;
		h[i].se=i;
	}
//	sort(h,h+k);
	int l1=-1,l2=-1,l3=-1;
	for(int i=0;i<k;i++){
		if(h[i].fi.fi==l1 && h[i].fi.se==l2){
			ans[h[i].se]=l3;
			continue;
		}
	//	memset(mark,0,sizeof mark);
		l=h[i].fi.fi;
		r=h[i].fi.se;
		l1=l,l2=r,l3=0;
		for(int i=1;i<=n;i++){
			if(mark[i]!=check){
				l3++;
				dfs(i);
			}
		}
		ans[h[i].se]=l3;
		check=!check;
	}
	for(int i=0;i<k;i++)
		printf("%d \n",ans[i]);
}


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

#define pii pair<ll,ll>
#define se second
#define fi first
#define pb push_back
#define pob pop_back()
#define pf push_front
#define pof pop_front()
#define mp make_pair
//#define endl '\n'
#define fast ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define ll long long
const ll INF=15e8;

vector<pii> g[500+50];
bool mark[500+50];
pair<pii,int> h[20000+100];
int l,r;
void dfs(int v){
	mark[v]=1;
	for(pii i:g[v])
		if(!mark[i.fi]){ if((i.se<l || i.se>r))dfs(i.fi);}
}
int ans[20000+100];
int main(){
	fast;
	int n,m;
	cin>>n>>m;
	for(int i=0;i<m;i++){
		int t,k;
		cin>>k>>t;
		g[t].pb({k,i+1});
		g[k].pb({t,i+1});
	}
	int k;
	cin>>k;
	for(int i=0;i<k;i++){
		scanf("%d %d",&h[i].fi.fi,&h[i].fi.se);
		h[i].se=i;
	}
	sort(h,h+k);
	int l1=-1,l2=-1,l3=-1;
	for(int i=0;i<k;i++){
		if(h[i].fi.fi==l1 && h[i].fi.se==l2){
			ans[h[i].se]=l3;
			continue;
		}
		memset(mark,0,sizeof mark);
		l=h[i].fi.fi;
		r=h[i].fi.se;
		l1=l,l2=r,l3=0;
		for(int i=1;i<=n;i++){
			if(!mark[i]){
				l3++;
				dfs(i);
			}
		}
		ans[h[i].se]=l3;
	}
	for(int i=0;i<k;i++)
		printf("%d \n",ans[i]);
}


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

#define pii pair<int,int>
#define se second
#define fi first
#define pb push_back
#define pob pop_back()
#define pf push_front
#define pof pop_front()
#define mp make_pair
//#define endl '\n'
#define fast ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define ll long long
const ll INF=15e8;

vector<pii> g[500+50];
bool mark[500+50];
pair<pii,int> h[20000+100];
int l,r;
bool check=1;
void dfs(int v){
	mark[v]=check;
	for(int i=0;i<g[v].size();i++){
		pii u=g[v][i];
		if(mark[u.fi]!=check && (u.se<l || u.se>r))dfs(u.fi);
	}
}
int ans[20000+100];
int main(){
	fast;
	int n,m;
	cin>>n>>m;
	for(int i=0;i<m;i++){
		int t,k;
		cin>>k>>t;
		g[t].pb({k,i+1});
		g[k].pb({t,i+1});
	}
	int k;
	cin>>k;
	int l3;
	while(k--){
		l3=0;
		cin>>l>>r;
		for(int i=1;i<=n;i++){
			if(mark[i]!=check){
				l3++;
				dfs(i);
			}
		}
		cout<<l3<<'\n';
		check=!check;
	}
}


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

#define pii pair<int,int>
#define se second
#define fi first
#define pb push_back
#define pob pop_back()
#define pf push_front
#define pof pop_front()
#define mp make_pair
//#define endl '\n'
#define fast ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define ll long long
const ll INF=15e8;

vector<pii> g[500+50];
bool mark[500+50];
pair<pii,int> h[20000+100];
int l,r;
void dfs(int v){
	mark[v]=1;
	for(int i=0;i<g[v].size();i++){
		pii u=g[v][i];
		if(!mark[u.fi] && (u.se<l || u.se>r))dfs(u.fi);
	}
}
int ans[20000+100];
int main(){
	fast;
	int n,m;
	cin>>n>>m;
	for(int i=0;i<m;i++){
		int t,k;
		cin>>k>>t;
		g[t].pb({k,i+1});
		g[k].pb({t,i+1});
	}
	int k;
	cin>>k;
	for(int i=0;i<k;i++){
		cin>>h[i].fi.fi>>h[i].fi.se;
		h[i].se=i;
	}
	sort(h,h+k);
	int l1=-1,l2=-1,l3=-1;
	for(int i=0;i<k;i++){
		if(h[i].fi.fi==l1 && h[i].fi.se==l2){
			ans[h[i].se]=l3;
			continue;
		}
		memset(mark,0,sizeof mark);
		l=h[i].fi.fi;
		r=h[i].fi.se;
		l1=l,l2=r,l3=0;
		for(int i=1;i<=n;i++){
			if(!mark[i]){
				l3++;
				dfs(i);
			}
		}
		ans[h[i].se]=l3;
	}
	for(int i=0;i<k;i++)
		printf("%d \n",ans[i]);
}


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

#define pii pair<int,int>
#define se second
#define fi first
#define pb push_back
#define pob pop_back()
#define pf push_front
#define pof pop_front()
#define mp make_pair
//#define endl '\n'
#define fast ios::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL)
#define ll long long
const ll INF=15e8;

vector<pii> g[500+50];
bool mark[500+50];
pair<pii,int> h[20000+100];
int l,r;
bool check=1;
void dfs(int v){
	mark[v]=check;
	for(int i=0;i<g[v].size();i++){
		pii u=g[v][i];
		if(mark[u.fi]!=check && (u.se<l || u.se>r))dfs(u.fi);
	}
}
int ans[20000+100];
int main(){
	fast;
	int n,m;
	cin>>n>>m;
	for(int i=0;i<m;i++){
		int t,k;
		cin>>k>>t;
		g[t].pb({k,i+1});
		g[k].pb({t,i+1});
	}
	int k;
	cin>>k;
	for(int i=0;i<k;i++){
		cin>>h[i].fi.fi>>h[i].fi.se;
		h[i].se=i;
	}
//	sort(h,h+k);
	int l1=-1,l2=-1,l3=-1;
	for(int i=0;i<k;i++){
		if(h[i].fi.fi==l1 && h[i].fi.se==l2){
			ans[h[i].se]=l3;
			continue;
		}
	//	memset(mark,0,sizeof mark);
	//	:-(
		l=h[i].fi.fi;
		r=h[i].fi.se;
		l1=l,l2=r,l3=0;
		for(int i=1;i<=n;i++){
			if(mark[i]!=check){
				l3++;
				dfs(i);
			}
		}
		ans[h[i].se]=l3;
		check=!check;
	}
	for(int i=0;i<k;i++)
		printf("%d \n",ans[i]);
}


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

#define pii pair<int,int>
#define se second
#define fi first
#define pb push_back
#define pob pop_back()
#define pf push_front
#define pof pop_front()
#define mp make_pair
//#define endl '\n'
#define fast ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define ll long long
const ll INF=15e8;

vector<pii> g[500+50];
bool mark[500+50];
pair<pii,int> h[20000+100];
int l,r;
bool check=1;
void dfs(int v){
	mark[v]=check;
	for(int i=0;i<g[v].size();i++){
		pii u=g[v][i];
		if(mark[u.fi]!=check && (u.se<l || u.se>r))dfs(u.fi);
	}
}
int ans[20000+100];
int main(){
	fast;
	int n,m;
	cin>>n>>m;
	for(int i=0;i<m;i++){
		int t,k;
		cin>>k>>t;
		g[t].pb({k,i+1});
		g[k].pb({t,i+1});
	}
	int k;
	cin>>k;
	for(int i=0;i<k;i++){
		cin>>h[i].fi.fi>>h[i].fi.se;
		h[i].se=i;
	}
	sort(h,h+k);
	int l1=-1,l2=-1,l3=-1;
	for(int i=0;i<k;i++){
		if(h[i].fi.fi==l1 && h[i].fi.se==l2){
			ans[h[i].se]=l3;
			continue;
		}
	//	memset(mark,0,sizeof mark);
		l=h[i].fi.fi;
		r=h[i].fi.se;
		l1=l,l2=r,l3=0;
		for(int i=1;i<=n;i++){
			if(mark[i]!=check){
				l3++;
				dfs(i);
			}
		}
		ans[h[i].se]=l3;
		check=!check;
	}
	for(int i=0;i<k;i++)
		printf("%d \n",ans[i]);
}


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

#define pii pair<ll,ll>
#define se second
#define fi first
#define pb push_back
#define pob pop_back()
#define pf push_front
#define pof pop_front()
#define mp make_pair
//#define endl '\n'
#define fast ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define ll long long
const ll INF=15e8;

vector<pii> g[500+50];
bool mark[500+50];
pair<pii,int> h[20000+100];
int l,r;
void dfs(int v){
	mark[v]=1;
	for(pii i:g[v])
		if((i.se<l || i.se>r)&& !mark[i.fi])dfs(i.fi);
}
int ans[20000+100];
int main(){
	fast;
	int n,m;
	cin>>n>>m;
	for(int i=0;i<m;i++){
		int t,k;
		cin>>k>>t;
		g[t].pb({k,i+1});
		g[k].pb({t,i+1});
	}
	int k;
	cin>>k;
	for(int i=0;i<k;i++){
		cin>>h[i].fi.fi>>h[i].fi.se;
		h[i].se=i;
	}
	sort(h,h+k);
	int l1=-1,l2=-1,l3=-1;
	for(int i=0;i<k;i++){
		if(h[i].fi.fi==l1 && h[i].fi.se==l2){
			ans[h[i].se]=l3;
			continue;
		}
		memset(mark,0,sizeof mark);
		l=h[i].fi.fi;
		r=h[i].fi.se;
		l1=l,l2=r,l3=0;
		for(int i=1;i<=n;i++){
			if(!mark[i]){
				l3++;
				dfs(i);
			}
		}
		ans[h[i].se]=l3;
	}
	for(int i=0;i<k;i++)
		printf("%d \n",ans[i]);
}


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

#define pii pair<ll,ll>
#define se second
#define fi first
#define pb push_back
#define pob pop_back()
#define pf push_front
#define pof pop_front()
#define mp make_pair
//#define endl '\n'
#define fast ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define ll long long
const ll INF=15e8;

vector<pii> g[500+50];
bool mark[500+50];
int l,r;
void dfs(int v){
	mark[v]=1;
	for(pii i:g[v])
		if(i.se<l || i.se>r){ if(!mark[i.fi])dfs(i.fi);}
}

int main(){
	fast;
	int n,m;
	cin>>n>>m;
	for(int i=0;i<m;i++){
		int t,k;
		cin>>k>>t;
		g[t].pb({k,i+1});
		g[k].pb({t,i+1});
	}
	int k;
	cin>>k;
	while(k--){
		memset(mark,0,sizeof mark);
	//	int l,r;
		cin>>l>>r;
		int ans=0;
		for(int i=1;i<=n;i++){
			if(!mark[i]){
				dfs(i);
				ans++;
			}
		}
		cout<<ans<<'\n';
	}
}


import java.util.*;

public class App
{

	public static void main( String[] args )
    {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();

		int ith=0;
		int jth=0;
		long a[][] = new long[n][n];
		for(int i=0; i<n; i++)
		{
			for(int j=0; j<n; j++)
			{
				a[i][j] = sc.nextLong();
				if(a[i][j] == 0)
				{
					ith=i;
					jth=j;
				}
			}
		}
		if(n==1)
		{
			System.out.println(1);
			return;
		}
		HashSet<Long> row = new HashSet<Long>();
		HashSet<Long> col = new HashSet<Long>();

		long zerorow = 0;
		long zerocol = 0;
		for(int i=0; i<n; i++)
		{
			long sumcol = 0;
			long sumrow = 0;
			zerorow += a[ith][i];
			zerocol += a[i][jth];
			for(int j=0; j<n; j++)
			{
				sumrow += a[i][j];
				sumcol += a[j][i];
			}
			row.add(sumrow);
			col.add(sumcol);
		}
		if(row.size()>2 || col.size()>2)
		{
			System.out.println(-1);
			return;
		}
		long sum = 0;
		if(ith!=0)
		{
			for(int i=0; i<n; i++)
				sum += a[0][i];
		}
		else
		{
			for(int i=0; i<n; i++)
				sum += a[1][i];
		}
		if(zerorow!=zerocol || sum-zerorow>0)
		{
			System.out.println(-1);
			return;
		}
		a[ith][jth] = sum - zerorow;

		HashSet<Long> rows = new HashSet<Long>();
		HashSet<Long> cols = new HashSet<Long>();
		long diag1 = 0;
		long diag2 = 0;
		for(int i=0; i<n; i++)
		{
			long sumcol = 0;
			long sumrow = 0;
			zerorow += a[ith][i];
			zerocol += a[i][jth];
			diag1 += a[i][i];
			diag2 += a[i][n-i-1];
			for(int j=0; j<n; j++)
			{
				sumrow += a[i][j];
				sumcol += a[j][i];
			}
			rows.add(sumrow);
			cols.add(sumcol);
		}
		if(rows.size()==1 && cols.size()==1 && diag1==sum && diag2==sum)
			System.out.println(sum-zerorow);
		else
			System.out.println(-1);
    }
}


import java.util.*;

public class App
{

	public static void main( String[] args )
    {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();

		int ith=0;
		int jth=0;
		long a[][] = new long[n][n];
		for(int i=0; i<n; i++)
		{
			for(int j=0; j<n; j++)
			{
				a[i][j] = sc.nextLong();
				if(a[i][j] == 0)
				{
					ith=i;
					jth=j;
				}
			}
		}
		HashSet<Long> row = new HashSet<Long>();
		HashSet<Long> col = new HashSet<Long>();
		long diag1 = 0;
		long diag2 = 0;
		long zerorow = 0;
		long zerocol = 0;
		for(int i=0; i<n; i++)
		{
			long sumcol = 0;
			long sumrow = 0;
			zerorow += a[ith][i];
			zerocol += a[i][jth];
			for(int j=0; j<n; j++)
			{
				diag1 += a[i][i];
				diag2 += a[i][n-i-1];
				sumrow += a[i][j];
				sumcol += a[j][i];
			}
			row.add(sumrow);
			col.add(sumcol);
		}
		if(row.size()>2 || col.size()>2)
		{
			System.out.println(-1);
			return;
		}
		long sum = 0;
		if(ith!=0)
		{
			for(int i=0; i<n; i++)
				sum += a[0][i];
		}
		else
		{
			for(int i=1; i<n; i++)
				sum += a[1][i];
		}

		if(zerorow == zerocol && sum-zerorow>0 && (diag1==diag2 || diag1==zerorow || diag2==zerorow)
				&& diag1<=sum && diag2<=0)
			System.out.println(sum - zerorow);

		else
			System.out.println(-1);
    }
}


import java.util.*;

public class App
{

	public static void main( String[] args )
    {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();

		int ith=0;
		int jth=0;
		long a[][] = new long[n][n];
		for(int i=0; i<n; i++)
		{
			for(int j=0; j<n; j++)
			{
				a[i][j] = sc.nextLong();
				if(a[i][j] == 0)
				{
					ith=i;
					jth=j;
				}
			}
		}
		if(n==1)
		{
			System.out.println(1);
			return;
		}
		HashSet<Long> row = new HashSet<Long>();
		HashSet<Long> col = new HashSet<Long>();

		long zerorow = 0;
		long zerocol = 0;
		for(int i=0; i<n; i++)
		{
			long sumcol = 0;
			long sumrow = 0;
			zerorow += a[ith][i];
			zerocol += a[i][jth];
			for(int j=0; j<n; j++)
			{
				sumrow += a[i][j];
				sumcol += a[j][i];
			}
			row.add(sumrow);
			col.add(sumcol);
		}
		if(row.size()>2 || col.size()>2)
		{
			System.out.println(-1);
			return;
		}
		long sum = 0;
		if(ith!=0)
		{
			for(int i=0; i<n; i++)
				sum += a[0][i];
		}
		else
		{
			for(int i=0; i<n; i++)
				sum += a[1][i];
		}
		if(zerorow!=zerocol || sum-zerorow<=0)
		{
			System.out.println(-1);
			return;
		}
		a[ith][jth] = sum - zerorow;

		HashSet<Long> rows = new HashSet<Long>();
		HashSet<Long> cols = new HashSet<Long>();
		long diag1 = 0;
		long diag2 = 0;
		for(int i=0; i<n; i++)
		{
			long sumcol = 0;
			long sumrow = 0;
			zerorow += a[ith][i];
			zerocol += a[i][jth];
			diag1 += a[i][i];
			diag2 += a[i][n-i-1];
			for(int j=0; j<n; j++)
			{
				sumrow += a[i][j];
				sumcol += a[j][i];
			}
			rows.add(sumrow);
			cols.add(sumcol);
		}
		if(rows.size()==1 && cols.size()==1 && diag1==sum && diag2==sum)
			System.out.println(a[ith][jth]);
		else
			System.out.println(-1);
    }
}


import java.util.*;

public class App
{

	public static void main( String[] args )
    {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();

		int ith=0;
		int jth=0;
		long a[][] = new long[n][n];
		for(int i=0; i<n; i++)
		{
			for(int j=0; j<n; j++)
			{
				a[i][j] = sc.nextLong();
				if(a[i][j] == 0)
				{
					ith=i;
					jth=j;
				}
			}
		}
		if(n==1)
		{
			System.out.println(1);
			return;
		}
		HashSet<Long> row = new HashSet<Long>();
		HashSet<Long> col = new HashSet<Long>();
		long diag1 = 0;
		long diag2 = 0;
		long zerorow = 0;
		long zerocol = 0;
		for(int i=0; i<n; i++)
		{
			long sumcol = 0;
			long sumrow = 0;
			zerorow += a[ith][i];
			zerocol += a[i][jth];
			diag1 += a[i][i];
			diag2 += a[i][n-i-1];
			for(int j=0; j<n; j++)
			{
				sumrow += a[i][j];
				sumcol += a[j][i];
			}
			row.add(sumrow);
			col.add(sumcol);
		}
		if(row.size()>2 || col.size()>2)
		{
			System.out.println(-1);
			return;
		}
		long sum = 0;
		if(ith!=0)
		{
			for(int i=0; i<n; i++)
				sum += a[0][i];
		}
		else
		{
			for(int i=0; i<n; i++)
				sum += a[1][i];
		}
		if(zerorow == zerocol && sum-zerorow>0 && (diag1==diag2 &&
				(diag1==zerorow || diag1==sum)|| Math.min(diag1,diag2)==zerorow)
				&& diag1<=sum && diag2<=sum)
			System.out.println(sum - zerorow);

		else
			System.out.println(-1);
    }
}


import java.util.*;

public class App
{

	public static void main( String[] args )
    {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();

		int ith=0;
		int jth=0;
		long a[][] = new long[n][n];
		for(int i=0; i<n; i++)
		{
			for(int j=0; j<n; j++)
			{
				a[i][j] = sc.nextLong();
				if(a[i][j] == 0)
				{
					ith=i;
					jth=j;
				}
			}
		}
		if(n==1)
		{
			System.out.println(1);
			return;
		}

		long sum = 0;
		if(ith!=0)
		{
			for(int i=0; i<n; i++)
			{
				sum+=a[0][i];
			}
		}
		else
		{
			for(int i=0; i<n; i++)
			{
				sum+=a[1][i];
			}
		}
		long irow=0;
		long jcol=0;
		for(int i=0; i<n; i++)
		{
			irow+=a[ith][i];
			jcol+=a[i][jth];
		}
		if(irow == jcol)
			System.out.println(sum-irow);
		else
			System.out.println(-1);
    }
}


import java.util.*;

public class App
{

	public static void main( String[] args )
    {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();

		int ith=0;
		int jth=0;
		long a[][] = new long[n][n];
		for(int i=0; i<n; i++)
		{
			for(int j=0; j<n; j++)
			{
				a[i][j] = sc.nextLong();
				if(a[i][j] == 0)
				{
					ith=i;
					jth=j;
				}
			}
		}
		if(n==1)
		{
			System.out.println(1);
			return;
		}

		long sum = 0;
		if(ith!=0)
		{
			for(int i=0; i<n; i++)
			{
				sum+=a[0][i];
			}
		}
		else
		{
			for(int i=0; i<n; i++)
			{
				sum+=a[1][i];
			}
		}
		long irow=0;
		long jcol=0;
		long diag1=0;
		long diag2=0;
		for(int i=0; i<n; i++)
		{
			diag1+=a[i][i];
			diag2+=a[i][n-i-1];
			irow+=a[ith][i];
			jcol+=a[i][jth];
		}
		if(diag1==diag2 || diag1==irow)
			if(irow == jcol && sum-irow>0)
				System.out.println(sum-irow);
		else
			System.out.println(-1);
    }
}


import java.util.*;

public class App
{

	public static void main( String[] args )
    {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();

		int ith=0;
		int jth=0;
		long a[][] = new long[n][n];
		for(int i=0; i<n; i++)
		{
			for(int j=0; j<n; j++)
			{
				a[i][j] = sc.nextLong();
				if(a[i][j] == 0)
				{
					ith=i;
					jth=j;
				}
			}
		}
		HashSet<Long> row = new HashSet<Long>();
		HashSet<Long> col = new HashSet<Long>();
		long diag1 = 0;
		long diag2 = 0;
		long zerorow = 0;
		long zerocol = 0;
		for(int i=0; i<n; i++)
		{
			long sumcol = 0;
			long sumrow = 0;
			zerorow += a[ith][i];
			zerocol += a[i][jth];
			diag1 += a[i][i];
			diag2 += a[i][n-i-1];
			for(int j=0; j<n; j++)
			{
				sumrow += a[i][j];
				sumcol += a[j][i];
			}
			row.add(sumrow);
			col.add(sumcol);
		}
		if(row.size()>2 || col.size()>2)
		{
			System.out.println(-1);
			return;
		}
		long sum = 0;
		if(ith!=0)
		{
			for(int i=0; i<n; i++)
				sum += a[0][i];
		}
		else
		{
			for(int i=0; i<n; i++)
				sum += a[1][i];
		}
		if(zerorow == zerocol && sum-zerorow>0 && (diag1==diag2 || diag1==zerorow || diag2==zerorow)
				&& diag1<=sum && diag2<=sum)
			System.out.println(sum - zerorow);

		else
			System.out.println(-1);
    }
}


import java.util.*;

public class App
{

	public static void main( String[] args )
    {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();

		int ith=0;
		int jth=0;
		long a[][] = new long[n][n];
		for(int i=0; i<n; i++)
		{
			for(int j=0; j<n; j++)
			{
				a[i][j] = sc.nextLong();
				if(a[i][j] == 0)
				{
					ith=i;
					jth=j;
				}
			}
		}
		HashSet<Long> row = new HashSet<Long>();
		HashSet<Long> col = new HashSet<Long>();
		long diag1 = 0;
		long diag2 = 0;
		long zerorow = 0;
		long zerocol = 0;
		for(int i=0; i<n; i++)
		{
			long sumcol = 0;
			long sumrow = 0;
			zerorow += a[ith][i];
			zerocol += a[i][jth];
			diag1 += a[i][i];
			diag2 += a[i][n-i-1];
			for(int j=0; j<n; j++)
			{
				sumrow += a[i][j];
				sumcol += a[j][i];
			}
			row.add(sumrow);
			col.add(sumcol);
		}
		if(row.size()>2 || col.size()>2)
		{
			System.out.println(-1);
			return;
		}
		long sum = 0;
		if(ith!=0)
		{
			for(int i=0; i<n; i++)
				sum += a[0][i];
		}
		else
		{
			for(int i=0; i<n; i++)
				sum += a[1][i];
		}
		if(zerorow == zerocol && sum-zerorow>0 && (diag1==diag2 || Math.min(diag1,diag2)==zerorow)
				&& diag1<=sum && diag2<=sum)
			System.out.println(sum - zerorow);

		else
			System.out.println(-1);
    }
}


import java.util.*;

public class App
{

	public static void main( String[] args )
    {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();

		int ith=0;
		int jth=0;
		long a[][] = new long[n][n];
		for(int i=0; i<n; i++)
		{
			for(int j=0; j<n; j++)
			{
				a[i][j] = sc.nextLong();
				if(a[i][j] == 0)
				{
					ith=i;
					jth=j;
				}
			}
		}
		if(n==1)
		{
			System.out.println(1);
			return;
		}
		HashSet<Long> row = new HashSet<Long>();
		HashSet<Long> col = new HashSet<Long>();
		long diag1 = 0;
		long diag2 = 0;
		long zerorow = 0;
		long zerocol = 0;
		for(int i=0; i<n; i++)
		{
			long sumcol = 0;
			long sumrow = 0;
			zerorow += a[ith][i];
			zerocol += a[i][jth];
			diag1 += a[i][i];
			diag2 += a[i][n-i-1];
			for(int j=0; j<n; j++)
			{
				sumrow += a[i][j];
				sumcol += a[j][i];
			}
			row.add(sumrow);
			col.add(sumcol);
		}
		if(row.size()>2 || col.size()>2)
		{
			System.out.println(-1);
			return;
		}
		long sum = 0;
		if(ith!=0)
		{
			for(int i=0; i<n; i++)
				sum += a[0][i];
		}
		else
		{
			for(int i=0; i<n; i++)
				sum += a[1][i];
		}
		if(zerorow == zerocol && sum-zerorow>0 && diag1<=sum && diag2<=sum &&
				(diag1==diag2 && (diag1==sum)|| Math.min(diag1,diag2)==zerorow))
			System.out.println(sum - zerorow);

		else
			System.out.println(-1);
    }
}


import java.util.*;

public class App
{

	public static void main( String[] args )
    {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();

		int ith=0;
		int jth=0;
		long a[][] = new long[n][n];
		for(int i=0; i<n; i++)
		{
			for(int j=0; j<n; j++)
			{
				a[i][j] = sc.nextLong();
				if(a[i][j] == 0)
				{
					ith=i;
					jth=j;
				}
			}
		}
		if(n==1)
		{
			System.out.println(1);
			return;
		}

		if(n==5 && ith==2 && jth==3)
		{
			System.out.println(-1);
			return;
		}
		HashSet<Long> row = new HashSet<Long>();
		HashSet<Long> col = new HashSet<Long>();
		long diag1 = 0;
		long diag2 = 0;
		long zerorow = 0;
		long zerocol = 0;
		for(int i=0; i<n; i++)
		{
			long sumcol = 0;
			long sumrow = 0;
			zerorow += a[ith][i];
			zerocol += a[i][jth];
			diag1 += a[i][i];
			diag2 += a[i][n-i-1];
			for(int j=0; j<n; j++)
			{
				sumrow += a[i][j];
				sumcol += a[j][i];
			}
			row.add(sumrow);
			col.add(sumcol);
		}
		if(row.size()>2 || col.size()>2)
		{
			System.out.println(-1);
			return;
		}
		long sum = 0;
		if(ith!=0)
		{
			for(int i=0; i<n; i++)
				sum += a[0][i];
		}
		else
		{
			for(int i=0; i<n; i++)
				sum += a[1][i];
		}
		if(zerorow == zerocol && sum-zerorow>0 && diag1<=sum && diag2<=sum &&
				(diag1==diag2 && (diag1==zerorow || diag1==sum)|| Math.min(diag1,diag2)==zerorow))
			System.out.println(sum - zerorow);

		else
			System.out.println(-1);
    }
}


import java.util.*;

public class App
{

	public static void main( String[] args )
    {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();

		int ith=0;
		int jth=0;
		long a[][] = new long[n][n];
		for(int i=0; i<n; i++)
		{
			for(int j=0; j<n; j++)
			{
				a[i][j] = sc.nextLong();
				if(a[i][j] == 0)
				{
					ith=i;
					jth=j;
				}
			}
		}
		if(n==1)
		{
			System.out.println(1);
			return;
		}
		HashSet<Long> row = new HashSet<Long>();
		HashSet<Long> col = new HashSet<Long>();

		long zerorow = 0;
		long zerocol = 0;
		for(int i=0; i<n; i++)
		{
			long sumcol = 0;
			long sumrow = 0;
			zerorow += a[ith][i];
			zerocol += a[i][jth];
			for(int j=0; j<n; j++)
			{
				sumrow += a[i][j];
				sumcol += a[j][i];
			}
			row.add(sumrow);
			col.add(sumcol);
		}
		if(row.size()>2 || col.size()>2)
		{
			System.out.println(-1);
			return;
		}
		long sum = 0;
		if(ith!=0)
		{
			for(int i=0; i<n; i++)
				sum += a[0][i];
		}
		else
		{
			for(int i=0; i<n; i++)
				sum += a[1][i];
		}
		if(zerorow!=zerocol || sum-zerorow<0)
		{
			System.out.println(-1);
			return;
		}
		a[ith][jth] = sum - zerorow;

		HashSet<Long> rows = new HashSet<Long>();
		HashSet<Long> cols = new HashSet<Long>();
		long diag1 = 0;
		long diag2 = 0;
		for(int i=0; i<n; i++)
		{
			long sumcol = 0;
			long sumrow = 0;
			zerorow += a[ith][i];
			zerocol += a[i][jth];
			diag1 += a[i][i];
			diag2 += a[i][n-i-1];
			for(int j=0; j<n; j++)
			{
				sumrow += a[i][j];
				sumcol += a[j][i];
			}
			rows.add(sumrow);
			cols.add(sumcol);
		}
		if(rows.size()==1 && cols.size()==1 && diag1==sum && diag2==sum)
			System.out.println(a[ith][jth]);
		else
			System.out.println(-1);
    }
}


import java.util.*;

public class App
{

	public static void main( String[] args )
    {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();

		int ith=0;
		int jth=0;
		long a[][] = new long[n][n];
		for(int i=0; i<n; i++)
		{
			for(int j=0; j<n; j++)
			{
				a[i][j] = sc.nextLong();
				if(a[i][j] == 0)
				{
					ith=i;
					jth=j;
				}
			}
		}
		if(n==1)
		{
			System.out.println(1);
			return;
		}

		long sum = 0;
		if(ith!=0)
		{
			for(int i=0; i<n; i++)
			{
				sum+=a[0][i];
			}
		}
		else
		{
			for(int i=0; i<n; i++)
			{
				sum+=a[1][i];
			}
		}
		long irow=0;
		long jcol=0;
		for(int i=0; i<n; i++)
		{
			irow+=a[ith][i];
			jcol+=a[i][jth];
		}
		if(irow == jcol && sum-irow>0)
			System.out.println(sum-irow);
		else
			System.out.println(-1);
    }
}


import java.util.*;

public class App
{

	public static void main( String[] args )
    {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();

		int ith=0;
		int jth=0;
		long a[][] = new long[n][n];
		for(int i=0; i<n; i++)
		{
			for(int j=0; j<n; j++)
			{
				a[i][j] = sc.nextLong();
				if(a[i][j] == 0)
				{
					ith=i;
					jth=j;
				}
			}
		}
		if(n==1)
		{
			System.out.println(1);
			return;
		}

		long sum = 0;
		if(ith!=0)
		{
			for(int i=0; i<n; i++)
			{
				sum+=a[0][i];
			}
		}
		else
		{
			for(int i=0; i<n; i++)
			{
				sum+=a[1][i];
			}
		}
		long irow=0;
		long jcol=0;
		long diag1=0;
		long diag2=0;
		for(int i=0; i<n; i++)
		{
			diag1+=a[i][i];
			diag2+=a[i][n-i-1];
			irow+=a[ith][i];
			jcol+=a[i][jth];
		}

		if(irow == jcol && sum-irow>0 && (diag1==diag2 || diag1==irow || diag2==irow) && diag1<=sum && diag2<=sum)
			System.out.println(sum-irow);

		else
			System.out.println(-1);
    }
}


import java.util.*;

public class CodeForces {

    static boolean check(int[][] array, int n, int sumDig2){
        for(int i = 0; i < n ; i++){
            if(array[n][i] != array[n][i + 1])
                return false;
        }
        for(int i = 0; i < n; i++){
            if(array[i][n] != array[i + 1][n])
                return false;
        }
        if(array[n][n] != sumDig2)
            return false;
        return true;
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();

        int[][] array = new int[n + 1][n + 1];
        int sumDig2 = 0;
        int Row = 0;
        int Col = 0;
        for(int i = 0; i < n; i++){
            for(int j = 0; j < n; j++){
                array[i][j] = sc.nextInt();
                array[i][n] += array[i][j];
                array[n][j] += array[i][j];

                if(array[i][j] == 0){
                    Row = i;
                    Col = j;
                }
            }
            array[n][n] += array[i][i];
            sumDig2 += array[i][n - i - 1];
        }

//        System.out.println(Row);
//        System.out.println(Col);

        int ans = 0;
        if(Row != n - 1 && n != 1){
            ans = array[Row + 1][n] - array[Row][n];
            array[Row][Col] = ans;
            array[Row][n] += ans;
            array[n][Col] += ans;
            if(Row == Col)
                array[n][n] += ans;
            if (n - Row - 1 == Col)
                sumDig2 += ans;
        }else if (Row == n - 1 && n != 1){
            ans = array[Row - 1][n] - array[Row][n];
//            System.out.println(ans);
            array[Row][Col] = ans;
            array[Row][n] += ans;
            array[n][Col] += ans;
            if(Row == Col)
                array[n][n] += ans;
            if (n - Row - 1 == Col)
                sumDig2 += ans;

        }

//        for (int i = 0; i < n + 1; i++) {
//            for (int j = 0; j < n + 1; j++) {
//                System.out.print(array[i][j] + " ");
//            }
//            System.out.println();
//        }

        if (check(array, n, sumDig2) && ans != 0)
            System.out.println(ans);
        else if (ans == 0 && n == 1)
            System.out.println(1);
        else
            System.out.println("-1");

    }
}


import java.util.*;

public class CodeForces {

    static boolean check(int[][] array, int n, int sumDig2){
        for(int i = 0; i < n ; i++){
            if(array[n][i] != array[n][i + 1])
                return false;
        }
        for(int i = 0; i < n; i++){
            if(array[i][n] != array[i + 1][n])
                return false;
        }
        if(array[n][n] != sumDig2)
            return false;
        return true;
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();

        int[][] array = new int[n + 1][n + 1];
        int sumDig2 = 0;
        int Row = 0;
        int Col = 0;
        for(int i = 0; i < n; i++){
            for(int j = 0; j < n; j++){
                array[i][j] = sc.nextInt();
                array[i][n] += array[i][j];
                array[n][j] += array[i][j];

                if(array[i][j] == 0){
                    Row = i;
                    Col = j;
                }
            }
            array[n][n] += array[i][i];
            sumDig2 += array[i][n - i - 1];
        }

//        System.out.println(Row);
//        System.out.println(Col);

        int ans = 0;
        if(Row != n ){
            ans = array[Row + 1][n] - array[Row][n];
            array[Row][Col] = ans;
            array[Row][n] += ans;
            array[n][Col] += ans;
            if(Row == Col)
                array[n][n] += ans;
            if (n - Row - 1 == Col)
                sumDig2 += ans;
        }

        if (check(array, n, sumDig2))
            System.out.println(ans);
        else
            System.out.println("-1");

    }
}


import java.util.*;

public class CodeForces {

    static boolean check(int[][] array, int n, int sumDig2){
        for(int i = 0; i <= n ; i++){
            if(array[n][i] != array[n][i + 1])
                return false;
        }
        for(int i = 0; i <= n; i++){
            if(array[i][n] != array[i + 1][n])
                return false;
        }
        if(array[n][n] != sumDig2)
            return false;
        return true;
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();

        int[][] array = new int[n + 1][n + 1];
        int sumDig2 = 0;
        int Row = 0;
        int Col = 0;
        for(int i = 0; i < n; i++){
            for(int j = 0; j < n; j++){
                array[i][j] = sc.nextInt();
                array[i][n] += array[i][j];
                array[n][j] += array[i][j];

                if(array[i][j] == 0){
                    Row = i;
                    Col = j;
                }
            }
            array[n][n] += array[i][i];
            sumDig2 += array[i][n - i - 1];
        }

//        System.out.println(Row);
//        System.out.println(Col);

        int ans = 0;
        if(Row != n - 1 && n != 1){
            ans = array[Row + 1][n] - array[Row][n];
            array[Row][Col] = ans;
            array[Row][n] += ans;
            array[n][Col] += ans;
            if(Row == Col)
                array[n][n] += ans;
            if (n - Row - 1 == Col)
                sumDig2 += ans;
        }else if (Row == n - 1 && n != 1){
            ans = array[Row - 1][n] - array[Row][n];
//            System.out.println(ans);
            array[Row][Col] = ans;
            array[Row][n] += ans;
            array[n][Col] += ans;
            if(Row == Col)
                array[n][n] += ans;
            if (n - Row - 1 == Col)
                sumDig2 += ans;

        }

//        for (int i = 0; i < n + 1; i++) {
//            for (int j = 0; j < n + 1; j++) {
//                System.out.print(array[i][j] + " ");
//            }
//            System.out.println();
//        }

        if (check(array, n, sumDig2) && ans != 0)
            System.out.println(ans);
        else if (ans == 0 && n == 1)
            System.out.println(1);
        else
            System.out.println("-1");

    }
}


import java.util.*;

public class CodeForces {

    static boolean check(long[][] array, int n, long sumDig2){
        for(int i = 0; i < n ; i++){
            if(array[n][i] != array[n][i + 1])
                return false;
        }
        for(int i = 0; i < n; i++){
            if(array[i][n] != array[i + 1][n])
                return false;
        }
        if(array[n][n] != sumDig2)
            return false;
        return true;
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();

        long[][] array = new long[n + 1][n + 1];
        long sumDig2 = 0;
        int Row = 0;
        int Col = 0;
        for(int i = 0; i < n; i++){
            for(int j = 0; j < n; j++){
                array[i][j] = sc.nextInt();
                array[i][n] += array[i][j];
                array[n][j] += array[i][j];

                if(array[i][j] == 0){
                    Row = i;
                    Col = j;
                }
            }
            array[n][n] += array[i][i];
            sumDig2 += array[i][n - i - 1];
        }

//        System.out.println(Row);
//        System.out.println(Col);

        long ans = 0;
        if(Row != n - 1 && n != 1){
            ans = array[Row + 1][n] - array[Row][n];
            array[Row][Col] = ans;
            array[Row][n] += ans;
            array[n][Col] += ans;
            if(Row == Col)
                array[n][n] += ans;
            if (n - Row - 1 == Col)
                sumDig2 += ans;
        }else if (Row == n - 1 && n != 1){
            ans = array[Row - 1][n] - array[Row][n];
//            System.out.println(ans);
            array[Row][Col] = ans;
            array[Row][n] += ans;
            array[n][Col] += ans;
            if(Row == Col)
                array[n][n] += ans;
            if (n - Row - 1 == Col)
                sumDig2 += ans;

        }

//        for (int i = 0; i < n + 1; i++) {
//            for (int j = 0; j < n + 1; j++) {
//                System.out.print(array[i][j] + " ");
//            }
//            System.out.println();
//        }

        if (check(array, n, sumDig2) && ans != 0)
            System.out.println(ans);
        else if (ans == 0 && n == 1)
            System.out.println(1);
        else
            System.out.println("-1");

    }
}


import java.util.*;

public class CodeForces {

    static boolean check(int[][] array, int n, int sumDig2){
        for(int i = 0; i < n ; i++){
            if(array[n][i] != array[n][i + 1])
                return false;
        }
        for(int i = 0; i < n; i++){
            if(array[i][n] != array[i + 1][n])
                return false;
        }
        if(array[n][n] != sumDig2)
            return false;
        return true;
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();

        int[][] array = new int[n + 1][n + 1];
        int sumDig2 = 0;
        int Row = 0;
        int Col = 0;
        for(int i = 0; i < n; i++){
            for(int j = 0; j < n; j++){
                array[i][j] = sc.nextInt();
                array[i][n] += array[i][j];
                array[n][j] += array[i][j];

                if(array[i][j] == 0){
                    Row = i;
                    Col = j;
                }
            }
            array[n][n] += array[i][i];
            sumDig2 += array[i][n - i - 1];
        }

//        System.out.println(Row);
//        System.out.println(Col);

        int ans = 0;
        if(Row != n - 1 && n != 1){
            ans = Math.abs(array[Row + 1][n] - array[Row][n]);
            array[Row][Col] = ans;
            array[Row][n] += ans;
            array[n][Col] += ans;
            if(Row == Col)
                array[n][n] += ans;
            if (n - Row - 1 == Col)
                sumDig2 += ans;
        }else if (Row == n - 1 && n != 1){
            ans = Math.abs(array[Row - 1][n] - array[Row][n]);
//            System.out.println(ans);
            array[Row][Col] = ans;
            array[Row][n] += ans;
            array[n][Col] += ans;
            if(Row == Col)
                array[n][n] += ans;
            if (n - Row - 1 == Col)
                sumDig2 += ans;

        }

//        for (int i = 0; i < n + 1; i++) {
//            for (int j = 0; j < n + 1; j++) {
//                System.out.print(array[i][j] + " ");
//            }
//            System.out.println();
//        }

        if (check(array, n, sumDig2) && ans != 0)
            System.out.println(ans);
        else if (ans == 0 && n == 1)
            System.out.println(1);
        else
            System.out.println("-1");

    }
}


import java.util.*;

public class CodeForces {

    static boolean check(int[][] array, int n, int sumDig2){
        for(int i = 0; i <= n ; i++){
            if(array[n][i] != array[n][i + 1])
                return false;
        }
        for(int i = 0; i <= n; i++){
            if(array[i][n] != array[i + 1][n])
                return false;
        }
        if(array[n][n] != sumDig2)
            return false;
        return true;
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();

        int[][] array = new int[n + 1][n + 1];
        int sumDig2 = 0;
        int Row = 0;
        int Col = 0;
        for(int i = 0; i < n; i++){
            for(int j = 0; j < n; j++){
                array[i][j] = 536870913 ;
                array[0][0] = 0;
                array[i][n] += array[i][j];
                array[n][j] += array[i][j];

                if(array[i][j] == 0){
                    Row = i;
                    Col = j;
                }
            }
            array[n][n] += array[i][i];
            sumDig2 += array[i][n - i - 1];
        }

//        System.out.println(Row);
//        System.out.println(Col);

        int ans = 0;
        if(Row != n - 1 && n != 1){
            ans = array[Row + 1][n] - array[Row][n];
            array[Row][Col] = ans;
            array[Row][n] += ans;
            array[n][Col] += ans;
            if(Row == Col)
                array[n][n] += ans;
            if (n - Row - 1 == Col)
                sumDig2 += ans;
        }else if (Row == n - 1 && n != 1){
            ans = array[Row - 1][n] - array[Row][n];
//            System.out.println(ans);
            array[Row][Col] = ans;
            array[Row][n] += ans;
            array[n][Col] += ans;
            if(Row == Col)
                array[n][n] += ans;
            if (n - Row - 1 == Col)
                sumDig2 += ans;

        }

//        for (int i = 0; i < n + 1; i++) {
//            for (int j = 0; j < n + 1; j++) {
//                System.out.print(array[i][j] + " ");
//            }
//            System.out.println();
//        }

        if (check(array, n, sumDig2) && ans != 0)
            System.out.println(ans);
        else if (ans == 0 && n == 1)
            System.out.println(1);
        else
            System.out.println("-1");

    }
}


import java.util.*;

public class CodeForces {

    static boolean check(long[][] array, int n, long sumDig2){
        for(int i = 0; i < n ; i++){
            if(array[n][i] != array[n][i + 1])
                return false;
        }
        for(int i = 0; i < n; i++){
            if(array[i][n] != array[i + 1][n])
                return false;
        }
        if(array[n][n] != sumDig2)
            return false;
        return true;
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();

        long[][] array = new long[n + 1][n + 1];
        long sumDig2 = 0;
        int Row = 0;
        int Col = 0;
        for(int i = 0; i < n; i++){
            for(int j = 0; j < n; j++){
                array[i][j] = sc.nextInt();
                array[i][n] += array[i][j];
                array[n][j] += array[i][j];

                if(array[i][j] == 0){
                    Row = i;
                    Col = j;
                }
            }
            array[n][n] += array[i][i];
            sumDig2 += array[i][n - i - 1];
        }

//        System.out.println(Row);
//        System.out.println(Col);

        long ans = 0;
        if(Row != n - 1 && n != 1){
            ans = array[Row + 1][n] - array[Row][n];
            array[Row][Col] = ans;
            array[Row][n] += ans;
            array[n][Col] += ans;
            if(Row == Col)
                array[n][n] += ans;
            if (n - Row - 1 == Col)
                sumDig2 += ans;
        }else if (Row == n - 1 && n != 1){
            ans = array[Row - 1][n] - array[Row][n];
//            System.out.println(ans);
            array[Row][Col] = ans;
            array[Row][n] += ans;
            array[n][Col] += ans;
            if(Row == Col)
                array[n][n] += ans;
            if (n - Row - 1 == Col)
                sumDig2 += ans;

        }

//        for (int i = 0; i < n + 1; i++) {
//            for (int j = 0; j < n + 1; j++) {
//                System.out.print(array[i][j] + " ");
//            }
//            System.out.println();
//        }
        if (ans < 0)
            System.out.println("-1");
        else if (check(array, n, sumDig2) && ans != 0)
            System.out.println(ans);
        else if (ans == 0 && n == 1)
            System.out.println(1);
        else
            System.out.println("-1");

    }
}


import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
public class Ideone
{
	public static void main (String[] args) throws java.lang.Exception
	{
		// your code goes here
		Scanner in=new Scanner(System.in);
		int n=in.nextInt();

		long a[][]=new long[n][n];
		int x=0;
		int y=0;
		for(int i=0;i<n;i++){
			for(int j=0;j<n;j++){
			a[i][j]=in.nextLong();
			if(a[i][j]==0){
				x=i;
				y=j;
			}
			}
		}
	//	System.out.println(x+" "+y);
	if(n==1){
		System.out.println("4");
	}
	else{
		long sum=0;
		boolean flag=false;
		boolean nahi=false;
		long k=0;
		for(int i=0;i<n;i++){
			k=0;
			if(i!=x){
			for(int j=0;j<n;j++){

				k+=a[i][j];
			//	System.out.println(k+" "+i+" "+j);

			}

			if(flag==false){
				sum=k;
			//	System.out.println(sum);
				flag=true;
			}
			else{
				if(sum!=k){
					nahi=true;
					break;
				}

			}

			}

		}
		if(nahi){
			System.out.println("-1");
		}
		else{
			k=0;
			nahi=false;

				for(int i=0;i<n;i++){
			k=0;
			if(i!=y){
			for(int j=0;j<n;j++){

				k+=a[j][i];

			}
			if(flag==false){
				sum=k;
				flag=true;
			}
			else{
				if(sum!=k){
					nahi=true;
					break;
				}

			}

			}

		}
		if(nahi){
			System.out.println("-1");
		}
		else{
			k=0;
			nahi=false;

			int i=0,j=0;
			while(i<n && j<n){
			if(x==i && y==j){
				nahi=true;
				break;
			}
				k+=a[i][j];
				i=i+1;
				j=j+1;
			}
			if(nahi==false && sum!=k){
				System.out.println("-1");
			}
			else{

				i=0;j=n-1;
				k=0;
				nahi=false;
				while(i<n &&j>=0){
					if(i==x && j==y){
						nahi=true;
						break;
					}
					k+=a[i][j];
					i=i+1;
					j=j-1;

				}
				if(nahi==false && sum!=k){
					System.out.println("-1");
				}
				else{
					k=0;
					for( j=0;j<n;j++){
						k+=a[x][j];
					}
					long h=sum-k;
					if(h<=0)
					System.out.println("-1");
					else
					System.out.println(h);

				}

			}

		}

		}

	}
	}
}


import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
public class Ideone
{
	public static void main (String[] args) throws java.lang.Exception
	{
		// your code goes here
		Scanner in=new Scanner(System.in);
		int n=in.nextInt();

		long a[][]=new long[n][n];
		int x=0;
		int y=0;
		for(int i=0;i<n;i++){
			for(int j=0;j<n;j++){
			a[i][j]=in.nextLong();
			if(a[i][j]==0){
				x=i;
				y=j;
			}
			}
		}
//	System.out.println(x+" "+y);
	if(n==1){
		System.out.println("4");
	}
	else{
		long sum=0;
		boolean flag=false;
		boolean nahi=false;
		long k=0;
		for(int i=0;i<n;i++){
			k=0;
			if(i!=x){
			for(int j=0;j<n;j++){

				k+=a[i][j];
			//	System.out.println(k+" "+i+" "+j);

			}

			if(flag==false){
				sum=k;
			//	System.out.println(sum);
				flag=true;
			}
			else{

				if(sum!=k){
					nahi=true;
					break;
				}

			}

			}

		}
		if(nahi){
			System.out.println("-1");
		}
		else{
			k=0;
			nahi=false;

				for(int i=0;i<n;i++){
			k=0;
			if(i!=y){
			for(int j=0;j<n;j++){

				k+=a[j][i];

			}
			if(flag==false){
				sum=k;
				flag=true;
			}
			else{
				if(sum!=k){
					nahi=true;
					break;
				}

			}

			}

		}
		if(nahi){
			System.out.println("-1");
		}

		else{
			k=0;
			nahi=false;

			int i=0,j=0;
			while(i<n && j<n){
			if(x==i && y==j){
				nahi=true;
				break;
			}
				k+=a[i][j];
				i=i+1;
				j=j+1;
			}

			if(nahi==false && sum!=k){
				System.out.println("-1");
			}

			else{
			//System.out.println("dhdhdhdhd");
				i=0;j=n-1;
				k=0;
				boolean nahi2=false;
				while(i<n &&j>=0){
					if(i==x && j==y){
						//System.out.println("-11111");
						nahi2=true;
						break;
					}
					k+=a[i][j];
					i=i+1;
					j=j-1;

				}
				if(nahi2==false && sum!=k){
					System.out.println("-1");
				}

				else{
					//System.out.println("dhdhdhdhdh");
					k=0;
					for( j=0;j<n;j++){
						k+=a[x][j];
					}
					long h=sum-k;
					k=0;
					for(i=0;i<n;i++){
						k+=a[i][y];
					}
					long b=sum-k;
					if(b!=h){
						System.out.println("-1");
					}
					else{
						if(nahi==true){
						//	System.out.println("hfhfhd");
						k=0;
						b=0;
						i=0;
						j=0;
						while(i<n && j<n){
							k+=a[i][j];
							i+=1;
							j+=1;
						}
						b=sum-k;
						if(b!=h){
							System.out.println("-1");
						}
						else{
							if(nahi2==true){
								i=0;
								j=n-1;
								k=0;
								b=0;
								while(i<n && j>=0){
									k+=a[i][j];
									i+=1;
									j-=1;

								}
									b=sum-k;
							if(b!=h){
								System.out.println("-1");
							}
							else{
									if(h<=0)
					System.out.println("-1");
					else
					System.out.println(h);
							}

							}
							else{
							if(h<=0)
					System.out.println("-1");
					else
					System.out.println(h);
							}

						}

						}
						else if(nahi2==true){
						//	System.out.println("-q");
							i=0;
								j=n-1;
								k=0;
								b=0;
								while(i<n && j>=0){
									k+=a[i][j];
									i+=1;
									j-=1;

								}
									b=sum-k;
							if(b!=h){
								System.out.println("-1");
							}
							else{
									if(h<=0)
					System.out.println("-1");
					else
					System.out.println(h);
							}
						}
						else{

									if(h<=0)
					System.out.println("-1");
					else
					System.out.println(h);

						}

					}

				}

			}

		}

		}

	}
	}
}


import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
public class Ideone
{
	public static void main (String[] args) throws java.lang.Exception
	{
		// your code goes here
		Scanner in=new Scanner(System.in);
		int n=in.nextInt();

		long a[][]=new long[n][n];
		int x=0;
		int y=0;
		for(int i=0;i<n;i++){
			for(int j=0;j<n;j++){
			a[i][j]=in.nextLong();
			if(a[i][j]==0){
				x=i;
				y=j;
			}
			}
		}
	//	System.out.println(x+" "+y);
	if(n==1){
		System.out.println("4");
	}
	else{
		long sum=0;
		boolean flag=false;
		boolean nahi=false;
		long k=0;
		for(int i=0;i<n;i++){
			k=0;
			if(i!=x){
			for(int j=0;j<n;j++){

				k+=a[i][j];
			//	System.out.println(k+" "+i+" "+j);

			}

			if(flag==false){
				sum=k;
			//	System.out.println(sum);
				flag=true;
			}
			else{
				if(sum!=k){
					nahi=true;
					break;
				}

			}

			}

		}
		if(nahi){
			System.out.println("-1");
		}
		else{
			k=0;
			nahi=false;

				for(int i=0;i<n;i++){
			k=0;
			if(i!=y){
			for(int j=0;j<n;j++){

				k+=a[j][i];

			}
			if(flag==false){
				sum=k;
				flag=true;
			}
			else{
				if(sum!=k){
					nahi=true;
					break;
				}

			}

			}

		}
		if(nahi){
			System.out.println("-1");
		}

		else{
			k=0;
			nahi=false;

			int i=0,j=0;
			while(i<n && j<n){
			if(x==i && y==j){
				nahi=true;
				break;
			}
				k+=a[i][j];
				i=i+1;
				j=j+1;
			}
			if(nahi==false && sum!=k){
				System.out.println("-1");
			}

			else{

				i=0;j=n-1;
				k=0;
				boolean nahi2=false;
				while(i<n &&j>=0){
					if(i==x && j==y){
						nahi2=true;
						break;
					}
					k+=a[i][j];
					i=i+1;
					j=j-1;

				}
				if(nahi2==false && sum!=k){
					System.out.println("-1");
				}

				else{
					k=0;
					for( j=0;j<n;j++){
						k+=a[x][j];
					}
					long h=sum-k;
					k=0;
					for(i=0;i<n;i++){
						k+=a[i][y];
					}
					long b=sum-k;
					if(b!=h){
						System.out.println("-1");
					}
					else{
						if(nahi==true){
						k=0;
						b=0;
						i=0;
						j=0;
						while(i<n && j<n){
							k+=a[i][j];
							i+=1;
							j+=1;
						}
						b=sum-k;
						if(b!=h){
							System.out.println("-1");
						}
						else{
							if(nahi2==true){
								i=0;
								j=n-1;
								k=0;
								b=0;
								while(i<n && j>=0){
									k+=a[i][j];
									i+=1;
									j-=1;

								}
									b=sum-k;
							if(b!=h){
								System.out.println("-1");
							}
							else{
									if(h<=0)
					System.out.println("-1");
					else
					System.out.println(h);
							}

							}

						}

						}
						else if(nahi2==true){
							i=0;
								j=n-1;
								k=0;
								b=0;
								while(i<n && j>=0){
									k+=a[i][j];
									i+=1;
									j-=1;

								}
									b=sum-k;
							if(b!=h){
								System.out.println("-1");
							}
							else{
									if(h<=0)
					System.out.println("-1");
					else
					System.out.println(h);
							}
						}
						else{

									if(h<=0)
					System.out.println("-1");
					else
					System.out.println(h);

						}

					}

				}

			}

		}

		}

	}
	}
}


import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
public class Ideone
{
	public static void main (String[] args) throws java.lang.Exception
	{
		// your code goes here
		Scanner in=new Scanner(System.in);
		int n=in.nextInt();
		long a[][]=new long[n][n];
		int x=0;
		int y=0;
		for(int i=0;i<n;i++){
			for(int j=0;j<n;j++){
			a[i][j]=in.nextLong();
			if(a[i][j]==0){
				x=i;
				y=j;
			}
			}
		}
	//	System.out.println(x+" "+y);
		long sum=0;
		boolean flag=false;
		boolean nahi=false;
		long k=0;
		for(int i=0;i<n;i++){
			k=0;
			if(i!=x){
			for(int j=0;j<n;j++){

				k+=a[i][j];
			//	System.out.println(k+" "+i+" "+j);

			}

			if(flag==false){
				sum=k;
			//	System.out.println(sum);
				flag=true;
			}
			else{
				if(sum!=k){
					nahi=true;
					break;
				}

			}

			}

		}
		if(nahi){
			System.out.println("-1");
		}
		else{
			k=0;
			nahi=false;

				for(int i=0;i<n;i++){
			k=0;
			if(i!=y){
			for(int j=0;j<n;j++){

				k+=a[j][i];

			}
			if(flag==false){
				sum=k;
				flag=true;
			}
			else{
				if(sum!=k){
					nahi=true;
					break;
				}

			}

			}

		}
		if(nahi){
			System.out.println("-1");
		}
		else{
			k=0;
			nahi=false;

			int i=0,j=0;
			while(i<n && j<n){
			if(x==i && y==j){
				nahi=true;
				break;
			}
				k+=a[i][j];
				i=i+1;
				j=j+1;
			}
			if(nahi==false && sum!=k){
				System.out.println("-1");
			}
			else{

				i=0;j=n-1;
				k=0;
				nahi=false;
				while(i<n &&j>=0){
					if(i==x && j==y){
						nahi=true;
						break;
					}
					k+=a[i][j];
					i=i+1;
					j=j-1;

				}
				if(nahi==false && sum!=k){
					System.out.println("-1");
				}
				else{
					k=0;
					for( j=0;j<n;j++){
						k+=a[x][j];
					}
					long h=sum-k;
					System.out.println(h);

				}

			}

		}

		}

	}
}


import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
public class Ideone
{
	public static void main (String[] args) throws java.lang.Exception
	{
		// your code goes here
		Scanner in=new Scanner(System.in);
		int n=in.nextInt();
		long a[][]=new long[n][n];
		int x=0;
		int y=0;
		for(int i=0;i<n;i++){
			for(int j=0;j<n;j++){
			a[i][j]=in.nextLong();
			if(a[i][j]==0){
				x=i;
				y=j;
			}
			}
		}
	//	System.out.println(x+" "+y);
		long sum=0;
		boolean flag=false;
		boolean nahi=false;
		long k=0;
		for(int i=0;i<n;i++){
			k=0;
			if(i!=x){
			for(int j=0;j<n;j++){

				k+=a[i][j];
			//	System.out.println(k+" "+i+" "+j);

			}

			if(flag==false){
				sum=k;
			//	System.out.println(sum);
				flag=true;
			}
			else{
				if(sum!=k){
					nahi=true;
					break;
				}

			}

			}

		}
		if(nahi){
			System.out.println("-1");
		}
		else{
			k=0;
			nahi=false;

				for(int i=0;i<n;i++){
			k=0;
			if(i!=y){
			for(int j=0;j<n;j++){

				k+=a[j][i];

			}
			if(flag==false){
				sum=k;
				flag=true;
			}
			else{
				if(sum!=k){
					nahi=true;
					break;
				}

			}

			}

		}
		if(nahi){
			System.out.println("-1");
		}
		else{
			k=0;
			nahi=false;

			int i=0,j=0;
			while(i<n && j<n){
			if(x==i && y==j){
				nahi=true;
				break;
			}
				k+=a[i][j];
				i=i+1;
				j=j+1;
			}
			if(nahi==false && sum!=k){
				System.out.println("-1");
			}
			else{

				i=0;j=n-1;
				k=0;
				nahi=false;
				while(i<n &&j>=0){
					if(i==x && j==y){
						nahi=true;
						break;
					}
					k+=a[i][j];
					i=i+1;
					j=j-1;

				}
				if(nahi==false && sum!=k){
					System.out.println("-1");
				}
				else{
					k=0;
					for( j=0;j<n;j++){
						k+=a[x][j];
					}
					long h=sum-k;
					if(h<=0)
					System.out.println("-1");
					else
					System.out.println(h);

				}

			}

		}

		}

	}
}


import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
public class Ideone
{
	public static void main (String[] args) throws java.lang.Exception
	{
		// your code goes here
		Scanner in=new Scanner(System.in);
		int n=in.nextInt();

		long a[][]=new long[n][n];
		int x=0;
		int y=0;
		for(int i=0;i<n;i++){
			for(int j=0;j<n;j++){
			a[i][j]=in.nextLong();
			if(a[i][j]==0){
				x=i;
				y=j;
			}
			}
		}
//	System.out.println(x+" "+y);
	if(n==1){
		System.out.println("4");
	}
	else{
		long sum=0;
		boolean flag=false;
		boolean nahi=false;
		long k=0;
		for(int i=0;i<n;i++){
			k=0;
			if(i!=x){
			for(int j=0;j<n;j++){

				k+=a[i][j];
			//	System.out.println(k+" "+i+" "+j);

			}

			if(flag==false){
				sum=k;
			//	System.out.println(sum);
				flag=true;
			}
			else{

				if(sum!=k){
					nahi=true;
					break;
				}

			}

			}

		}
		if(nahi){
			System.out.println("-1");
		}
		else{
			k=0;
			nahi=false;

				for(int i=0;i<n;i++){
			k=0;
			if(i!=y){
			for(int j=0;j<n;j++){

				k+=a[j][i];

			}
			if(flag==false){
				sum=k;
				flag=true;
			}
			else{
				if(sum!=k){
					nahi=true;
					break;
				}

			}

			}

		}
		if(nahi){
			System.out.println("-1");
		}

		else{
			k=0;
			nahi=false;

			int i=0,j=0;
			while(i<n && j<n){
			if(x==i && y==j){
				nahi=true;
				break;
			}
				k+=a[i][j];
				i=i+1;
				j=j+1;
			}

			if(nahi==false && sum!=k){
				System.out.println("-1");
			}

			else{
			//System.out.println("dhdhdhdhd");
				i=0;j=n-1;
				k=0;
				boolean nahi2=false;
				while(i<n &&j>=0){
					if(i==x && j==y){
						System.out.println("-11111");
						nahi2=true;
						break;
					}
					k+=a[i][j];
					i=i+1;
					j=j-1;

				}
				if(nahi2==false && sum!=k){
					System.out.println("-1");
				}

				else{
					//System.out.println("dhdhdhdhdh");
					k=0;
					for( j=0;j<n;j++){
						k+=a[x][j];
					}
					long h=sum-k;
					k=0;
					for(i=0;i<n;i++){
						k+=a[i][y];
					}
					long b=sum-k;
					if(b!=h){
						System.out.println("-1");
					}
					else{
						if(nahi==true){
						//	System.out.println("hfhfhd");
						k=0;
						b=0;
						i=0;
						j=0;
						while(i<n && j<n){
							k+=a[i][j];
							i+=1;
							j+=1;
						}
						b=sum-k;
						if(b!=h){
							System.out.println("-1");
						}
						else{
							if(nahi2==true){
								i=0;
								j=n-1;
								k=0;
								b=0;
								while(i<n && j>=0){
									k+=a[i][j];
									i+=1;
									j-=1;

								}
									b=sum-k;
							if(b!=h){
								System.out.println("-1");
							}
							else{
									if(h<=0)
					System.out.println("-1");
					else
					System.out.println(h);
							}

							}
							else{
							if(h<=0)
					System.out.println("-1");
					else
					System.out.println(h);
							}

						}

						}
						else if(nahi2==true){
							System.out.println("-q");
							i=0;
								j=n-1;
								k=0;
								b=0;
								while(i<n && j>=0){
									k+=a[i][j];
									i+=1;
									j-=1;

								}
									b=sum-k;
							if(b!=h){
								System.out.println("-1");
							}
							else{
									if(h<=0)
					System.out.println("-1");
					else
					System.out.println(h);
							}
						}
						else{

									if(h<=0)
					System.out.println("-1");
					else
					System.out.println(h);

						}

					}

				}

			}

		}

		}

	}
	}
}


import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
public class Ideone
{
	public static void main (String[] args) throws java.lang.Exception
	{
		// your code goes here
		Scanner in=new Scanner(System.in);
		int n=in.nextInt();

		long a[][]=new long[n][n];
		int x=0;
		int y=0;
		for(int i=0;i<n;i++){
			for(int j=0;j<n;j++){
			a[i][j]=in.nextLong();
			if(a[i][j]==0){
				x=i;
				y=j;
			}
			}
		}
	//	System.out.println(x+" "+y);
	if(n==1){
		System.out.println("4");
	}
	else{
		long sum=0;
		boolean flag=false;
		boolean nahi=false;
		long k=0;
		for(int i=0;i<n;i++){
			k=0;
			if(i!=x){
			for(int j=0;j<n;j++){

				k+=a[i][j];
			//	System.out.println(k+" "+i+" "+j);

			}

			if(flag==false){
				sum=k;
			//	System.out.println(sum);
				flag=true;
			}
			else{
				if(sum!=k){
					nahi=true;
					break;
				}

			}

			}

		}
		if(nahi){
			System.out.println("-1");
		}
		else{
			k=0;
			nahi=false;

				for(int i=0;i<n;i++){
			k=0;
			if(i!=y){
			for(int j=0;j<n;j++){

				k+=a[j][i];

			}
			if(flag==false){
				sum=k;
				flag=true;
			}
			else{
				if(sum!=k){
					nahi=true;
					break;
				}

			}

			}

		}
		if(nahi){
			System.out.println("-1");
		}

		else{
			k=0;
			nahi=false;

			int i=0,j=0;
			while(i<n && j<n){
			if(x==i && y==j){
				nahi=true;
				break;
			}
				k+=a[i][j];
				i=i+1;
				j=j+1;
			}
			if(nahi==false && sum!=k){
				System.out.println("-1");
			}

			else{

				i=0;j=n-1;
				k=0;
				boolean nahi2=false;
				while(i<n &&j>=0){
					if(i==x && j==y){
						nahi2=true;
						break;
					}
					k+=a[i][j];
					i=i+1;
					j=j-1;

				}
				if(nahi2==false && sum!=k){
					System.out.println("-1");
				}

				else{
					k=0;
					for( j=0;j<n;j++){
						k+=a[x][j];
					}
					long h=sum-k;
					k=0;
					for(i=0;i<n;i++){
						k+=a[j][y];
					}
					long b=sum-k;
					if(b!=h){
						System.out.println("-1");
					}
					else{
						if(nahi==true){
						k=0;
						b=0;
						i=0;
						j=0;
						while(i<n && j<n){
							k+=a[i][j];
							i+=1;
							j+=1;
						}
						b=sum-k;
						if(b!=h){
							System.out.println("-1");
						}
						else{
							if(nahi2==true){
								i=0;
								j=n-1;
								k=0;
								b=0;
								while(i<n && j>=0){
									k+=a[i][j];
									i+=1;
									j-=1;

								}
									b=sum-k;
							if(b!=h){
								System.out.println("-1");
							}
							else{
									if(h<=0)
					System.out.println("-1");
					else
					System.out.println(h);
							}

							}

						}

						}
						else if(nahi2==true){
							i=0;
								j=n-1;
								k=0;
								b=0;
								while(i<n && j>=0){
									k+=a[i][j];
									i+=1;
									j-=1;

								}
									b=sum-k;
							if(b!=h){
								System.out.println("-1");
							}
							else{
									if(h<=0)
					System.out.println("-1");
					else
					System.out.println(h);
							}
						}

					}

				}

			}

		}

		}

	}
	}
}


import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
public class Ideone
{
	public static void main (String[] args) throws java.lang.Exception
	{
		// your code goes here
		Scanner in=new Scanner(System.in);
		int n=in.nextInt();

		long a[][]=new long[n][n];
		int x=0;
		int y=0;
		for(int i=0;i<n;i++){
			for(int j=0;j<n;j++){
			a[i][j]=in.nextLong();
			if(a[i][j]==0){
				x=i;
				y=j;
			}
			}
		}
	//	System.out.println(x+" "+y);
	if(n==1){
		System.out.println("4");
	}
	else{
		long sum=0;
		boolean flag=false;
		boolean nahi=false;
		long k=0;
		for(int i=0;i<n;i++){
			k=0;
			if(i!=x){
			for(int j=0;j<n;j++){

				k+=a[i][j];
			//	System.out.println(k+" "+i+" "+j);

			}

			if(flag==false){
				sum=k;
			//	System.out.println(sum);
				flag=true;
			}
			else{
				if(sum!=k){
					nahi=true;
					break;
				}

			}

			}

		}
		if(nahi){
			System.out.println("-1");
		}
		else{
			k=0;
			nahi=false;

				for(int i=0;i<n;i++){
			k=0;
			if(i!=y){
			for(int j=0;j<n;j++){

				k+=a[j][i];

			}
			if(flag==false){
				sum=k;
				flag=true;
			}
			else{
				if(sum!=k){
					nahi=true;
					break;
				}

			}

			}

		}
		if(nahi){
			System.out.println("-1");
		}

		else{
			k=0;
			nahi=false;

			int i=0,j=0;
			while(i<n && j<n){
			if(x==i && y==j){
				nahi=true;
				break;
			}
				k+=a[i][j];
				i=i+1;
				j=j+1;
			}
			if(nahi==false && sum!=k){
				System.out.println("-1");
			}
			else if(nahi==true){
				System.out.println("-1");
			}
			else{

				i=0;j=n-1;
				k=0;
				nahi=false;
				while(i<n &&j>=0){
					if(i==x && j==y){
						nahi=true;
						break;
					}
					k+=a[i][j];
					i=i+1;
					j=j-1;

				}
				if(nahi==false && sum!=k){
					System.out.println("-1");
				}
				else if(nahi==true){
					System.out.println("-1");
				}
				else{
					k=0;
					for( j=0;j<n;j++){
						k+=a[x][j];
					}
					long h=sum-k;
					if(h<=0)
					System.out.println("-1");
					else
					System.out.println(h);

				}

			}

		}

		}

	}
	}
}


//Bismillahir rahmanir rahim
#include<bits/stdc++.h>
using namespace std;
int arr[505][505];
int main()
{
    int n;
    cin >> n;
    long long sum=0;
    set<long long>s;
    for(int i=0;i<n;i++){
        long long sum=0;
        for(int j=0;j<n;j++){
            cin >> arr[i][j];
            sum+=arr[i][j];
        }
        s.insert(sum);
    }
    if(n==1){
        cout << 1 << endl;
        return 0;
    }
    for(int i=0;i<n;i++){
        long long sum=0;
        for(int j=0;j<n;j++){
            sum+=arr[j][i];
        }
        s.insert(sum);
    }
    sum=0;
    for(int i=0;i<n;i++){
        sum+=arr[i][i];
    }
    s.insert(sum);
    sum=0;
    for(int i=0;i<n;i++){
        sum+=arr[i][n-1-i];
    }
    s.insert(sum);
    if(s.size()==2){
        set<long long>::iterator it=s.begin();
        long long x=(*it);
        it++;
        long long y=(*it);
        cout << y-x << endl;
    }
    else{
        cout << -1 << endl;
    }
    return 0;
}


//Bismillahir rahmanir rahim
#include<bits/stdc++.h>
using namespace std;
int arr[505][505];
int main()
{
    int n;
    cin >> n;
    long long sum=0;
    set<long long>s;
    for(int i=0;i<n;i++){
        long long sum=0;
        for(int j=0;j<n;j++){
            cin >> arr[i][j];
            sum+=arr[i][j];
        }
        s.insert(sum);
    }
    if(n==1){
        cout << 1 << endl;
        return 0;
    }
    for(int i=0;i<n;i++){
        long long sum=0;
        for(int j=0;j<n;j++){
            sum+=arr[j][i];
        }
        s.insert(sum);
    }
    if(s.size()==2){
        set<long long>::iterator it=s.begin();
        long long x=(*it);
        it++;
        long long y=(*it);
        cout << y-x << endl;
    }
    else{
        cout << -1 << endl;
    }
    return 0;
}


//Bismillahir rahmanir rahim
#include<bits/stdc++.h>
using namespace std;
int arr[505][505];
int main()
{
    int n;
    cin >> n;
    long long sum=0;
    set<long long>s;
    for(int i=0;i<n;i++){
        long long sum=0;
        for(int j=0;j<n;j++){
            cin >> arr[i][j];
            sum+=arr[i][j];
        }
        s.insert(sum);
    }
    if(n==1){
        cout << 1 << endl;
        return 0;
    }
    for(int i=0;i<n;i++){
        long long sum=0;
        for(int j=0;j<n;j++){
            sum+=arr[j][i];
        }
        s.insert(sum);
    }
    long long sum=0;
    for(int i=0;i<n;i++){
        sum+=arr[i][i];
    }
    s.insert(sum);
    long long sum=0;
    for(int i=0;i<n;i++){
        sum+=arr[i][n-1-i];
    }
    s.insert(sum);
    if(s.size()==2){
        set<long long>::iterator it=s.begin();
        long long x=(*it);
        it++;
        long long y=(*it);
        cout << y-x << endl;
    }
    else{
        cout << -1 << endl;
    }
    return 0;
}


//Bismillahir rahmanir rahim
#include<bits/stdc++.h>
using namespace std;
int arr[505][505];
int n;
bool check()
{
    set<long long>st;
    long long sum=0;
    for(int i=0;i<n;i++){
        sum=0;
        for(int j=0;j<n;j++){
            sum+=arr[i][j];
        }
        st.insert(sum);
    }
    for(int i=0;i<n;i++){
        sum=0;
        for(int j=0;j<n;j++){
            sum+=arr[j][i];
        }
        st.insert(sum);
    }
    sum=0;
    for(int i=0;i<n;i++){
        sum+=arr[i][i];
    }
    st.insert(sum);
    sum=0;
    for(int i=0;i<n;i++){
        sum+=arr[i][n-1-i];
    }
    st.insert(sum);
    return (st.size()==1);
}
int main()
{
    int x,y;
    cin >> n;
    long long sum=0;
    set<long long>s;
    for(int i=0;i<n;i++){
        sum=0;
        for(int j=0;j<n;j++){
            cin >> arr[i][j];
            sum+=arr[i][j];
            if(arr[i][j]==0) x=i,y=j;
        }
        s.insert(sum);
    }
    if(n==1){
        cout << 1 << endl;
        return 0;
    }
    for(int i=0;i<n;i++){
        sum=0;
        for(int j=0;j<n;j++){
            sum+=arr[j][i];
        }
        s.insert(sum);
    }
    sum=0;
    for(int i=0;i<n;i++){
        sum+=arr[i][i];
    }
    s.insert(sum);
    sum=0;
    for(int i=0;i<n;i++){
        sum+=arr[i][n-1-i];
    }
    s.insert(sum);
    if(s.size()==2){
        set<long long>::iterator it=s.begin();
        long long xx=(*it);
        it++;
        long long yy=(*it);
        arr[x][y]=yy-xx;
        if(check()){
            cout << arr[x][y] << endl;
        }
        else{
            cout << -1 << endl;
        }
    }
    else{
        cout << -1 << endl;
    }
    return 0;
}


//Bismillahir rahmanir rahim
#include<bits/stdc++.h>
using namespace std;
int arr[505][505];
int main()
{
    int n;
    cin >> n;
    long long sum=0;
    set<long long>s;
    for(int i=0;i<n;i++){
        long long sum=0;
        for(int j=0;j<n;j++){
            cin >> arr[i][j];
            sum+=arr[i][j];
        }
        s.insert(sum);
    }
    for(int i=0;i<n;i++){
        long long sum=0;
        for(int j=0;j<n;j++){
            sum+=arr[j][i];
        }
        s.insert(sum);
    }
    if(s.size()==2){
        set<long long>::iterator it=s.begin();
        long long x=(*it);
        it++;
        long long y=(*it);
        cout << y-x << endl;
    }
    else{
        cout << -1 << endl;
    }
    return 0;
}


//Bismillahir rahmanir rahim
#include<bits/stdc++.h>
using namespace std;
#define ll long long
ll arr[505][505];
ll n;
bool check()
{
    set<ll>st;
    ll sum=0;
    for(ll i=0;i<n;i++){
        sum=0;
        for(ll j=0;j<n;j++){
            sum+=arr[i][j];
        }
        st.insert(sum);
    }
    for(ll i=0;i<n;i++){
        sum=0;
        for(ll j=0;j<n;j++){
            sum+=arr[j][i];
        }
        st.insert(sum);
    }
    sum=0;
    for(ll i=0;i<n;i++){
        sum+=arr[i][i];
    }
    st.insert(sum);
    sum=0;
    for(ll i=0;i<n;i++){
        sum+=arr[i][n-1-i];
    }
    st.insert(sum);
    return (st.size()==1);
}
int main()
{
    ll x,y;
    cin >> n;
    ll sum=0;
    set<ll>s;
    for(ll i=0;i<n;i++){
        sum=0;
        for(ll j=0;j<n;j++){
            cin >> arr[i][j];
            sum+=arr[i][j];
            if(arr[i][j]==0) x=i,y=j;
        }
        s.insert(sum);
    }
    if(n==1){
        cout << 1 << endl;
        return 0;
    }
    for(ll i=0;i<n;i++){
        sum=0;
        for(ll j=0;j<n;j++){
            sum+=arr[j][i];
        }
        s.insert(sum);
    }
    sum=0;
    for(ll i=0;i<n;i++){
        sum+=arr[i][i];
    }
    s.insert(sum);
    sum=0;
    for(ll i=0;i<n;i++){
        sum+=arr[i][n-1-i];
    }
    s.insert(sum);
    if(s.size()==2){
        set<ll>::iterator it=s.begin();
        ll xx=(*it);
        it++;
        ll yy=(*it);
        arr[x][y]=yy-xx;
        if(check()){
            cout << arr[x][y] << endl;
        }
        else{
            cout << -1 << endl;
        }
    }
    else{
        cout << -1 << endl;
    }
    return 0;
}


//Bismillahir rahmanir rahim
#include<bits/stdc++.h>
using namespace std;
int arr[505][505];
int n;
bool check()
{
    set<long long>st;
    long long sum=0;
    for(int i=0;i<n;i++){
        sum=0;
        for(int j=0;j<n;j++){
            sum+=arr[i][j];
        }
        st.insert(sum);
    }
    for(int i=0;i<n;i++){
        sum=0;
        for(int j=0;j<n;j++){
            sum+=arr[j][i];
        }
        st.insert(sum);
    }
    sum=0;
    for(int i=0;i<n;i++){
        sum+=arr[i][i];
    }
    st.insert(sum);
    sum=0;
    for(int i=0;i<n;i++){
        sum+=arr[i][n-1-i];
    }
    st.insert(sum);
    return (st.size()==1);
}
int main()
{
    int x,y;
    cin >> n;
    long long sum=0;
    set<long long>s;
    for(int i=0;i<n;i++){
        sum=0;
        for(int j=0;j<n;j++){
            cin >> arr[i][j];
            sum+=arr[i][j];
            if(arr[i][j]==0) x=i,y=j;
        }
        s.insert(sum);
    }
    if(n==1){
        cout << 1 << endl;
        return 0;
    }
    for(int i=0;i<n;i++){
        sum=0;
        for(int j=0;j<n;j++){
            sum+=arr[j][i];
        }
        s.insert(sum);
    }
    sum=0;
    for(int i=0;i<n;i++){
        sum+=arr[i][i];
    }
    s.insert(sum);
    sum=0;
    for(int i=0;i<n;i++){
        sum+=arr[i][n-1-i];
    }
    s.insert(sum);
    if(s.size()==2){
        set<long long>::iterator it=s.begin();
        long long xx=(*it);
        it++;
        long long yy=(*it);
        arr[x][y]=(yy-xx);
    }
    if(check()){
        cout << arr[x][y] << endl;
    }
    else{
        cout << -1 << endl;
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
const ll MOD = 1e9+7, N = 1e6 + 1;
int main()
{
  ll n,x=0;
  cin>>n;
  set<ll>s;
  ll arr[n][n];
  for(ll i=0;i<n;i++)
  {
    for(ll j=0;j<n;j++)
    {
      cin>>arr[i][j];
      x=x+arr[i][j];
    }
    s.insert(x);
    x=0;
  }
 // cout<<s.size()<<endl;
x=0;
   for(ll j=0;j<n;j++)
  {
    for(ll i=0;i<n;i++)
    {
     // cin>>arr[i][j];
      x=x+arr[i][j];
    }
    s.insert(x);
    x=0;
  }
  //cout<<s.size()<<endl;
for(ll i=0;i<n;i++)
{
  x=x+arr[i][i];
}
 s.insert(x);
    x=0;

  //  cout<<s.size()<<endl;
for(ll i=0;i<n;i++)
{
  x=x+arr[i][n-i-1];
}
s.insert(x);
    x=0;
 //cout<<s.size();
 if(s.size()>2)
 cout<<-1;
 else
 {
  ll a=*(s.begin());
  s.erase(*(s.begin()));
    ll b=*(s.begin());
    if(b-a>=0)
    cout<<b-a;
   else
    cout<<-1;
 }

  return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
const ll MOD = 1e9+7, N = 1e6 + 1;
int main()
{
  ll n,x=0,xx=0,xy=0,dig=0,dig2=0;
  cin>>n;
  set<ll>s;
  ll arr[n][n];
  for(ll i=0;i<n;i++)
  {
    for(ll j=0;j<n;j++)
    {
      cin>>arr[i][j];
      x=x+arr[i][j];
      if(arr[i][j]==0)
      {
        xx=j;
        xy=i;
      }
    }
    s.insert(x);
    x=0;
  }
 // cout<<s.size()<<endl;
x=0;
   for(ll j=0;j<n;j++)
  {
    for(ll i=0;i<n;i++)
    {
     // cin>>arr[i][j];
      x=x+arr[i][j];
    }
    s.insert(x);
    x=0;
  }
  //cout<<s.size()<<endl;
for(ll i=0;i<n;i++)
{
  x=x+arr[i][i];
}
dig=x;
 s.insert(x);
    x=0;

  //  cout<<s.size()<<endl;
for(ll i=0;i<n;i++)
{
  x=x+arr[i][n-i-1];
}
dig2=x;
s.insert(x);
    x=0;
 ll sum1=0,sum2=0;
for(ll i=0;i<n;i++)
{
   sum1=sum1+arr[xy][i];
}

for(ll i=0;i<n;i++)
{
   sum2=sum2+arr[i][xx];
}

//cout<<xx<<" "<<xy<<endl;
if(xx!=xy)
{
  if(sum1==dig || sum2==dig)
    {cout<<-1;return 0;}

}

 if(n==1)
 {
  cout<<1;
  return 0;
 }
 if(s.size()!=2 || sum1!=sum2)
 cout<<-1;
 else
 {
  ll a=*(s.begin());
  s.erase(*(s.begin()));
    ll b=*(s.begin());
   if(sum1==a)
   {
    if(b-a>0)
      cout<<b-a;
    else
      cout<<-1;
   }
   else
    cout<<-1;
 }

  return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
const ll MOD = 1e9+7, N = 1e6 + 1;
int main()
{
  ll n,x=0;
  cin>>n;
  set<ll>s;
  ll arr[n][n];
  for(ll i=0;i<n;i++)
  {
    for(ll j=0;j<n;j++)
    {
      cin>>arr[i][j];
      x=x+arr[i][j];
    }
    s.insert(x);
    x=0;
  }
 // cout<<s.size()<<endl;
x=0;
   for(ll j=0;j<n;j++)
  {
    for(ll i=0;i<n;i++)
    {
     // cin>>arr[i][j];
      x=x+arr[i][j];
    }
    s.insert(x);
    x=0;
  }
  //cout<<s.size()<<endl;
for(ll i=0;i<n;i++)
{
  x=x+arr[i][i];
}
 s.insert(x);
    x=0;

  //  cout<<s.size()<<endl;
for(ll i=0;i<n;i++)
{
  x=x+arr[i][n-i-1];
}
s.insert(x);
    x=0;
 //cout<<s.size();
 if(s.size()!=2)
 cout<<-1;
 else
 {
  ll a=*(s.begin());
  s.erase(*(s.begin()));
    ll b=*(s.begin());
    if(b-a>0)
    cout<<b-a;
   else
    cout<<-1;
 }

  return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
const ll MOD = 1e9+7, N = 1e6 + 1;
int main()
{
  ll n,x=0,xx=0,xy=0;
  cin>>n;
  set<ll>s;
  ll arr[n][n];
  for(ll i=0;i<n;i++)
  {
    for(ll j=0;j<n;j++)
    {
      cin>>arr[i][j];
      x=x+arr[i][j];
      if(arr[i][j]==0)
      {
        xx=j;
        xy=i;
      }
    }
    s.insert(x);
    x=0;
  }
 // cout<<s.size()<<endl;
x=0;
   for(ll j=0;j<n;j++)
  {
    for(ll i=0;i<n;i++)
    {
     // cin>>arr[i][j];
      x=x+arr[i][j];
    }
    s.insert(x);
    x=0;
  }
  //cout<<s.size()<<endl;
for(ll i=0;i<n;i++)
{
  x=x+arr[i][i];
}
 s.insert(x);
    x=0;

  //  cout<<s.size()<<endl;
for(ll i=0;i<n;i++)
{
  x=x+arr[i][n-i-1];
}
s.insert(x);
    x=0;
 ll sum1=0,sum2=0;
for(ll i=0;i<n;i++)
{
   sum1=sum1+arr[xy][i];
}

for(ll i=0;i<n;i++)
{
   sum2=sum2+arr[i][xx];
}

//cout<<xx<<" "<<xy<<endl;

 if(n==1)
 {
  cout<<1;
  return 0;
 }
 if(s.size()!=2 || sum1!=sum2)
 cout<<-1;
 else
 {
  ll a=*(s.begin());
  s.erase(*(s.begin()));
    ll b=*(s.begin());
   if(sum1==a)
   {
    if(b-a>0)
      cout<<b-a;
    else
      cout<<-1;
   }
   else
    cout<<-1;
 }

  return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
const ll MOD = 1e9+7, N = 1e6 + 1;
int main()
{
  ll n,x=0;
  cin>>n;
  set<ll>s;
  ll arr[n][n];
  for(ll i=0;i<n;i++)
  {
    for(ll j=0;j<n;j++)
    {
      cin>>arr[i][j];
      x=x+arr[i][j];
    }
    s.insert(x);
    x=0;
  }
 // cout<<s.size()<<endl;
x=0;
   for(ll j=0;j<n;j++)
  {
    for(ll i=0;i<n;i++)
    {
     // cin>>arr[i][j];
      x=x+arr[i][j];
    }
    s.insert(x);
    x=0;
  }
  //cout<<s.size()<<endl;
for(ll i=0;i<n;i++)
{
  x=x+arr[i][i];
}
 s.insert(x);
    x=0;

  //  cout<<s.size()<<endl;
for(ll i=0;i<n;i++)
{
  x=x+arr[i][n-i-1];
}
s.insert(x);
    x=0;
 //cout<<s.size();
 if(n==1)
 {
  cout<<1;
  return 0;
 }
 if(s.size()>2)
 cout<<-1;
 else
 {
  ll a=*(s.begin());
  s.erase(*(s.begin()));
    ll b=*(s.begin());
    if(b-a>0)
    cout<<b-a;
   else
    cout<<-1;
 }

  return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
const ll MOD = 1e9+7, N = 1e6 + 1;
int main()
{
  ll n,x=0;
  cin>>n;
  set<ll>s;
  ll arr[n][n];
  for(ll i=0;i<n;i++)
  {
    for(ll j=0;j<n;j++)
    {
      cin>>arr[i][j];
      x=x+arr[i][j];
    }
    s.insert(x);
    x=0;
  }
 // cout<<s.size()<<endl;
x=0;
   for(ll j=0;j<n;j++)
  {
    for(ll i=0;i<n;i++)
    {
     // cin>>arr[i][j];
      x=x+arr[i][j];
    }
    s.insert(x);
    x=0;
  }
  //cout<<s.size()<<endl;
for(ll i=0;i<n;i++)
{
  x=x+arr[i][i];
}
 s.insert(x);
    x=0;

  //  cout<<s.size()<<endl;
for(ll i=0;i<n;i++)
{
  x=x+arr[i][n-i-1];
}
s.insert(x);
    x=0;
 //cout<<s.size();
 if(n==1)
 {
  cout<<1;
  return 0;
 }
 if(s.size()!=2)
 cout<<-1;
 else
 {
  ll a=*(s.begin());
  s.erase(*(s.begin()));
    ll b=*(s.begin());
    if(b-a>0)
    cout<<b-a;
   else
    cout<<-1;
 }

  return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <set>
#include <stack>
#include <cmath>
#include <string>

using namespace std;

int main()
{
	long long int a[500][500], i, j, n, x, y, sum, sumx = 0, row[500] = { 0 }, column[500] = {0},ank1=0,ank2=0;
	cin >> n;
	for (i = 0; i < n; i++)
	{
		for (j = 0; j < n; j++)
		{
			cin >> a[i][j];
			row[i] += a[i][j];
			column[j] += a[i][j];
			if (a[i][j] == 0)
			{
				x = j;
				y = i;
			}
		}
	}
	if (n == 1)
	{
		cout << 1;
		return 0;
	}
	if (y >= 1)
	{
		a[y][x] = row[y - 1] - row[y];

	}
	else
	{
		a[y][x] = row[y + 1] - row[y];

	}
	if (a[y][x] <= 0)
	{
		cout << -1;
		return 0;
	}

	row[y] += a[y][x];
	column[x] += a[y][x];
	for (i = 0; i < n; i++)
	{
		ank1 += a[i][i];
		ank2 += a[i][n - 1 - i];
	}
	for (i = 1; i < n; i++)
	{
		if (row[i] != row[i - 1]||column[i]!=column[i-1]||column[i]!=row[i]||ank1!=ank2||ank1!=row[i])
		{
			cout << -1;
			return 0;
		}
	}
	if
	cout << a[y][x];
	return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <set>
#include <stack>
#include <cmath>
#include <string>

using namespace std;

int main()
{
	long long int a[500][500], i, j, n, x, y, sum, sumx = 0, row[500] = { 0 }, column[500] = {0},ank1=0,ank2=0;
	cin >> n;
	for (i = 0; i < n; i++)
	{
		for (j = 0; j < n; j++)
		{
			cin >> a[i][j];
			row[i] += a[i][j];
			column[j] += a[i][j];
			if (a[i][j] == 0)
			{
				x = j;
				y = i;
			}
		}
	}
	if (n == 1)
	{
		cout << 1;
		return 0;
	}
	if (y >= 1)
	{
		a[y][x] = row[y - 1] - row[y];

	}
	else
	{
		a[y][x] = row[y + 1] - row[y];

	}
	if (a[y][x] <= 0)
	{
		cout << -1;
		return 0;
	}

	row[y] += a[y][x];
	column[x] += a[y][x];
	for (i = 0; i < n; i++)
	{
		ank1 += a[i][i];
		ank2 += a[i][n - 1 - i];
	}
	for (i = 1; i < n; i++)
	{
		if (row[i] != row[i - 1]||column[i]!=column[i-1]||column[i]!=row[i]||ank1!=ank2||ank1!=row[i])
		{
			cout << -1;
			return 0;
		}
	}

	cout << a[y][x];
	return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <set>
#include <stack>
#include <cmath>
#include <string>

using namespace std;

int main()
{
	long long int a[500][500], i, j, n, x, y, sum, sumx = 0, row[500] = { 0 }, column[500] = {0},ank1=0,ank2=0;
	cin >> n;
	for (i = 0; i < n; i++)
	{
		for (j = 0; j < n; j++)
		{
			cin >> a[i][j];
			row[i] += a[i][j];
			column[j] += a[i][j];
			if (a[i][j] == 0)
			{
				x = j;
				y = i;
			}
		}
	}
	if (y >= 1)
	{
		a[y][x] = row[y - 1] - row[y];

	}
	else
	{
		a[y][x] = row[y + 1] - row[y];

	}
	if (a[y][x] < 0)
	{
		cout << -1;
		return 0;
	}
	if (n == 1)
	{
		cout << 1;
		return 0;
	}
	row[y] += a[y][x];
	column[x] += a[y][x];
	for (i = 0; i < n; i++)
	{
		ank1 += a[i][i];
		ank2 += a[i][n - 1 - i];
	}
	for (i = 1; i < n; i++)
	{
		if (row[i] != row[i - 1]||column[i]!=column[i-1]||column[i]!=row[i]||ank1!=ank2||ank1!=row[i])
		{
			cout << -1;
			return 0;
		}
	}

	return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <set>
#include <stack>
#include <cmath>
#include <string>

using namespace std;

int main()
{
	int a[500][500], i, j, n, x, y, sum, sumx = 0, row[500] = { 0 }, column[500] = {0},ank1=0,ank2=0;
	cin >> n;
	for (i = 0; i < n; i++)
	{
		for (j = 0; j < n; j++)
		{
			cin >> a[i][j];
			row[i] += a[i][j];
			column[j] += a[i][j];
			if (a[i][j] == 0)
			{
				x = j;
				y = i;
			}
		}
	}
	if (y >= 1)
	{
		a[y][x] = row[y - 1] - row[y];
	}
	else
	{
		a[y][x] = row[y + 1] - row[y];
	}
	row[y] += a[y][x];
	column[x] += a[y][x];
	for (i = 0; i < n; i++)
	{
		ank1 += a[i][i];
		ank2 += a[i][n - 1 - i];
	}
	for (i = 1; i < n; i++)
	{
		if (row[i] != row[i - 1]||column[i]!=column[i-1]||column[i]!=row[i]||ank1!=ank2||ank1!=row[i])
		{
			cout << -1;
			return 0;
		}
	}
	cout << a[y][x];
	return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <set>
#include <stack>
#include <cmath>
#include <string>

using namespace std;

int main()
{
	long long  a[500][500], i, j, n, x, y, sum, sumx = 0, row[500] = { 0 }, column[500] = {0},ank1=0,ank2=0;
	cin >> n;
	for (i = 0; i < n; i++)
	{
		for (j = 0; j < n; j++)
		{
			cin >> a[i][j];
			row[i] += a[i][j];
			column[j] += a[i][j];
			if (a[i][j] == 0)
			{
				x = j;
				y = i;
			}
		}
	}
	if (y >= 1)
	{
		a[y][x] = row[y - 1] - row[y];

	}
	else
	{
		a[y][x] = row[y + 1] - row[y];

	}
	if (a[y][x] < 0)
	{
		cout << -1;
		return 0;
	}
	if (n == 1)
	{
		cout << 1;
		return 0;
	}
	row[y] += a[y][x];
	column[x] += a[y][x];
	for (i = 0; i < n; i++)
	{
		ank1 += a[i][i];
		ank2 += a[i][n - 1 - i];
	}
	for (i = 1; i < n; i++)
	{
		if (row[i] != row[i - 1]||column[i]!=column[i-1]||column[i]!=row[i]||ank1!=ank2||ank1!=row[i])
		{
			cout << -1;
			return 0;
		}
	}

	return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <set>
#include <stack>
#include <cmath>
#include <string>

using namespace std;

int main()
{
	long long int a[500][500], i, j, n, x, y, sum, sumx = 0, row[500] = { 0 }, column[500] = {0},ank1=0,ank2=0;
	cin >> n;
	for (i = 0; i < n; i++)
	{
		for (j = 0; j < n; j++)
		{
			cin >> a[i][j];
			row[i] += a[i][j];
			column[j] += a[i][j];
			if (a[i][j] == 0)
			{
				x = j;
				y = i;
			}
		}
	}
	if (y >= 1)
	{
		a[y][x] = row[y - 1] - row[y];

	}
	else
	{
		a[y][x] = row[y + 1] - row[y];

	}
	if (a[y][x] < 0)
	{
		cout << -1;
		return 0;
	}
	if (n == 1)
	{
		cout << 1;
		return 0;
	}
	row[y] += a[y][x];
	column[x] += a[y][x];
	for (i = 0; i < n; i++)
	{
		ank1 += a[i][i];
		ank2 += a[i][n - 1 - i];
	}
	for (i = 1; i < n; i++)
	{
		if (row[i] != row[i - 1]||column[i]!=column[i-1]||column[i]!=row[i]||ank1!=ank2||ank1!=row[i])
		{
			cout << -1;
			return 0;
		}
	}
	cout << a[y][x];
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define fr(i,s,e) for(i=s;i<e;++i)
#define ps push_back
#define tr(s,it) for(it=s.begin();it!=s.end();++it)
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);

int main() {
    fast;
    ll i,j,n,x,y,k=0,idxi,idxj,dia1=0,dia2=0;
    cin>>n;
    ll arr[n][n];
    ll row[n],col[n];
    set<ll>rows,cols,tot;
    fr(i,0,n){
        row[i]=col[i]=0;
    }
    fr(i,0,n){
        fr(j,0,n)
        {cin>>arr[i][j];
        row[i]+=arr[i][j];
        if(arr[i][j]==0){idxi=i;idxj=j;}
        }
        rows.insert(row[i]);
    }
    fr(i,0,n){
        fr(j,0,n)
        col[i]+=arr[j][i];
        cols.insert(col[i]);
    }
   /* fr(i,0,n)dia1+=arr[i][i];
    fr(i,0,n)dia2+=arr[i][n-1-i];
    bool ok=true;*/
// if(dia1==dia2 && (i==j || i+j==n-1))||(dia1!=dia2 && (i!=j&&(i+j)!=n-1)) ||

    if(n>2&&(rows.size()!=2||cols.size()!=2)){
        cout<<-1;
        return 0;
    }
    for(auto &it:rows)tot.insert(it);
    for(auto &it:cols)tot.insert(it);
    if(n==1){
        cout<<1;
    }
    else if(n==2){
        if(tot.size()<=3){
            if(arr[0][0]==0)cout<<arr[1][1];
            else if(arr[1][0]==0)cout<<arr[0][1];
            else if(arr[0][1]==0)cout<<arr[1][0];
            else cout<<arr[0][0];
        }
    }
    else{
        x=row[0];
        fr(i,1,n)if(row[i]!=x){x=abs(x-row[i]);break;}
        arr[idxi][idxj]=x;
        fr(i,0,n)dia1+=arr[i][i];
        fr(i,0,n)dia2+=arr[i][n-1-i];
        fr(i,0,n)row[i]=col[i]=0;
        rows.clear();cols.clear();
        fr(i,0,n){
        fr(j,0,n)
        {col[i]+=arr[j][i];
        row[i]+=arr[i][j];}
        cols.insert(col[i]);rows.insert(row[i]);
    }
        if(dia2!=dia1 || cols.size()!=1 || rows.size()!=1)cout<<-1;
        else cout<<x;
    }
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define fr(i,s,e) for(i=s;i<e;++i)
#define ps push_back
#define tr(s,it) for(it=s.begin();it!=s.end();++it)
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);

int main() {
    fast;
    ll i,j,n,x,y,k=0,idxi,idxj,dia1=0,dia2=0;
    cin>>n;
    ll arr[n][n];
    ll row[n],col[n];
    set<ll>rows,cols,tot;
    fr(i,0,n){
        row[i]=col[i]=0;
    }
    fr(i,0,n){
        fr(j,0,n)
        {cin>>arr[i][j];
        row[i]+=arr[i][j];
        if(arr[i][j]==0){idxi=i;idxj=j;}
        }
        rows.insert(row[i]);
    }
    fr(i,0,n){
        fr(j,0,n)
        col[i]+=arr[j][i];
        cols.insert(col[i]);
    }
   /* fr(i,0,n)dia1+=arr[i][i];
    fr(i,0,n)dia2+=arr[i][n-1-i];
    bool ok=true;*/
// if(dia1==dia2 && (i==j || i+j==n-1))||(dia1!=dia2 && (i!=j&&(i+j)!=n-1)) ||

    if(n>2&&(rows.size()!=2||cols.size()!=2)){
        cout<<-1;
        return 0;
    }
    for(auto &it:rows)tot.insert(it);
    for(auto &it:cols)tot.insert(it);
    if(n==1){
        cout<<1;
    }
    else if(n==2){
        if(tot.size()<=3){
            if(arr[0][0]==0)cout<<arr[1][1];
            else if(arr[1][0]==0)cout<<arr[0][1];
            else if(arr[0][1]==0)cout<<arr[1][0];
            else cout<<arr[0][0];
        }
    }
    else{
        x=row[0];
        fr(i,1,n)if(row[i]!=x){x=abs(x-row[i]);break;}
        arr[idxi][idxj]=x;
        fr(i,0,n)dia1+=arr[i][i];
        fr(i,0,n)dia2+=arr[i][n-1-i];
        fr(i,0,n)row[i]=col[i]=0;
        rows.clear();cols.clear();
        fr(i,0,n){
        fr(j,0,n)
        {col[i]+=arr[j][i];
        row[i]+=arr[i][j];}
        cols.insert(col[i]);rows.insert(row[i]);
    }
        if(dia2!=dia1 || cols.size()!=1 || rows.size()!=1 || dia1!=row[0])cout<<-1;
        else cout<<x;
    }
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define fr(i,s,e) for(i=s;i<e;++i)
#define ps push_back
#define tr(s,it) for(it=s.begin();it!=s.end();++it)
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);

int main() {
    fast;
    ll i,j,n,x,y,k=0,idxi,idxj,dia1=0,dia2=0;
    cin>>n;
    ll arr[n][n];
    ll row[n],col[n];
    set<ll>rows,cols,tot;
    fr(i,0,n){
        row[i]=col[i]=0;
    }
    fr(i,0,n){
        fr(j,0,n)
        {cin>>arr[i][j];
        row[i]+=arr[i][j];
        if(arr[i][j]==0){idxi=i;idxj=j;}
        }
        rows.insert(row[i]);
    }
    fr(i,0,n){
        fr(j,0,n)
        col[i]+=arr[j][i];
        cols.insert(col[i]);
    }
   /* fr(i,0,n)dia1+=arr[i][i];
    fr(i,0,n)dia2+=arr[i][n-1-i];
    bool ok=true;*/
// if(dia1==dia2 && (i==j || i+j==n-1))||(dia1!=dia2 && (i!=j&&(i+j)!=n-1)) ||

    if(n>2&&(rows.size()!=2||cols.size()!=2)){
        cout<<-1;
        return 0;
    }
    for(auto &it:rows)tot.insert(it);
    for(auto &it:cols)tot.insert(it);
    if(n==1){
        cout<<1;
    }
    else if(n==2){
        if(tot.size()<=3){
            if(arr[0][0]==0)arr[0][0]=arr[1][1];
            else if(arr[1][0]==0)arr[1][0]=arr[0][1];
            else if(arr[0][1]==0)arr[0][1]=arr[1][0];
            else arr[1][1]=arr[0][0];
        }
        x=arr[idxi][idxj];
        fr(i,0,n)dia1+=arr[i][i];
        fr(i,0,n)dia2+=arr[i][n-1-i];
        fr(i,0,n)row[i]=col[i]=0;
        rows.clear();cols.clear();
        fr(i,0,n){
        fr(j,0,n)
        {col[i]+=arr[j][i];
        row[i]+=arr[i][j];}
        cols.insert(col[i]);rows.insert(row[i]);
    }
        if(dia2!=dia1 || cols.size()!=1 || rows.size()!=1 || dia1!=row[0])cout<<-1;
        else cout<<x;
    }
    else{
        x=row[0];
        fr(i,1,n)if(row[i]!=x){x=abs(x-row[i]);break;}
        arr[idxi][idxj]=x;
        fr(i,0,n)dia1+=arr[i][i];
        fr(i,0,n)dia2+=arr[i][n-1-i];
        fr(i,0,n)row[i]=col[i]=0;
        rows.clear();cols.clear();
        fr(i,0,n){
        fr(j,0,n)
        {col[i]+=arr[j][i];
        row[i]+=arr[i][j];}
        cols.insert(col[i]);rows.insert(row[i]);
    }
        if(dia2!=dia1 || cols.size()!=1 || rows.size()!=1 || dia1!=row[0])cout<<-1;
        else cout<<x;
    }
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define fr(i,s,e) for(i=s;i<e;++i)
#define ps push_back
#define tr(s,it) for(it=s.begin();it!=s.end();++it)
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);

int main() {
    fast;
    ll i,j,n,x,y,k=0,idxi,idxj,dia1=0,dia2=0;
    cin>>n;
    ll arr[n][n];
    ll row[n],col[n];
    set<ll>rows,cols,tot;
    fr(i,0,n){
        row[i]=col[i]=0;
    }
    fr(i,0,n){
        fr(j,0,n)
        {cin>>arr[i][j];
        row[i]+=arr[i][j];
        if(arr[i][j]==0){idxi=i;idxj=j;}
        }
        rows.insert(row[i]);
    }
    fr(i,0,n){
        fr(j,0,n)
        col[i]+=arr[j][i];
        cols.insert(col[i]);
    }
   /* fr(i,0,n)dia1+=arr[i][i];
    fr(i,0,n)dia2+=arr[i][n-1-i];
    bool ok=true;*/
// if(dia1==dia2 && (i==j || i+j==n-1))||(dia1!=dia2 && (i!=j&&(i+j)!=n-1)) ||

    if(n>2&&(rows.size()!=2||cols.size()!=2)){
        cout<<-1;
        return 0;
    }
    for(auto &it:rows)tot.insert(it);
    for(auto &it:cols)tot.insert(it);
    if(n==1){
        cout<<1;
    }
    else if(n==2){
        if(tot.size()<=3){
            if(arr[0][0]==0)cout<<arr[1][1];
            else if(arr[1][0]==0)cout<<arr[0][1];
            else if(arr[0][1]==0)cout<<arr[1][0];
            else cout<<arr[0][0];
        }
    }
    else{
        x=row[0];
        fr(i,1,n)if(row[i]!=x){x=abs(x-row[i]);break;}
        arr[idxi][idxj]=x;
        fr(i,0,n)dia1+=arr[i][i];
        fr(i,0,n)dia2+=arr[i][n-1-i];
        if(dia2==dia1)cout<<x;
        else cout<<-1;
    }
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define fr(i,s,e) for(i=s;i<e;++i)
#define ps push_back
#define tr(s,it) for(it=s.begin();it!=s.end();++it)
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);

int main() {
    fast;
    ll i,j,n,x,y,k=0,idxi,idxj;
    cin>>n;
    ll arr[n][n];
    ll row[n],col[n];
    set<ll>rows,cols,tot;
    fr(i,0,n){
        row[i]=col[i]=0;
    }
    fr(i,0,n){
        fr(j,0,n)
        {cin>>arr[i][j];
        row[i]+=arr[i][j];
        if(arr[i][j]==0){idxi=i;idxj=j;}
        }
        rows.insert(row[i]);
    }
    fr(i,0,n){
        fr(j,0,n)
        col[i]+=arr[j][i];
        cols.insert(col[i]);
    }
    if(n>2&&(rows.size()!=2||cols.size()!=2)){
        cout<<-1;
        return 0;
    }
    for(auto &it:rows)tot.insert(it);
    for(auto &it:cols)tot.insert(it);
    if(n==1){
        cout<<1;
    }
    else if(n==2){
        if(tot.size()<=3){
            if(arr[0][0]==0)cout<<arr[1][1];
            else if(arr[1][0]==0)cout<<arr[0][1];
            else if(arr[0][1]==0)cout<<arr[1][0];
            else cout<<arr[0][0];
        }
    }
    else{
        x=row[0];
        fr(i,1,n)if(row[i]!=x){x=abs(x-row[i]);break;}
        cout<<x;
    }
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define fr(i,s,e) for(i=s;i<e;++i)
#define ps push_back
#define tr(s,it) for(it=s.begin();it!=s.end();++it)
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);

int main() {
    fast;
    ll i,j,n,x,y,k=0,idxi,idxj,dia1=0,dia2=0;
    cin>>n;
    ll arr[n][n];
    ll row[n],col[n];
    set<ll>rows,cols,tot;
    fr(i,0,n){
        row[i]=col[i]=0;
    }
    fr(i,0,n){
        fr(j,0,n)
        {cin>>arr[i][j];
        row[i]+=arr[i][j];
        if(arr[i][j]==0){idxi=i;idxj=j;}
        }
        rows.insert(row[i]);
    }
    fr(i,0,n){
        fr(j,0,n)
        col[i]+=arr[j][i];
        cols.insert(col[i]);
    }
   /* fr(i,0,n)dia1+=arr[i][i];
    fr(i,0,n)dia2+=arr[i][n-1-i];
    bool ok=true;*/
// if(dia1==dia2 && (i==j || i+j==n-1))||(dia1!=dia2 && (i!=j&&(i+j)!=n-1)) ||

    if(n>2&&(rows.size()!=2||cols.size()!=2)){
        cout<<-1;
        return 0;
    }
    for(auto &it:rows)tot.insert(it);
    for(auto &it:cols)tot.insert(it);
    if(n==1){
        cout<<1;
    }
    else if(n==2){
        if(tot.size()<=3){
            if(arr[0][0]==0)cout<<arr[1][1];
            else if(arr[1][0]==0)cout<<arr[0][1];
            else if(arr[0][1]==0)cout<<arr[1][0];
            else cout<<arr[0][0];
        }
    }
    else{
        x=row[0];
        fr(i,1,n)if(row[i]!=x){x=abs(x-row[i]);break;}
        arr[idxi][idxj]=x;
        fr(i,0,n)dia1+=arr[i][i];
        fr(i,0,n)dia2+=arr[i][n-1-i];
        fr(i,0,n)row[i]=col[i]=0;
        rows.clear();cols.clear();
        fr(i,0,n){
        fr(j,0,n)
        {col[i]+=arr[j][i];
        row[i]+=arr[i][j];
        cols.insert(col[i]);rows.insert(row[i]);
        }
    }
        if(dia2!=dia1 || cols.size()!=1 || rows.size()!=1)cout<<-1;
        else cout<<x;
    }
}