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

#ifndef bhartiya
    // #pragma GCC optimize("Ofast")
    // #pragma GCC optimize("unroll-loops")
    // #pragma GCC target ("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#endif

typedef long long ll;
typedef pair<ll, ll> pll;
// not that imp
typedef pair<pll, ll> plll;
typedef vector<ll> vl;
typedef vector<pll> vll;

#define pb push_back
#define bitc  __builtin_popcountl
#define mp make_pair
#define ff first
#define ss second

#define swap(a,b) {a=a^b;b=a^b;a=a^b;}
#define fr(i,a,b) for (ll i = (a), _b = (b); i <= _b; i++)
#define rep(i,n) for (ll i = 0, _n = (n); i < _n; i++)
#define repr(i,n) for (ll i = n - 1; i >= 0; i--)
#define frr(i,a,b) for (ll i = (a), _b = (b); i >= _b; i--)

#ifdef bhartiya
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
	cout << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
	const char* comma = strchr(names + 1, ',');cout.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
#define start_routine() int begtime = clock();
#define end_routine() int endtime = clock(); \
					  cout << "\n\nTime elapsed: " << (endtime - begtime)*1000/CLOCKS_PER_SEC << " ms\n\n";
}
#else
#define endl '\n'
#define trace(...)
#define start_routine()
#define end_routine()
#endif

#define inf 200000000000000ll
#define mod 1000000007ll
#define eps 1e-7

ll gcd(ll a, ll b){ return b==0 ? a : gcd(b, a%b); }
const ll maxn = 51, maxj = 60;
ll n,k;
ll dp[maxn][maxn][maxj+2];
ll cont[maxn][maxj+2];
map<pll, ll> rec;
ll recur(ll s, ll k){
    ll ans = 0;
    // trace(s,k);
    if(k==0) return 1;
    if(rec.find(mp(s,k)) != rec.end()) return rec[mp(s,k)];
    frr(i,maxj,0){
        if(k >= (1ll << i)){
            // trace(s,i,k,ans);
            ll pos = k-(1ll<<i);
            rep(j,n){
                if(dp[s][j][i])
                    ans = (ans+ (dp[s][j][i] * recur(j,pos))%mod)%mod;
            }
            rec[mp(s,k)] = ans;
            return ans;
        }
    }
}

int main()
{ 
    start_routine();
	ios_base::sync_with_stdio(false);
    cin.tie(NULL); cout.precision(10);
    cin.exceptions(cin.failbit);
    #ifdef bhartiya
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    #endif
    int t = 1;
    // cin>>t;
    while(t--){
        cin>>n>>k;
        rep(i,n){
            rep(j,n){
                cin>>dp[i][j][0];
            }
        }
        // rep(i,n){
        //     rep(j,n){
        //         trace(dp[i][j][0]);
        //     }
        // }
        fr(j,1,maxj-1){
            rep(i,n){
                rep(k,n){
                    rep(l,n){
                        dp[i][l][j] =(dp[i][l][j] + (dp[i][k][j-1]*dp[k][l][j-1])%mod)%mod;
                        // trace(i,l,j,dp[i][l][j]);
                    }
                }
            }
            // trace(j, dp[0][1][0]);
        }
        // rep(i,n){
        //     rep(j,n){
        //         trace(dp[i][j][0]);
        //     }
        // }
        fr(j,0,maxj){
            rep(i,n){
                rep(k,n){
                    // trace(cont[i][j], dp[i][k][j], j);
                    cont[i][j] = (cont[i][j] + dp[i][k][j]);
                }
            }
        }
        // rep(i,n){
        //     trace(cont[i][0]);
        // }
        ll ans = 0;
        rep(i,n){
            // trace(i,ans,k);
            ans = (ans + recur(i,k))%mod;
        }
        cout<<ans;
    }
    end_routine();
}

Measured memory_bytes: 1664