#include <iostream>
#include <cstring>
using namespace std;
#define INF (1<<25)
int dp[60][50][50][2] = {};
int g[50][50] = {};
int n,m;

int dfs(int mid,int a,int b,int t){
	if(t == 0 && a == b) return -INF;
	if(dp[mid][a][b][t] != -1) return dp[mid][a][b][t];
	if(mid == 0){
		//cout << a << " " << b << " " << t << endl;
		return 0;
	}
	if(t==0){
		int ans = -INF;
		for(int i = 0 ; i < n ; i++)
			if(g[a][i]) ans = max(ans,dfs(mid,i,b,t^1));
		return dp[mid][a][b][t] = ans;
	}else{
		int ans = INF;
		for(int i = 0 ; i < n ; i++)
			if(g[b][i]) ans = min(ans,dfs(mid-1,a,i,t^1)+1);
		return dp[mid][a][b][t] = ans;
	}
}
int main(){
	int T; cin >> T;
	while(T--){
		cin >> n;
		int a,b;
		for(int i = 0 ; i < n ; i++)
			for(int j = 0 ; j < n ; j++)
				cin >> g[i][j];
		for(int i = 0 ; i < n ; i++)
			g[i][i] = 1;
		cin >> a >> b;
		
		memset(dp,-1,sizeof(dp));
		int l = 0 , r = 52;
		while(l!=r){
			int mid = (l+r+1) / 2;
			if( dfs(mid,a,b,0) >= 0 ) l = mid;
			else r = mid-1;
		}
		if(l==52) cout << "infinity" << endl;
		else cout << l+1 << endl;
	}
}