import java.io.*;
import java.util.*;

/**
* ACM Problem #11149 (Matrix Sum)
*
* @author Felix Dietrich
* @version 1.0, 28/07/09
*
* Status : Accepted
* Runtime: 1.016
*/

class Main
{

static BufferedReader reader;
static BufferedReader outputReader;
static boolean useTestData = false;
static String testData = "test.txt";

private static String readline(int maxLg) throws IOException
{
if (useTestData)
return reader.readLine();

byte lin[] = new byte[maxLg];
int lg = 0, car = -1;

try
{
while (lg < maxLg)
{
car = System.in.read();
if ((car < 0) || (car == '\n'))
break;
lin[lg++] += car;
}
}
catch (IOException e)
{
return (null);
}

if ((car < 0) && (lg == 0))
return (null); // eof
return (new String(lin, 0, lg));

}

public static void main(String args[]) throws IOException
{
String input;

if (useTestData)
{
reader = new BufferedReader(new FileReader(testData));
// outputReader = new BufferedReader(new FileReader("output.txt"));
}
else
reader = new BufferedReader(new InputStreamReader(System.in));

while ((input = readline(255)) != null)
{
if (work(input.trim()) == false) // the true entry point
{
return;
}
}
}

// / Code starts here

static boolean firstline = true;
static Map<Integer, Matrix> matrixMap;
static Matrix I;

private static boolean work(String trim) throws IOException
{
if (trim.length() < 3)
return true;

StringTokenizer st = new StringTokenizer(trim);
int n = Integer.parseInt(st.nextToken());
int k = Integer.parseInt(st.nextToken());

if (n == 0)
return false;

matrixMap = new HashMap<Integer, Matrix>();
I = new Matrix(n);
for (int i = 0; i < n * n; i += n + 1)
I.values[i] = 1;

Matrix A = new Matrix(n);
Matrix result = new Matrix(n);

for (int i = 0; i < n; i++)
A.addLine(i, readline(255));

result = findSum(A, k);

System.out.println(result);
System.out.println();
return true;
}

// S[n] = S[n/2] + A^(n/2) * S[n/2]; when n is even
// S[n] = A + A * S[n-1]; when n is odd
private static Matrix findSum(Matrix A, int n)
{
if (n == 0)
return I;
if (n == 1)
return A;
if (matrixMap.containsKey(n))
return matrixMap.get(n);

// odd
if ((n & 1) != 0)
{
Matrix temp = A.add(A.mult(findSum(A, n - 1)));
matrixMap.put(n, temp);
return temp;
}
// even
else
{
Matrix nHalf = findSum(A, n / 2);
Matrix temp = nHalf.add(A.pow(n / 2).mult(nHalf));
matrixMap.put(n, temp);
return temp;
}
}

public static class Matrix
{
long[] values;
int n;

public Matrix(int n)
{
values = new long[n * n];
this.n = n;
}

//A^n = (A^(n/2))2 for even n
//A^n = A * ( A^(n/2) )2 for odd n
public Matrix pow(int i)
{
if (i == 0)
return I;
if (i == 1)
return this;
if (i == 2)
return this.mult(this);
// even
if ((i & 1) == 0)
{
Matrix temp = this.pow(i / 2);
return temp.mult(temp);
}
else
{
Matrix temp = this.pow(i / 2);
return this.mult(temp.mult(temp));
}
}

public void addLine(int index, String readline)
{
StringTokenizer st = new StringTokenizer(readline);
for (int i = index * n; i < index * n + n; i++)
{
values[i] = Integer.parseInt(st.nextToken());
}
}

public Matrix mult(Matrix a)
{
Matrix result = new Matrix(n);

for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
for (int k = 0; k < n; k++)
{
result.values[i * n + j] += (this.values[i * n + k] * a.values[j + k * n]) % 10;
}
}
}

return result;
}

public Matrix add(Matrix a)
{
Matrix result = new Matrix(n);

for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
result.values[i * n + j] = (this.values[i * n + j] + a.values[i * n + j]);
}
}

return result;
}

@Override
public String toString()
{
StringBuilder sb = new StringBuilder();
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
sb.append(values[i * n + j] % 10);
if (j < n - 1)
sb.append(" ");
}
sb.append("\n");
}
return sb.toString().trim();
}
}

}