#include <iostream>
using namespace std;

#define ll long long int
#define MD 1000000007

int N;
ll A[55][55], R[55][55], H[55][55];

void mul(ll M1[][55], ll M2[][55])
{
    for (int i = 0; i < N; i++) for (int j = 0; j < N; j++)
    {
        H[i][j] = 0;
        for (int k = 0; k < N; k++) H[i][j] = (H[i][j] + M1[i][k] * M2[k][j]) % MD;
    }
    for (int i = 0; i < N; i++) for (int j = 0; j < N; j++) M1[i][j] = H[i][j];
}

void fastpow(ll p)
{
    while (p)
    {
        if (p % 2) mul(R, A);
        mul(A, A);
        p /= 2;
    }
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ll K;
    cin >> N >> K;
    for (int i = 0; i < N; i++)
    {
        R[i][i] = 1;
        for (int j = 0; j < N; j++) cin >> A[i][j];
    }
    fastpow(K);
    ll res = 0;
    for (int i = 0; i < N; i++) for (int j = 0; j < N; j++) res = (res + R[i][j]) % MD;
    cout << res << '\n';
    return(0);
}