#include<bits/stdc++.h>
using namespace std;
typedef struct node
{
	int a[51][51];
	int scale;
	struct node operator *(const struct node & tmp)
	{
		struct node ret;
		int i,j,k;
		ret.scale=scale;
		memset(ret.a,0,sizeof(ret.a));
		for(k=1;k<=scale;k++)
			for(i=1;i<=scale;i++)
				for(j=1;j<=scale;j++)
					if(a[i][k]&&tmp.a[k][j])
						ret.a[i][j]=(ret.a[i][j]+(long long)a[i][k]*tmp.a[k][j])%1000000007;
		return ret;
	}
}NODE,*PNODE;
NODE g,ans;
int main()
{
	long long n,k,i,j,answer=0,flag=0;
	scanf("%d %lld",&n,&k);
	g.scale=n;
	for(i=1;i<=n;i++)
	{
		for(j=1;j<=n;j++)
		{
			scanf("%d",&g.a[i][j]);
		}
	}
	while(k)
	{
		if(k%2)
		{
			if(!flag)
			{
				ans=g;
				flag=1;	
			}
			else
				ans=ans*g;
		}
		g=g*g;
		k/=2;
	}
	for(i=1;i<=n;i++)
	{
		for(j=1;j<=n;j++)
		{
			answer=(answer+ans.a[i][j])%1000000007;
		}
	}
	printf("%lld",answer);
}