/**
 * code generated by JHelper
 * More info: https://github.com/AlexeyDmitriev/JHelper
 * @author P.Alizadeh
 */

#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>

using namespace std;
using namespace __gnu_pbds;

template <class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

typedef long long int   ll;
typedef long double     ld;
typedef pair<ll,ll>     pll;
typedef complex<ld>     cld;

#define all(x)          x.begin(),x.end()
#define Sort(x)         sort(all(x))
#define kill(x)         return cout << x << endl, 0;
#define Mp              make_pair
#define X               first
#define Y               second
#define sep             " "
#define endl            "\n"
#define fastio          ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define file_io         freopen("in.txt" , "r" , stdin) ; freopen("out.txt" , "w" , stdout);
#define ld_io           cout << setprecision(10) << fixed;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

ll pw(ll a, ll b, ll md) {
    if (!b)
        return 1;
    if (b & 1)
        return a * pw(a * a % md, b / 2, md) % md;
    return pw(a * a % md, b / 2, md) % md;
}

const ll MAXN = 2e5 + 10,
         INF  = 8e18,
         MOD  = 1e9 + 7,
         LOG  = 32;
//const ld PI   = M_PI;

template<class T> T inverse(T a, T b) {
    a %= b; if (a == 0) return b == 1 ? 0 : -1;
    T x = inverse(b, a);
    return x == -1 ? -1 : ((1 - (ll)b * x) / a + b) % b;
}

template <class T>
struct modular {
    T val;
    ll mod;
    explicit operator T() { return val; }
    modular() : val(0) {}
    template <class U> modular(const U& x, ll md = MOD) : mod(md) {
        val = (-mod < x && x < mod) ? x : x % mod;
        val = (val < 0) ? val + mod : val;
    }
    friend ostream& operator<<(ostream& out, const modular& m) { return out << m.val; }
    friend bool operator==(const modular& a, const modular& b) { return a.val == b.val; }
    friend bool operator!=(const modular& a, const modular& b) { return !(a.val == b.val); }
    friend bool operator< (const modular& a, const modular& b) { return a.val < b.val; }

    modular& operator-() { return modular(-val); }
    modular& operator+=(const modular& m) { if ((val += m.val) >= mod) val -= mod; return *this; }
    modular& operator-=(const modular& m) { if ((val -= m.val) < 0) val += mod; return *this; }
    modular& operator*=(const modular& m) { val = val * m.val % mod ; return *this; }

    friend modular pw(const modular& m, ll b) { return pw(m.val, b, m.mod); }
    modular inv() { return inverse(val, mod); }
    modular& operator/=(const modular& m) { return (*this) * m.inv(); }

    friend modular operator+(modular a, const modular& b) { return a += b; }
    friend modular operator-(modular a, const modular& b) { return a -= b; }
    friend modular operator*(modular a, const modular& b) { return a *= b; }
    friend modular operator/(modular a, const modular& b) { return a /= b; }

};

typedef modular<ll> mll;

template <class T> struct Mat {
    // n-rows m-columns
    ll n, m;
    vector<vector<T>> mat;

    Mat() : n(0), m(0) {}
    Mat(ll _n, ll _m, ll init=0) : n(_n), m(_m) { mat.assign(n, vector<T>(m, init)); }
    Mat(const vector<vector<T>>& v) : n(v.size()), m(v[0].size) { mat = v; }

    template <class U>
    Mat(const initializer_list<initializer_list<U>>& l) {
        n = l.size(), m = (*l.begin()).size();
        mat.assign(n, vector<T>(m));
        auto u = l.begin();
        for (ll i = 0; i < n; i++, u++) {
            auto v = (*u).begin();
            for (ll j = 0; j < m; j++, v++)
                mat[i][j] = *v;
        }
    }

    friend ostream& operator<<(ostream& out, const Mat& a) {
        for (ll i = 0; i < a.n; i++) {
            for (ll j = 0; j < a.m; j++) {
                out << a.mat[i][j] << sep;
            }
            out << endl;
        }
        return out;
    }

    Mat& operator+=(const Mat& a) {
        assert(n == a.n && m == a.m);
        for (ll i = 0; i < n; i++)
            for (ll j = 0; j < n; j++)
                mat[i][j] += a.mat[i][j];
        return *this;
    }

    Mat& operator-=(const Mat& a) {
        assert(n == a.n && m == a.m);
        for (ll i = 0; i < n; i++)
            for (ll j = 0; j < n; j++)
                mat[i][j] -= a.mat[i][j];
        return *this;
    }


    Mat operator*(const Mat& a) {
        assert(m == a.n);
        Mat res(n, a.m);
        for (ll i = 0; i < n; i++)
            for (ll j = 0; j < a.m; j++)
                for (ll k = 0; k < m; k++)
                    res.mat[i][j] += mat[i][k] * a.mat[k][j];
        return res;
    }

    Mat operator+(const Mat& a) { return Mat(*this) += a; }
    Mat operator-(const Mat& a) { return Mat(*this) -= a; }
    Mat& operator*=(const Mat& b) { return *this = (*this) * b; }

    Mat operator^(ll p) {
        Mat res(n, m), t(*this);
        for (ll i = 0; i < n; i++)
            res.mat[i][i] = 1;
        while (p) {
            if (p & 1)  res = res * t;
            t = t * t;
            p >>= 1;
        }
        return res;
    }
};

ll n, k, x;
mll ans = 0;

int main() {
    fastio;
    //ld_io;

    cin >> n >> k;
    Mat<mll> a(n, n);

    for (ll i = 0; i < n; i++) {
        for (ll j = 0; j < n; j++) {
            cin >> x;
            a.mat[i][j] = x;
        }
    }

    a = a ^ k;

    for (ll i = 0; i < n; i++) {
        for (ll j = 0; j < n; j++) {
            ans += a.mat[i][j];
        }
    }

    cout << ans << endl;

    return 0;
}



Measured memory_bytes: 384