/*
* @Author: MaxPhast
* @File: test.cpp
* @Date: 2026-04-06 20:40:29
*/
#include <bits/stdc++.h>
using namespace std;
#define ii pair <int , int>
#define iii pair <int , ii>
#define iiii pair <ii , ii>
#define FOR(i , l , r) for(int i = (l) , _r = (r) ; i <= _r ; ++ i)
#define FORD(i , r , l) for(int i = (r) , _l = (l) ; i >= _l ; -- i)
#define ALL(vec) vec.begin() , vec.end()
#define UNI(vec) sort(ALL(vec)) , vec.erase(unique(ALL(vec)) , vec.end())
#define pb push_back
#define MASK(i) (1ll << i)
#define BIT(mask , i) ((mask >> i) & 1ll)
#define ON(mask , i) (mask | MASK(i))
#define OFF(mask , i) (mask & (~MASK(i)))
#define TURN(mask , i) (mask ^ MASK(i))
#define BP(mask) __builtin_popcountll(mask)
#define sqr(x) (1ll * (x) * (x))
#define fi first
#define se second
#define oo (int)1e9
#define time() cerr << " \n " << "Time elapsed : " << 1000.0 * clock() / CLOCKS_PER_SEC << "ms."
#define IO(TASK) if(fopen(#TASK".INP" , "r")){freopen(#TASK".INP" , "r" , stdin); freopen(#TASK".OUT" , "w" , stdout);}
template <class X , class Y>
bool maxz(X &a , const Y b)
{
if(a < b)
{
a = b;
return true;
}
return false;
}
template <class X , class Y>
bool minz(X &a , const Y b)
{
if(a > b)
{
a = b;
return true;
}
return false;
}
namespace MaxPhast
{
const int WH = 500;
const int N = 10;
int n , w , h , ans = oo;
char c[WH][WH];
int dp[WH][WH][N][N];
int dx[4] = {0 , -1 , 0 , 1};
int dy[4] = {-1 , 0 , 1 , 0};
struct State
{
int cost , x , y , l , r;
State()
{
cost = x = y = l = r = 0;
}
State(int _cost , int _x , int _y , int _l , int _r)
{
cost = _cost;
x = _x;
y = _y;
l = _l;
r = _r;
}
bool operator < (const State &other) const
{
return cost > other.cost;
}
};
queue <State> pq;
int ins(int x , int y)
{
return x >= 0 && x < h && y >= 0 && y < w;
}
int next(int dir , int x , int y)
{
return (dir + (c[x][y] == 'A' ? -1 : (c[x][y] == 'C' ? 1 : 0)) + 4) % 4;
}
void solve()
{
cin >> n >> w >> h;
FOR(i , 0 , h - 1)
FOR(j , 0 , w - 1)
cin >> c[i][j];
FOR(i , 0 , h - 1)
{
FOR(j , 0 , w - 1)
{
FOR(l , 0 , n - 1)
{
FOR(r , l , n - 1)
{
dp[i][j][l][r] = oo;
}
}
}
}
FOR(i , 0 , h - 1)
{
FOR(j , 0 , w - 1)
{
if(c[i][j] >= '1' && c[i][j] <= char(n + '0'))
{
dp[i][j][c[i][j] - '1'][c[i][j] - '1'] = 0;
pq.push(State(0 , i , j , c[i][j] - '1' , c[i][j] - '1'));
}
}
}
while(!pq.empty())
{
State cur = pq.front();
pq.pop();
if(cur.cost > dp[cur.x][cur.y][cur.l][cur.r])
continue;
FOR(dir , 0 , 3)
{
int newx = cur.x , newy = cur.y , newdir = next(dir , newx , newy);
while(ins(newx + dx[newdir] , newy + dy[newdir]) && c[newx + dx[newdir]][newy + dy[newdir]] != 'x')
{
newx += dx[newdir];
newy += dy[newdir];
newdir = next(newdir , newx , newy);
}
if(minz(dp[newx][newy][cur.l][cur.r] , dp[cur.x][cur.y][cur.l][cur.r] + 1))
pq.push(State(dp[newx][newy][cur.l][cur.r] , newx , newy , cur.l , cur.r));
}
FOR(newl , 0 , cur.l - 1)
if(minz(dp[cur.x][cur.y][newl][cur.r] , dp[cur.x][cur.y][newl][cur.l - 1] + dp[cur.x][cur.y][cur.l][cur.r]))
pq.push(State(dp[cur.x][cur.y][newl][cur.r] , cur.x , cur.y , newl , cur.r));
FORD(newr , cur.r + 1 , n - 1)
if(minz(dp[cur.x][cur.y][cur.l][newr] , dp[cur.x][cur.y][cur.l][cur.r] + dp[cur.x][cur.y][cur.r + 1][newr]))
pq.push(State(dp[cur.x][cur.y][cur.l][newr] , cur.x , cur.y , cur.l , newr));
}
FOR(i , 0 , h - 1)
FOR(j , 0 , w - 1)
minz(ans , dp[i][j][0][n - 1]);
if(ans < oo)
cout << ans;
else
cout << -1;
}
}
signed main()
{
ios_base :: sync_with_stdio(false) ; cin.tie(nullptr) ; cout.tie(nullptr) ;
IO(test);
MaxPhast :: solve();
time();
}
/*
*/