local mfl = math.floor
local mod = 1000000007
local function badd(x, y) return (x + y) % mod end
local function bmul(x, y)
  local x1, y1 = mfl(x / 31623), mfl(y / 31623)
  local x0, y0 = x - x1 * 31623, y - y1 * 31623
  return (x1 * y1 * 14122 + (x1 * y0 + x0 * y1) * 31623 + x0 * y0) % mod
end
local function lltonumber(str)
  local ret = 0LL
  for i = 1, #str do
    ret = ret * 10LL + str:sub(i, i):byte() - 48
  end
  return ret
end

local s = io.read()
local n, k = s:match("(%d+) (%d+)")
n = tonumber(n)
k = lltonumber(k)
local t = {}
for i = 1, n do
  t[i] = {}
  for j = 1, n do
    t[i][j] = io.read("*n")
  end
end
local cpy = {}
for i = 1, n do
  cpy[i] = {}
  for j = 1, n do
    cpy[i][j] = 0
  end
end
local function update()
  for i = 1, n do
    for j = 1, n do
      cpy[i][j] = 0
    end
  end
  for i = 1, n do
    for j = 1, n do
      for k = 1, n do
        cpy[i][k] = badd(cpy[i][k], bmul(t[i][j], t[j][k]))
      end
    end
  end
  for i = 1, n do
    for j = 1, n do
      t[i][j] = cpy[i][j]
    end
  end
end
local src, dst = {}, {}
for i = 1, n do
  src[i] = 1
  dst[i] = 0
end
while 0LL < k do
  if k % 2LL == 1LL then
    for i = 1, n do
      dst[i] = 0
    end
    for i = 1, n do
      for j = 1, n do
        dst[j] = badd(dst[j], bmul(t[i][j], src[i]))
      end
    end
    for i = 1, n do
      src[i] = dst[i]
    end
  end
  update()
  k = k / 2LL
end
local ret = 0
for i = 1, n do
  ret = badd(ret, src[i])
end
print(ret)
