// Author: Tyler Wang

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cctype>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdint>
#include <cstdlib>
#include <deque>
#include <functional>
#include <initializer_list>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <type_traits>
#include <utility>
#include <vector>

using namespace std;

template <int Modulus>
class mod_int {
 public:
  constexpr mod_int() : val_(0) {}

  template <typename T>
  constexpr mod_int(T val) : val_((int)(val % Modulus)) {
    if (val_ < 0) {
      val_ += Modulus;
    }
  }

  static constexpr int modulus() { return Modulus; }

  template <typename T>
  constexpr explicit operator T() const {
    return (T)val_;
  }

  constexpr mod_int& operator++() {
    if (++val_ == Modulus) {
      val_ = 0;
    }
    return *this;
  }

  constexpr mod_int operator++(int) {
    mod_int t = *this;
    ++*this;
    return t;
  }

  constexpr mod_int& operator--() {
    if (val_ == 0) {
      val_ = Modulus;
    }
    --val_;
    return *this;
  }

  constexpr mod_int operator--(int) {
    mod_int t = *this;
    --*this;
    return t;
  }

  constexpr mod_int operator+=(mod_int other) {
    if ((val_ += other.val_) >= Modulus) {
      val_ -= Modulus;
    }
    return *this;
  }

  constexpr mod_int operator-=(mod_int other) {
    if ((val_ -= other.val_) < 0) {
      val_ += Modulus;
    }
    return *this;
  }

  constexpr mod_int operator*=(mod_int other) {
    val_ = (int)((long long)val_ * other.val_ % Modulus);
    return *this;
  }

  constexpr mod_int operator/=(mod_int other) { return *this *= inv(other); }

  friend constexpr mod_int operator+(mod_int mi) { return mi; }

  friend constexpr mod_int operator-(mod_int mi) {
    if (mi.val_ != 0) {
      mi.val_ = Modulus - mi.val_;
    }
    return mi;
  }

  friend constexpr mod_int operator+(mod_int lhs, mod_int rhs) {
    return lhs += rhs;
  }

  friend constexpr mod_int operator-(mod_int lhs, mod_int rhs) {
    return lhs -= rhs;
  }

  friend constexpr mod_int operator*(mod_int lhs, mod_int rhs) {
    return lhs *= rhs;
  }

  friend constexpr mod_int operator/(mod_int lhs, mod_int rhs) {
    return lhs /= rhs;
  }

  friend constexpr bool operator==(mod_int lhs, mod_int rhs) {
    return lhs.val_ == rhs.val_;
  }

  friend constexpr bool operator!=(mod_int lhs, mod_int rhs) {
    return lhs.val_ != rhs.val_;
  }

  friend constexpr bool operator<(mod_int lhs, mod_int rhs) {
    return lhs.val_ < rhs.val_;
  }

  friend constexpr bool operator<=(mod_int lhs, mod_int rhs) {
    return lhs.val_ <= rhs.val_;
  }

  friend constexpr bool operator>(mod_int lhs, mod_int rhs) {
    return lhs.val_ > rhs.val_;
  }

  friend constexpr bool operator>=(mod_int lhs, mod_int rhs) {
    return lhs.val_ >= rhs.val_;
  }

  friend std::ostream& operator<<(std::ostream& os, mod_int mi) {
    return os << mi.val_;
  }

  friend std::istream& operator>>(std::istream& is, mod_int& mi) {
    long long val;
    is >> val;
    mi = mod_int(val);
    return is;
  }

  friend constexpr mod_int inv(mod_int mi) {
    int a = mi.val_, b = Modulus;
    int x = 0, xx = 1;
    while (a != 0) {
      int q = b / a;
      int t = a;
      a = b - q * a;
      b = t;
      t = xx;
      xx = x - q * xx;
      x = t;
    }
    return mod_int(x);
  }

  template <typename T>
  friend constexpr mod_int pow(mod_int base, T expo) {
    if (expo < 0) {
      base = inv(base);
      expo = -expo;
    }
    mod_int power = 1;
    for (; expo != 0; expo >>= 1) {
      if (expo & 1) {
        power *= base;
      }
      base *= base;
    }
    return power;
  }

 private:
  int val_;
};

constexpr int MODULUS = 1'000'000'007;
using m_int = mod_int<MODULUS>;

template <typename T>
class matrix {
 public:
  matrix(int m, int n, const T& val = T()) : m_(m), n_(n), data_(m * n, val) {}

  matrix(std::initializer_list<std::initializer_list<T>> data)
      : m_((int)data.size()), n_((int)data.begin()->size()) {
    data_.reserve(m_ * n_);
    for (const std::initializer_list<T>& row : data) {
      assert((int)row.size() == n_);
      std::copy(row.begin(), row.end(), std::back_inserter(data_));
    }
  }

  static matrix identity(int n) {
    matrix mat(n, n);
    for (int i = 0; i < n; ++i) {
      mat[i][i] = 1;
    }
    return mat;
  }

  int size1() const { return m_; }

  int size2() const { return n_; }

  T* operator[](int i) { return data_.data() + i * n_; }

  const T* operator[](int i) const { return data_.data() + i * n_; }

  matrix& operator+=(const matrix& other) {
    assert(m_ == other.m_ && n_ == other.n_);
    for (int i = 0; i < (int)data_.size(); ++i) {
      data_[i] += other.data_[i];
    }
    return *this;
  }

  matrix& operator-=(const matrix& other) {
    assert(m_ == other.m_ && n_ == other.n_);
    for (int i = 0; i < (int)data_.size(); ++i) {
      data_[i] -= other.data_[i];
    }
    return *this;
  }

  matrix& operator*=(const matrix& other) { return *this = *this * other; }

  matrix& operator*=(const T& val) {
    for (T& d : data_) {
      d *= val;
    }
    return *this;
  }

  matrix& operator/=(const T& val) {
    for (T& d : data_) {
      d /= val;
    }
    return *this;
  }

  friend matrix operator+(matrix lhs, const matrix& rhs) { return lhs += rhs; }

  friend matrix operator-(matrix lhs, const matrix& rhs) { return lhs -= rhs; }

  friend matrix operator*(const matrix& lhs, const matrix& rhs) {
    assert(lhs.n_ == rhs.m_);
    matrix prod(lhs.m_, rhs.n_);
    for (int i = 0; i < lhs.m_; ++i) {
      for (int j = 0; j < rhs.n_; ++j) {
        for (int k = 0; k < lhs.n_; ++k) {
          prod[i][j] += lhs[i][k] * rhs[k][j];
        }
      }
    }
    return prod;
  }

  friend matrix operator*(matrix lhs, const T& val) { return lhs *= val; }

  friend matrix operator*(const T& val, matrix rhs) { return rhs *= val; }

  friend matrix operator/(matrix lhs, const T& val) { return lhs /= val; }

  friend bool operator==(const matrix& lhs, const matrix& rhs) {
    return lhs.m_ == rhs.m_ && lhs.data_ == rhs.data_;
  }

  friend bool operator!=(const matrix& lhs, const matrix& rhs) {
    return !(lhs == rhs);
  }

  friend std::ostream& operator<<(std::ostream& os, const matrix& mat) {
    os << '{';
    for (int i = 0; i < mat.m_; ++i) {
      if (i != 0) {
        os << ", ";
      }
      os << '{';
      for (int j = 0; j < mat.n_; ++j) {
        if (j != 0) {
          os << ", ";
        }
        os << mat[i][j];
      }
      os << '}';
    }
    return os << '}';
  }

  template <typename U>
  friend matrix pow(matrix base, U expo) {
    matrix power = identity(base.m_);
    for (; expo > 0; expo >>= 1) {
      if (expo & 1) {
        power *= base;
      }
      base *= base;
    }
    return power;
  }

 private:
  int m_, n_;
  std::vector<T> data_;
};

int main_ac() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  std::cout.precision(12);
  std::cerr.precision(12);
  std::mt19937 rng((std::uint_fast32_t)std::chrono::high_resolution_clock::now()
                       .time_since_epoch()
                       .count());
  constexpr int LOG = 62;
  int n;
  long long k;
  cin >> n >> k;
  vector<vector<vector<m_int>>> dp(LOG,
                                   vector<vector<m_int>>(n, vector<m_int>(n)));
  for (int u = 0; u < n; ++u) {
    for (int v = 0; v < n; ++v) {
      bool b;
      cin >> b;
      dp[0][u][v] = b;
    }
  }
  for (int i = 1; i < LOG; ++i) {
    for (int u = 0; u < n; ++u) {
      for (int v = 0; v < n; ++v) {
        for (int w = 0; w < n; ++w) {
          dp[i][u][v] += dp[i - 1][u][w] * dp[i - 1][w][v];
        }
      }
    }
  }
  vector<vector<m_int>> dp2;
  for (int i = 0; i < LOG; ++i) {
    if (k & (1LL << i)) {
      if (dp2.empty()) {
        dp2 = dp[i];
        continue;
      }
      vector<vector<m_int>> next_dp2(n, vector<m_int>(n));
      for (int u = 0; u < n; ++u) {
        for (int v = 0; v < n; ++v) {
          for (int w = 0; w < n; ++w) {
            next_dp2[u][v] += dp2[u][w] * dp[i][w][v];
          }
        }
      }
      dp2 = move(next_dp2);
    }
  }
  m_int ans;
  for (int u = 0; u < n; ++u) {
    for (int v = 0; v < n; ++v) {
      ans += dp2[u][v];
    }
  }
  cout << ans << '\n';
  return 0;
}

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  std::cout.precision(12);
  std::cerr.precision(12);
  std::mt19937 rng((std::uint_fast32_t)std::chrono::high_resolution_clock::now()
                       .time_since_epoch()
                       .count());
  int n;
  long long k;
  cin >> n >> k;
  matrix<m_int> mat(n, n);
  for (int u = 0; u < n; ++u) {
    for (int v = 0; v < n; ++v) {
      bool b;
      cin >> b;
      mat[u][v] = b;
    }
  }
  mat = pow(mat, k);
  m_int ans;
  for (int u = 0; u < n; ++u) {
    for (int v = 0; v < n; ++v) {
      ans += mat[u][v];
    }
  }
  cout << ans << '\n';
  return 0;
}

Measured memory_bytes: 256