http://acm.hdu.edu.cn/showproblem.php?pid=1429
//1329152 2009-05-02 08:34:49 Accepted 1429 687MS 4252K 2405 B C++ no way 
#include<iostream>
#include
<queue>
using namespace std;
struct Node
{
    
int x;
    
int y;
    
int step;
    
int num;
    
bool mark[10];
}
;
int dir[4][2]={-1,0,1,0,0,-1,0,1};
int binary[26]={1,2,4,8,16,32,64,128,256,512,1024,2048,4096,8192,16384,32768,65536,131072,
                   
262144,524288,1048576,2097152,4194304,8388608,16777216,33554432}
;
char gra[21][21];
int flag[21][21][2049];
int m,n,t;
int sx,sy,ex,ey;

void bfs()
{
    
int i,k,temp,ch;
    Node p,q;
    queue
<Node>Q;
    p.x 
= sx;
    p.y 
= sy;
    p.step 
= 0;
    p.num 
= 0;
    
for(i=0;i<10;i++)
        p.mark[i] 
= false;
    Q.push(p);
    
while(!Q.empty())
    
{
        q 
= Q.front();
        Q.pop();
        
if(q.x == ex && q.y == ey)
        
{
            
if(q.step < t)
            
{
                cout
<<q.step<<endl;
                
return ;
            }

            
break ;
        }

        
for(k=0;k<4;k++)
        
{
            p 
= q;
            p.x 
+= dir[k][0];
            p.y 
+= dir[k][1];
            
            
if(p.x >=0 && p.x<&& p.y>=0 && p.y<&& gra[p.x][p.y] != '*')
            
{
                
if(gra[p.x][p.y] >= 'a' && gra[p.x][p.y] <= 'j')
                
{
                    ch 
= gra[p.x][p.y] - 'a';
                    
if(p.mark[ch] == false)
                        temp 
= p.num + binary[ch];
                    
else
                        temp 
= p.num;
                    
if(flag[p.x][p.y][temp] == -1)
                    
{
                        flag[p.x][p.y][temp] 
= q.step + 1;
                        p.step 
= q.step + 1;
                        p.num 
= temp;
                        p.mark[ch] 
= true;
                        Q.push(p);
                    }

                }

                
else if(gra[p.x][p.y] >= 'A' && gra[p.x][p.y] <= 'J')
                
{
                    ch 
= gra[p.x][p.y] - 'A';
                    
if(flag[p.x][p.y][p.num] == -1 && p.mark[ch] == true)
                    
{
                        flag[p.x][p.y][p.num] 
= q.step + 1;
                        p.step 
= q.step + 1;
                        Q.push(p);
                    }

                }

                
else 
                
{
                    
if(flag[p.x][p.y][p.num] == -1)
                    
{
                        flag[p.x][p.y][p.num] 
= q.step + 1;
                        p.step 
= q.step + 1;
                        Q.push(p);
                    }

                }

            }

        }
//for(k=0;k<4;k++)
    }
//while(!Q.empty())
    cout<<-1<<endl;
}


int main()
{
   
int i,j,k;
   
while(cin>>m>>n>>t)
   
{
       
for(i=0;i<m;i++)
       
{
           scanf(
"%s",gra[i]);
           
for(j=0;j<n;j++)
           
{
               
if(gra[i][j] == '@')
               
{
                   sx 
= i;
                   sy 
= j;
               }

               
else if(gra[i][j] == '^')
               
{
                   ex 
= i;
                   ey 
= j;
               }

               
for(k=0;k<2049;k++)
                   flag[i][j][k] 
= -1;
           }

       }

       bfs();
   }

   
return 0;
}