#include <stdio.h>
#include <cmath>
#include <algorithm>
#include <cfloat>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <iostream>
#include <set>
#include <map>
#include <time.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;


typedef vector<ll> V;
typedef vector<V> MATRIX;

int num_station,N,M,L,case_num=1;
int SIZE,CALC_SIZE;
int diff_row[7] = {-1,-1,0,0,0,1,1},diff_col[7] = {-1,0,-1,0,1,0,1};


bool rangeCheck(int row,int col){
	if(row < 0 || row >= 2*N-1)return false;

	if(row < N){
		if(col >= 0 && col < N+row)return true;
		else{
			return false;
		}
	}else{
		if(col >= row-N+1 && col <= 2*N-2)return true;
		else{
			return false;
		}
	}
}

MATRIX calc_ans(MATRIX left, MATRIX right){
    MATRIX RET(CALC_SIZE,V(1));
    for(int row = 0; row < CALC_SIZE; row++){
        for(int col = 0; col < 1; col++){
            RET[row][col] = 0;
            for(int k = 0; k < CALC_SIZE; k++){
                RET[row][col] += left[row][k]*right[k][col];
                RET[row][col] %=M;
            }
        }
    }
    return RET;
}



MATRIX calc(MATRIX left,MATRIX right){

	MATRIX ret(CALC_SIZE,V(CALC_SIZE));

	for(int i = 0; i < CALC_SIZE; i++){
		for(int k = 0; k < CALC_SIZE; k++)ret[i][k] = 0;
	}

	for(int row = 0; row < CALC_SIZE; row++){
		for(int col = 0; col < CALC_SIZE; col++){
			for(int a = 0; a < CALC_SIZE; a++){
				ret[row][col] += left[row][a]*right[a][col];
				ret[row][col] %= M;
			}
		}
	}

	return ret;

}

MATRIX pow(MATRIX MULT,int count){

	MATRIX ret(CALC_SIZE,V(CALC_SIZE));

	for(int row = 0; row < CALC_SIZE; row++){
		for(int col = 0; col < CALC_SIZE; col++){
			if(row == col)ret[row][col] = 1;
			else{
				ret[row][col] = 0;
			}
		}
	}

	while(count > 0){
		if(count%2 == 1)ret = calc(ret,MULT);
		MULT = calc(MULT,MULT);
		count /= 2;
	}

	return ret;
}

void func(){

	SIZE = 2*N-1;

	MATRIX TATE(SIZE*SIZE,V(1));

	for(int row = 0; row < SIZE*SIZE; row++){
		TATE[row][0] = 0;
	}

	for(int row = 0; row < SIZE; row++){
		if(row < N){
			for(int col = 0; col < N+row; col++){
				scanf("%lld",&TATE[row*SIZE+col][0]);
			}
		}else{
			for(int col = row-N+1; col < SIZE; col++){
				scanf("%lld",&TATE[row*SIZE+col][0]);
			}
		}
	}

	CALC_SIZE = SIZE*SIZE;
	MATRIX POWER(CALC_SIZE,V(CALC_SIZE));

	for(int row = 0; row < SIZE; row++){
		if(row < N){
			for(int col = 0; col < N+row; col++){
				for(int k = 0; k < 7; k++){
					if(rangeCheck(row+diff_row[k],col+diff_col[k])){
						POWER[row*SIZE+col][(row+diff_row[k])*SIZE+col+diff_col[k]] = 1;
					}
				}
			}
		}else{
			for(int col = row-N+1; col < SIZE; col++){
				for(int k = 0; k < 7; k++){
					if(rangeCheck(row+diff_row[k],col+diff_col[k])){
						POWER[row*SIZE+col][(row+diff_row[k])*SIZE+col+diff_col[k]] = 1;
					}
				}
			}
		}
	}

	if(L > 1){
		POWER = pow(POWER,L);
	}

	TATE = calc_ans(POWER,TATE);

	ll ans = 0;

	for(int row = 0; row < CALC_SIZE; row++){
		ans += TATE[row][0];
	}

	printf("Case %d: %lld\n",case_num++,ans);
}


int main(){

	while(true){
		scanf("%d %d %d",&N,&M,&L);
		if(N == 0 && M == 0 && L == 0)break;

		func();
	}


	return 0;
}




Measured memory_bytes: 3620