/* Problem 4--Portal Maze
   It's been a while since I've had a maze problem in the contest.  This
   one doesn't involve much beyond the normal BFS technique of maze
   solving.  Just check for the portal! */

import java.io.*;
import java.util.*;

public class prob4 {

 public static Scanner in=null;
 public static PrintWriter out=null;
 public static int r=0, c=0, cs=1;
 public static char[][] A = null;
 public static boolean[][] V = null;

 public static void main (String[] args) throws Exception {

  in = new Scanner (new File ("prob4.in"));
  out = new PrintWriter ("prob4.out");
  int cs=1;
  while (true) {
   r = in.nextInt();
   c = in.nextInt();
   if (r==0 && c==0) break;
   in.nextLine();
   A = new char[r][c]; // get input
   for (int i=0; i < r; i++)
    A[i] = in.nextLine().toCharArray();
   V = new boolean[r][c];
   Process ();
  }
  in.close();
  out.close();
 }

 /* Process uses the built-in LinkedList class to manipulate the queue
    for BFS */
 public static void Process () {

  LinkedList<minfo> Q = new LinkedList ();
  Q.offer (new minfo (0,0,0)); //add start square
  minfo p = null;
  while (true) {
   p = Q.remove(); //try next square
   if (V[p.r][p.c]) continue;
   if (p.r==r-1 && p.c==c-1) break; //enqueue neighbors
   if (p.r > 0 && A[p.r-1][p.c] != '*')
    Q.offer (new minfo(p.r-1,p.c,p.dist+1));
   if (p.r < r-1 && A[p.r+1][p.c] != '*')
    Q.offer (new minfo(p.r+1,p.c,p.dist+1));
   if (p.c > 0 && A[p.r][p.c-1] != '*')
    Q.offer (new minfo(p.r,p.c-1,p.dist+1));
   if (p.c < c-1 && A[p.r][p.c+1] != '*')
    Q.offer (new minfo(p.r,p.c+1,p.dist+1));
   if (A[p.r][p.c] >= 'A' && A[p.r][p.c] <= 'Z') // enqueue portal
    Q.offer (find ((char)(A[p.r][p.c]+32),p.dist+1));
   V[p.r][p.c] = true;
  }
  out.println ("Case "+(cs++)+": The maze can be solved in "+p.dist+
               " move(s).");
 }

 /* minfo looks for the square containing the other end of the portal
    and returns an minfo structure containing that square */
 public static minfo find (char ch, int dist) {

  for (int i=0; i < r; i++)
   for (int j=0; j < c; j++)
    if (A[i][j]==ch) return new minfo (i,j,dist);
  return null;
 }
}

/* Basic information for the queue */
class minfo {
 public int r=0, c=0, dist = 0;
 public minfo (int rr, int cc, int d) {
  r = rr; c = cc; dist = d;
 }
}
