import java.util.Scanner;


public class Main {
	static class matrix{
		long[][] mat;
		matrix() {
			mat = new long[40][40];
		}
		matrix(matrix from) {
			mat = new long[40][40];
			for (int i = 0; i < n; i++)
				for (int j = 0; j < n; j++)
					mat[i][j] = from.mat[i][j];
		}
		void mod() {
			for (int i = 0; i < n; i++)
				for (int j = 0; j < n; j++)
					mat[i][j] = mat[i][j] % 10;			
		}
	}
	
	static matrix ident;
	static matrix curr;
	
	static matrix[] mats;
	static matrix[] sums;
	
	static int n;
	
	static matrix mult(matrix a, matrix b) {
		matrix ret = new matrix();
		for (int i = 0; i < n; i++)
			for (int j = 0; j < n; j++)
				for (int k = 0; k < n; k++)
					ret.mat[i][j] += a.mat[i][k] * b.mat[k][j];
		ret.mod();
		return ret;
	}
	
	static matrix sum(matrix a, matrix b) {
		matrix ret = new matrix();
		for (int i = 0; i < n; i++)
			for (int j = 0; j < n; j++)
				ret.mat[i][j] = (a.mat[i][j] + b.mat[i][j]);
		ret.mod();
		return ret;
	}
	
	static void calc_mat(int k) {
		matrix a = curr;
		for (int i = 0; i <= k; i++) {
			mats[i] = new matrix(a);
			a = mult(a, a);
		}			
	}
	
	static void calc_sum(int k) {
		matrix a = curr;
		for (int i = 0; i <= k; i++) {
			sums[i] = new matrix(a);
			a = sum(a, mult(mats[i], a));
		}
	}
	
	static matrix sum(int k, int l) {
		matrix ret = sums[l];
		matrix mu = mats[l];
		k -= (1 << l);
		l--;
		while (l >= 0 && (k & (1 << l)) == 0) l--;
		
		if (l >= 0) ret = sum(ret, mult(mu, sum(k, l)));

		return ret;		
	}	
	
	public static void main(String[] args) {
		mats = new matrix[22];
		sums = new matrix[22];
		curr = new matrix();
		ident = new matrix();
		for (int i = 0; i < 40; i++)
			ident.mat[i][i] = 1;
		
		Scanner in = new Scanner(System.in);
		
		while (true) {
			n = in.nextInt();
			int k = in.nextInt();
			if (n == 0) break;
			in.nextLine();
			for (int i = 0; i < n; i++) {
				for (int j = 0; j < n; j++)
					curr.mat[i][j] = in.nextLong() % 10;
				in.nextLine();					
			}
			int l = 23;
			while ((k & (1 << l)) == 0) l--;
			
			calc_mat(l);
			calc_sum(l);
			
			matrix res = sum(k, l);
			
			for (int i = 0; i < n; i++) {
				for (int j = 0; j < n; j++) {
					if (j != 0) System.out.print(" ");
					System.out.print(res.mat[i][j]);
				}
				System.out.println();
			}
			System.out.println();
		}
		
	}
}
