/*
FORHAD-SUST-BD
*/
#include <iostream>
#include <string>
#include <map>
#include <queue>
#include <stack>
#include <algorithm>
#include <list>
#include <set>
#include <cmath>
#include <cstring>
#include <stdio.h>
#include <string.h>
#include <sstream>
#include <stdlib.h>
#include <vector>
#include <iomanip>
using namespace std;
#ifdef __GNUC__
typedef long long ll;typedef unsigned long long ull;
#else
typedef __int64 ll; typedef unsigned __int64 ull;
#endif
#define INF 1<<28
#define SIZE 100
#define PI 2*acos(0)
#define fg %I64d
#define REP(i,n) for (i=0;i<n;i++)
#define REV(i,n) for (i=n;i>=0;i--)
#define FOR(i,p,k) for (i=p; i<k;i++)
#define FOREACH(it,x) for(__typeof((x).begin()) it=(x.begin()); it!=(x).end(); ++it)
#define bug(x) cout<< "->" <<#x<<": "<<x<<endl
#define Sort(x) sort(x.begin(),x.end())
#define Reverse(x) reverse(x.begin(),x.end())
#define MP(a,b) make_pair(a,b)
#define Clear(x,with) memset(x,with,sizeof(x))
#define Copy(c,r) memcpy(c,r,sizeof(r))
#define SZ(x) (int)x.size()
#define length(x) (int)x.length()
#define All(x) x.begin(),x.end()
#define pb push_back
#define popcount(i) __builtin_popcount(i)
#define gcd(a,b) __gcd(a,b)
#define fs first
#define sc second
#define two(X) (1<<(X))
#define twoL(X) (((ll)(1))<<(X))
#define contain(S,X) (((S)&two(X))!=0)
#define containL(S,X) (((S)&twoL(X))!=0)
typedef pair<int,int> pii;
typedef pair<double,double> pdd;
typedef vector<ll> vi;
typedef vector<double>vd;
typedef vector<ll>vll;
typedef vector<string> vs;
typedef vector<vi>vvi;
typedef vector<vll>vvll;
typedef vector<vd>vvd;
typedef vector<pii>vpii;
typedef map<string,int> msi;
typedef map<int,int>mii;
typedef map<pii,int>mpi;
template<class T> inline T sqr(T x){return x*x;}
template<class T> inline bool isPrime(T n){if(n<=1)return false;for (T i=2;i*i<=n;i++) if (n%i==0) return false;return true;}
template<class T> inline T Mod(T n,T m) {return (n%m+m)%m;} //For Positive Negative No.
template<class T> string toString(T n){ostringstream oss;oss<<n;oss.flush();return oss.str();}
int toInt(string s){int r=0;istringstream sin(s);sin>>r;return r;}
ll toLl(string s){ll r=0;istringstream sin(s); sin>>r; return r;}
template<class T> void debug(const T& e){cout<<e<<endl;}
template<class T1,class T2> void debug(const T1& e1,const T2& e2){cout<<e1<<"\t"<<e2<<endl;}
template<class T1,class T2,class T3> void debug(const T1& e1,const T2& e2,const T3& e3){cout<<e1<<"\t"<<e2<<"\t"<<e3<<endl;}
template<class T1,class T2,class T3,class T4> void debug(const T1& e1,const T2& e2,const T3& e3,const T4& e4){cout<<e1<<"\t"<<e2<<"\t"<<e3<<"\t"<<e4<<endl;}
template<class T1,class T2,class T3,class T4,class T5> void debug(const T1& e1,const T2& e2,const T3& e3,const T4& e4,const T5& e5){cout<<e1<<"\t"<<e2<<"\t"<<e3<<"\t"<<e4<<"\t"<<e5<<endl;}
template<class T> void debug(vector<T>& e){int i;REP(i,SZ(e)) cout<<e[i]<<" ";cout<<endl;}
template<class T> void debug(vector< basic_string<T> >& e){int i,j;REP(i,SZ(e)) {REP(j,SZ(e[i])) cout<<e[i][j];cout<<endl;} cout<<endl;}
template<class T> void debug(vector< vector<T> >& e){int i,j;REP(i,SZ(e)) {REP(j,SZ(e[i])) cout<<e[i][j]<<"\t";cout<<endl;} cout<<endl;}
template<class T> void debug(T e[SIZE][SIZE],int row,int col){int i,j;REP(i,row) {REP(j,col) cout<<e[i][j]<<" ";cout<<endl;}}
ll Pow(ll B,ll P){ ll R=1; while(P>0) {if(P%2==1) R=(R*B);P/=2;B=(B*B);}return R;} //compute b^p
//struct pq{ int cost,node;bool operator<(const pq &b)const{return cost>b.cost;}};// Min Priority Queue
//bool comp(pq a,pq b){ return a.cost > b.cost;} //Comp Sort
//int dx[]={1,0,-1,0};int dy[]={0,1,0,-1}; //4 Direction
//int dx[]={1,1,0,-1,-1,-1,0,1};int dy[]={0,1,1,1,0,-1,-1,-1};//8 direction
//int dx[]={2,1,-1,-2,-2,-1,1,2};int dy[]={1,2,2,1,-1,-2,-2,-1};//Knight Direction
vvi Multiplication(vvi A,vvi B) //Matrix Should be MxN ans NxP. return MxP matrix
{
ll m,n,p,i,j,k;
double val;
m=SZ(A); n=SZ(B); p=SZ(B[0]);
vvi C(m,vi(p));
REP(i,m)
REP(j,p)
REP(k,n)
{
if(C[i][j]<0) continue;
if(A[i][k]==0 || B[k][j]==0) continue;
if(A[i][k]<0 || B[k][j]<0) C[i][j]=-1;
else if(C[i][j]+(double)A[i][k]*(double)B[k][j]>=(twoL(62)*2.0)) C[i][j]=-1;
else C[i][j]+=(A[i][k]*B[k][j]);
}
return C;
}
vvi PowerMat(vvi A,ll steps) //Compute A^steps
{
if(steps==1) return A;
if(steps%2==1) return Multiplication(PowerMat(A,steps-1),A);
vvi T = PowerMat(A,steps/2);
return Multiplication(T,T);
}
class GraphPaths
{
public:
long long howMany(vector <string> graph, int start, int destination, int length)
{
ll i,j,n,v;
n=SZ(graph);
vvi mat(n,vi(n));
REP(i,n)
{
istringstream iss(graph[i]);
while(iss>>v)
{
mat[i][v]=1;
}
}
mat=PowerMat(mat,length);
return mat[start][destination];
}
};
template<typename T> void print( T a ) {
cerr << a;
}
void print( long long a ) {
cerr << a << "L";
}
void print( string a ) {
cerr << '"' << a << '"';
}
template<typename T> void print( vector<T> a ) {
cerr << "{";
for ( int i = 0 ; i != a.size() ; i++ ) {
if ( i != 0 ) cerr << ", ";
print( a[i] );
}
cerr << "}" << endl;
}
template<typename T> void assert_eq( int n, T have, T need ) {
if ( have == need ) {
cerr << "Case " << n << " passed." << endl;
} else {
cerr << "Case " << n << " failed: expected ";
print( need );
cerr << " received ";
print( have );
cerr << "." << endl;
}
}
template<typename T> void assert_eq( int n, vector<T> have, vector<T> need ) {
if( have.size() != need.size() ) {
cerr << "Case " << n << " failed: returned " << have.size() << " elements; expected " << need.size() << " elements.";
print( have );
print( need );
return;
}
for( int i= 0; i < have.size(); i++ ) {
if( have[i] != need[i] ) {
cerr << "Case " << n << " failed. Expected and returned array differ in position " << i << ".";
print( have );
print( need );
return;
}
}
cerr << "Case " << n << " passed." << endl;
}
void assert_eq( int n, string have, string need ) {
if ( have == need ) {
cerr << "Case " << n << " passed." << endl;
} else {
cerr << "Case " << n << " failed: expected ";
print( need );
cerr << " received ";
print( have );
cerr << "." << endl;
}
}
int main( int argc, char* argv[] )
{
GraphPaths objectGraphPaths;
//test case0
vector <string> param00;
param00.push_back("1 2 3");
param00.push_back("0 1 2");
param00.push_back("0");
param00.push_back("1 2");
int param01 = 0;
int param02 = 1;
int param03 = 2;
long long ret0 = objectGraphPaths.howMany(param00,param01,param02,param03);
long long need0 = 2;
assert_eq(0,ret0,need0);
//test case1
vector <string> param10;
param10.push_back("1");
param10.push_back("2");
param10.push_back("3");
param10.push_back("4");
param10.push_back("5");
param10.push_back("0 3");
int param11 = 0;
int param12 = 0;
int param13 = 10000001;
long long ret1 = objectGraphPaths.howMany(param10,param11,param12,param13);
long long need1 = 0;
assert_eq(1,ret1,need1);
//test case2
vector <string> param20;
param20.push_back("1 2");
param20.push_back("0");
param20.push_back("0");
int param21 = 0;
int param22 = 0;
int param23 = 126;
long long ret2 = objectGraphPaths.howMany(param20,param21,param22,param23);
long long need2 = -1;
assert_eq(2,ret2,need2);
//test case3
vector <string> param30;
param30.push_back("0 1 2 3");
param30.push_back("0 1 2 3");
param30.push_back("0 1 2 3");
param30.push_back("0 1 2 3");
int param31 = 2;
int param32 = 2;
int param33 = 63;
long long ret3 = objectGraphPaths.howMany(param30,param31,param32,param33);
long long need3 = -1;
assert_eq(3,ret3,need3);
}