#include <iostream>
using namespace std;
struct matrix {
int mat[31][31];
};
long n, m;
matrix E; //единичная матрица
matrix mul(matrix A, matrix B) {
matrix C;
for(int i = 0; i < n; i++)
for(int j = 0; j < n; j++) {
C.mat[i][j] = 0;
for(int k = 0; k < n; k++)
C.mat[i][j] += A.mat[i][k] * B.mat[k][j] % m;
}
return C;
}
matrix sum(matrix A, matrix B) {
for(int i = 0; i < n; i++)
for(int j = 0; j < n; j++)
A.mat[i][j] += B.mat[i][j] % m;
return A;
}
matrix power(matrix A, int k) {
if (k == 1)
return A;
matrix B = power(A, k / 2);
B = mul(B, B);
if (k % 2 != 0)
B = mul(B, A);
return B;
}
matrix res(matrix A, int k) {
if (k == 1)
return A;
if (k % 2 == 0) {
matrix B = res(A, k / 2);
return mul(sum(power(A, k / 2), E), B);
}
else {
matrix B = res(A, k / 2);
matrix C = power(A, (k+1) / 2);
return sum(C, mul(B, sum(C, E)));
}
}
int main() {
long long k;
matrix A;
cin >> n >> k >> m;
for(int i = 0; i < n; i++) {
E.mat[i][i] = 1;
for(int j = 0; j < n; j++)
cin >> A.mat[i][j];
}
matrix ans = res(A, k);
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
if (j != 0)
cout << " ";
cout << ans.mat[i][j] % m;
}
cout << "\n";
}
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKc3RydWN0IG1hdHJpeCB7CiAgICBpbnQgbWF0WzMxXVszMV07Cn07Cgpsb25nIG4sIG07Cm1hdHJpeCBFOyAvL9C10LTQuNC90LjRh9C90LDRjyDQvNCw0YLRgNC40YbQsAoKbWF0cml4IG11bChtYXRyaXggQSwgbWF0cml4IEIpIHsKICAgIG1hdHJpeCBDOwogICAgZm9yKGludCBpID0gMDsgaSA8IG47IGkrKykKICAgICAgICBmb3IoaW50IGogPSAwOyBqIDwgbjsgaisrKSB7CiAgICAgICAgICAgIEMubWF0W2ldW2pdID0gMDsKICAgICAgICAgICAgZm9yKGludCBrID0gMDsgayA8IG47IGsrKykKICAgICAgICAgICAgICAgIEMubWF0W2ldW2pdICs9IEEubWF0W2ldW2tdICogQi5tYXRba11bal0gJSBtOwogICAgICAgIH0KCXJldHVybiBDOwp9CgptYXRyaXggc3VtKG1hdHJpeCBBLCBtYXRyaXggQikgeyAKICAgIGZvcihpbnQgaSA9IDA7IGkgPCBuOyBpKyspCiAgICAgICAgZm9yKGludCBqID0gMDsgaiA8IG47IGorKykKICAgICAgICAgICAgQS5tYXRbaV1bal0gKz0gQi5tYXRbaV1bal0gJSBtOwogICAgcmV0dXJuIEE7Cn0KCm1hdHJpeCBwb3dlcihtYXRyaXggQSwgaW50IGspIHsgCiAgICBpZiAoayA9PSAxKQogICAgCXJldHVybiBBOwogICAgbWF0cml4IEIgPSBwb3dlcihBLCBrIC8gMik7CiAgICBCID0gbXVsKEIsIEIpOwogICAgaWYgKGsgJSAyICE9IDApCiAgICAJQiA9IG11bChCLCBBKTsKICAgIHJldHVybiBCOwp9CgptYXRyaXggcmVzKG1hdHJpeCBBLCBpbnQgaykgewogICAgaWYgKGsgPT0gMSkKICAgIAlyZXR1cm4gQTsKICAgIGlmIChrICUgMiA9PSAwKSB7CiAgICAgICAgbWF0cml4IEIgPSByZXMoQSwgayAvIDIpOwogICAgICAgIHJldHVybiBtdWwoc3VtKHBvd2VyKEEsIGsgLyAyKSwgRSksIEIpOwogICAgfQogICAgZWxzZSB7CiAgICAgICAgbWF0cml4IEIgPSByZXMoQSwgayAvIDIpOwogICAgICAgIG1hdHJpeCBDID0gcG93ZXIoQSwgKGsrMSkgLyAyKTsKICAgICAgICByZXR1cm4gc3VtKEMsIG11bChCLCBzdW0oQywgRSkpKTsKICAgIH0KfQoKaW50IG1haW4oKSB7CiAgICBsb25nIGxvbmcgazsKICAgIG1hdHJpeCBBOwoJY2luID4+IG4gPj4gayA+PiBtOwogICAgZm9yKGludCBpID0gMDsgaSA8IG47IGkrKykgewogICAgICAgIEUubWF0W2ldW2ldID0gMTsKICAgICAgICBmb3IoaW50IGogPSAwOyBqIDwgbjsgaisrKSAKICAgICAgICAgICAgY2luID4+IEEubWF0W2ldW2pdOwogICAgfQogICAgbWF0cml4IGFucyA9IHJlcyhBLCBrKTsKICAgIGZvcihpbnQgaSA9IDA7IGkgPCBuOyBpKyspIHsKICAgICAgICBmb3IoaW50IGogPSAwOyBqIDwgbjsgaisrKSB7CiAgICAgICAgICAgaWYgKGogIT0gMCkKICAgICAgICAgICAgCWNvdXQgPDwgIiAiOwogICAgICAgICAgICBjb3V0IDw8IGFucy5tYXRbaV1bal0gJSBtOwogICAgICAgIH0KICAgICAgICBjb3V0IDw8ICJcbiI7CiAgICB9CiAgICByZXR1cm4gMDsKfQ==