import re
import sys

import numpy as np

stdin:str = sys.stdin.read()

lines = list(map(int,re.split(r"\s+",stdin.strip())))

def gn(now={
    "num":0
}):
    now["num"]=now["num"]+1
    return lines[now["num"]-1]


n = gn()
k= gn()

ma = np.zeros((n,n))
for i in range(n):
    for j in range(n):
        ma[i][j] = gn()
        # print(ma[i][j])

res = np.identity(n)

for i in range(k):
    res = res@ma

for i in range(n):
    for j in range(n):
        print(f'{int(res[i][j]%(1e9+7))} ',end='')
    print()