// Slower C++ solution:
#include<bits/stdc++.h>

#define ll long long int

#define mp make_pair

#define pb push_back

#define ff first 

#define ss second

#define mod 1000000007

#define lim 100005

#define nfs_mw ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

using namespace std;



int n;



struct matrix{

	ll mat[51][51] = {};

	

	matrix(const matrix &m) {

	int i,j;

	for(i=0; i<51; i++)

		for(j=0; j<51; j++)

			mat[i][j] = m.mat[i][j];

	}

	

	matrix(){

	};

};





void print(matrix m){

	for(int i=0; i<n; i++){

		for(int j=0; j<n; j++)

			cout<<m.mat[i][j]<<" ";

		cout<<endl;

	}

	cout<<endl;

}



matrix identity()

{

	matrix A;

	for(int i=0; i<n; i++) A.mat[i][i] = 1;

	return A;

}



//gets A B

matrix mul(matrix A, matrix B){

	matrix C;

	for(int i=0; i<n; i++)

		for(int j=0; j<n; j++)

			for(int k=0; k<n; k++)

				C.mat[i][j] = (C.mat[i][j] + ((A.mat[i][k]*B.mat[k][j])%mod))%mod;

	return C;

}





matrix bin_expo(matrix B, ll e)

{

	matrix out = identity();

	while(e)

	{

		if(e&1) out = mul(out, B);

		B = mul(B, B);

		e>>=1;

	}

	return out;

}



int main()

{

	int i,j,v; ll k, ans = 0;

	matrix inp;

	cin>>n>>k;

	for(i=0; i<n; i++)

		for(j=0; j<n; j++){

			cin>>v;

			inp.mat[i][j] = v;

		}



	matrix raised = bin_expo(inp, k);



	for(i=0; i<n; i++) for(j=0; j<n; j++) ans = (ans + raised.mat[i][j])%mod;

	cout<<ans;

}

// Faster C++ solution for the same problem (measured speedup: 3.0900467740883166x):
#include <bits/stdc++.h>

#define ull uint64_t

#define ll long long int

#define pb push_back

#define mp make_pair

#define ff first

#define ss second

#define mx5 100005

#define mx6 1000005

#define mod 1000000007

#define nfs ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

using namespace std;



class SqMatrix {

    private:

    int dim;

    vector<vector<ll>> mat;

    

    public:

    /* Identity Matrix*/

    SqMatrix(int n) {

        dim = n;

        mat = vector<vector<ll>>(dim, vector<ll>(dim, 0));

        for (int i = 0; i < n; i++)

            mat[i][i] = 1;

    }



    void gedit() {

        for (int i = 0; i < dim; i++)

            for (int j = 0; j < dim; j++)

                cin >> mat[i][j];

    }



    SqMatrix operator * (SqMatrix const &obj) { 

        assert(dim == obj.dim);

        SqMatrix res(dim); 

        for (int i = 0; i < dim; i++)

            for (int j = 0; j < dim; j++)

                for (int k = 0; k < dim; k++)

                    res.mat[i][k] = (res.mat[i][k] + (mat[i][j] * obj.mat[j][k]) % mod) % mod;

        return res;

    }



    SqMatrix operator *= (SqMatrix const & obj) {

        assert(dim == obj.dim);

        vector<vector<ll>> mat2(dim, vector<ll>(dim, 0));

        for (int i = 0; i < dim; i++)

            for (int j = 0; j < dim; j++)

                for (int k = 0; k < dim; k++)

                    mat2[i][k] = (mat2[i][k] + (mat[i][j] * obj.mat[j][k]) % mod) % mod;

        mat = mat2;

        return *this;

    }



    SqMatrix raise(ll k) {

        SqMatrix tmp = *this;

        SqMatrix res = SqMatrix(dim);

        while (k) {

            if (k & 1) {

                res *= tmp;

            }

            tmp *= tmp;

            k /= 2;

        }

        return res;

    }



    ll sum() {

        ll out = 0;

        for (int i = 0; i < dim; i++)

            for (int j = 0; j < dim; j++)

                out = (out + mat[i][j]) % mod;

        return out;

    }



    void print() {

        for (int i = 0; i < dim; i++) {

            for (int j = 0; j < dim; j++) {

                cout << mat[i][j] << ' ';

            }

            cout << '\n';

        }

    }

};



int main() {

    nfs

    int n;

    ll k;

    cin >> n >> k;

    SqMatrix adjMat(n);

    adjMat.gedit();

    SqMatrix res = adjMat.raise(k);

    cout << res.sum();

}