/* Problem 2--Triangular Word Search
   Of course, we won't really read the grid in as a "triangle," but as a
   lower matrix.  We can then search the grid forward backward and one
   but not both) of the diagonal directions. */

import java.io.*;
import java.util.*;

public class prob2 {

 public static Scanner in;
 public static PrintStream out;
 public static int cs, ct, sz;
 public static char[][] GT;
 public static String SS, SSR;

 public static void main (String[] args) throws Exception {

  in = new Scanner (new File ("prob2.in"));
  out = new PrintStream (new FileOutputStream ("prob2.out"));
  while ((sz=in.nextInt())>0) { //Read in the Grid
   GT = new char[sz][sz];
   for (int i=0; i < sz; i++)
    for (int j=0; j<=i; j++)
     GT[i][j] = in.next().charAt(0);
   SS = in.next(); //Get the search word.
   SSR = "";       //Reverse the search word.
   for (int i=SS.length()-1; i >=0; i--) SSR += SS.charAt(i);
   ct = 0;
   ProcessRow (); //Look horizontally.
   if (SS.length()>1) { //Look vertically and diagonally, unless the
    ProcessCol ();      //word is a single character.
    ProcessDiag ();
   }
   out.println ("Case "+(++cs)+": "+SS+" appears in the triangle "+ct+
                " time(s).");
  }
 }

 /* Scan the rows for the word. */
 public static void ProcessRow () {

  for (int i=0; i < sz; i++) {
   String A = "";
   for (int j=0; j <= i; j++) A += GT[i][j];
   Process (A,true,SS);
  }
 }

 /* Scan the columns for the word. */
 public static void ProcessCol () {

  for (int j=0; j < sz; j++) {
   String A = "";
   for (int i=j; i < sz; i++) A += GT[i][j];
   Process (A,true,SS);
  }
 }

 /* Scan the diagaonals for the word. */
 public static void ProcessDiag () {

  for (int i=0; i < sz; i++) {
   String A = "";
   for (int j=0; j < sz-i; j++) A += GT[i+j][j];
   Process (A,true,SS);
  }
 }

 /* Scan a string fro the word. */
 public static void Process (String A, boolean forward, String SS) {

  int k=-1;
  while (true) {
   k = A.indexOf (SS,k+1);
   if (k==-1) break;
   ct++;
  } //If the word is not a palindrome, we need to scan backwards, too.
  if (forward && !SS.equals(SSR)) Process (A,false,SSR);
 }
}
