using System;
using System.Collections.Generic;
using System.IO;
using System.Threading;

namespace Contest
{

    class Program
    {
        private const long Mod = (long)1e9 + 7;
        private Scanner sc;
        private int N;
        private long K;
        private List<int>[] E;

        private bool[,] A;
        // 長さ2^i start j end k DPLog[i,j,k]
        private long[,,] DPLog;

        public void Solve()
        {
            sc = new Scanner();
            N = sc.NextInt();
            K = sc.NextLong();
            E = new List<int>[N];
            A = new bool[N, N];
            for (int i = 0; i < N; i++)
            {
                E[i] = new List<int>();
            }
            for (int i = 0; i < N; i++)
            {
                var ar = sc.Array();
                for (int j = 0; j < N; j++)
                {
                    if (ar[j] == "1")
                        E[i].Add(j);
                    A[i, j] = ar[j] == "1";
                }
            }

            DPLog = new long[64, N, N];

            for (int i = 0; i < 64; i++)
            {
                for (int start = 0; start < N; start++)
                {
                    if (i == 0)
                    {
                        foreach (var i1 in E[start])
                        {
                            DPLog[i, start, i1] = 1;
                        }
                    }
                    else
                    {
                        for (int end = 0; end < N; end++)
                        {
                            for (int j = 0; j < N; j++)
                            {
                                DPLog[i, start, end] += (DPLog[i - 1, start, j] * DPLog[i - 1, j, end]) % Mod;
                                DPLog[i, start, end] %= Mod;
                            }
                        }
                    }
                }
            }


            //for (int j = 0; j < N; j++)
            //{
            //    for (int k = 0; k < N; k++)
            //    {
            //        Console.Write(DPLog[1, j, k]);
            //        Console.Write(" ");
            //    }
            //    Console.WriteLine();

            //}

            var dp = new long[N, N];
            for (int i = 0; i < N; i++)
            {
                dp[i, i] = 1;
            }
            long b = 0;
            while (K > 0)
            {
                if (K % 2 == 1)
                {
                    var next = new long[N, N];
                    for (int start = 0; start < N; start++)
                    {
                        for (int end = 0; end < N; end++)
                        {
                            for (int i = 0; i < N; i++)
                            {
                                next[start, end] += (dp[start, i] * DPLog[b, i, end]) % Mod;
                                next[start, end] %= Mod;
                            }
                        }
                    }

                    dp = next;
                }

                b++;
                K /= 2;
            }

            long ans = 0;
            for (int i = 0; i < N; i++)
            {
                for (int j = 0; j < N; j++)
                {
                    ans += dp[i, j];
                    ans %= Mod;
                }
            }
            Console.WriteLine(ans);
        }


        static void Main() => new Program().Solve();
    }
}

class Scanner
{
    public Scanner()
    {
        _stream = new StreamReader(Console.OpenStandardInput());
        _pos = 0;
        _line = new string[0];
        _separator = ' ';
    }
    private readonly char _separator;
    private readonly StreamReader _stream;
    private int _pos;
    private string[] _line;
    #region get a element
    public string Next()
    {
        if (_pos >= _line.Length)
        {
            _line = _stream.ReadLine().Split(_separator);
            _pos = 0;
        }
        return _line[_pos++];
    }
    public int NextInt()
    {
        return int.Parse(Next());
    }
    public long NextLong()
    {
        return long.Parse(Next());
    }
    public double NextDouble()
    {
        return double.Parse(Next());
    }
    #endregion
    #region convert array
    private int[] ToIntArray(string[] array)
    {
        var result = new int[array.Length];
        for (int i = 0; i < array.Length; i++)
        {
            result[i] = int.Parse(array[i]);
        }

        return result;
    }
    private long[] ToLongArray(string[] array)
    {
        var result = new long[array.Length];
        for (int i = 0; i < array.Length; i++)
        {
            result[i] = long.Parse(array[i]);
        }

        return result;
    }
    private double[] ToDoubleArray(string[] array)
    {
        var result = new double[array.Length];
        for (int i = 0; i < array.Length; i++)
        {
            result[i] = double.Parse(array[i]);
        }

        return result;
    }
    #endregion
    #region get array
    public string[] Array()
    {
        if (_pos >= _line.Length)
            _line = _stream.ReadLine().Split(_separator);
        _pos = _line.Length;
        return _line;
    }
    public int[] IntArray()
    {
        return ToIntArray(Array());
    }
    public long[] LongArray()
    {
        return ToLongArray(Array());
    }
    public double[] DoubleArray()
    {
        return ToDoubleArray(Array());
    }
    #endregion
}

Measured memory_bytes: 13408