#include <iostream>
#include <sstream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <cstring>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <bitset>
#include <numeric>
#include <utility>
#include <iomanip>
#include <algorithm>
#include <functional>
using namespace std;

typedef long long ll;
typedef vector<int> vint;
typedef vector<long long> vll;
typedef pair<int,int> pint;
typedef pair<long long, long long> pll;

#define MP make_pair
#define PB push_back
#define ALL(s) (s).begin(),(s).end()
#define EACH(i, s) for (__typeof__((s).begin()) i = (s).begin(); i != (s).end(); ++i)
#define COUT(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
template<class T1, class T2> ostream& operator << (ostream &s, pair<T1,T2> P) 
{ return s << '<' << P.first << ", " << P.second << '>'; }
template<class T> ostream& operator << (ostream &s, vector<T> P) 
{ for (int i = 0; i < P.size(); ++i) { if (i > 0) { s << " "; } s << P[i]; } return s; }
template<class T> ostream& operator << (ostream &s, vector<vector<T> > P) 
{ for (int i = 0; i < P.size(); ++i) { s << endl << P[i]; } return s << endl; }
template<class T1, class T2> ostream& operator << (ostream &s, map<T1,T2> P) 
{ EACH(it, P) { s << "<" << it->first << "->" << it->second << "> "; } return s; }



/*
今回は、常に、
a側: Win or Draw
b側: Lose or Draw
がわかっているので, Win or Loseの判定は要らない -> adp, bdpの初期化が-INF, INFでよい. 
*/

const int INF = 1<<29;
int CASE;
int n, sa, sb;
int G[55][55];

int adp[55][55];
int bdp[55][55];
bool aknown[55][55];
bool bknown[55][55];

int solve() {
    cin >> n;
    for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j) cin >> G[i][j];

    cin >> sa >> sb;
    for (int i = 0; i < 55; ++i) for (int j = 0; j < 55; ++j) adp[i][j] = -1, bdp[i][j] = -1;
    for (int i = 0; i < 55; ++i) for (int j = 0; j < 55; ++j) if (G[i][j] || i == j) bdp[i][j] = 1;
    
    int LIM = n*n + 1;
    bool update = false;
    for (int ite = 0; ite <= LIM; ++ite) {
        update = false;
        
        // A's turn
        for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j) {
            if (i == j) continue;
            int Max = -INF;
            bool unknown = false;
            for (int k = 0; k < n; ++k) {
                if (G[i][k] == 0 && i != k) continue;
                if (bdp[k][j] == -1) { unknown = true; continue; }
                chmax(Max, bdp[k][j] + 1);
            }
            if (Max > -INF && !unknown) {
                if (adp[i][j] == -1) { adp[i][j] = Max; update = true; }
                else if (chmax(adp[i][j], Max)) update = true;
            }
        }
        
        // B's tuen
        for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j) {
            if (i == j) continue;
            int Min = INF;
            for (int k = 0; k < n; ++k) {
                if (G[j][k] == 0) continue;
                if (adp[i][k] == -1) { continue; }
                chmin(Min, adp[i][k] + 1);
            }
            if (Min < INF) {
                if (bdp[i][j] == -1) { bdp[i][j] = Min; update = true; }
                else if (chmin(bdp[i][j], Min)) update = true;
            }
        }

        if (!update) break;
    }

    if (adp[sa][sb] == -1) return -1;
    else return adp[sa][sb]/2;
}

int main() {
    cin >> CASE;
    for (int AAA = 0; AAA < CASE; ++AAA) {
        int res = solve();
        if (res != -1) cout << res << endl;
        else puts("infinity");
    }
    return 0;
}


Measured memory_bytes: 1200