Problem: You are working for an amusement park as an operator of an obakeyashiki, or a haunted house, in which guests walk through narrow and dark corridors. The house is proud of their lively ghosts, which are actually robots remotely controlled by the operator, hiding here and there in the corridors. One morning, you found that the ghosts are not in the positions where they are supposed to be. Ah, yesterday was Halloween. Believe or not, paranormal spirits have moved them around the corridors in the night. You have to move them into their right positions before guests come. Your manager is eager to know how long it takes to restore the ghosts.

In this problem, you are asked to write a program that, given a floor map of a house, finds the smallest number of steps to move all ghosts to the positions where they are supposed to be.

A floor consists of a matrix of square cells. A cell is either a wall cell where ghosts cannot move into or a corridor cell where they can.

At each step, you can move any number of ghosts simultaneously. Every ghost can either stay in the current cell, or move to one of the corridor cells in its 4-neighborhood (i.e. immediately left, right, up or down), if the ghosts satisfy the following conditions:

1. No more than one ghost occupies one position at the end of the step.
2. No pair of ghosts exchange their positions one another in the step.



For example, suppose ghosts are located as shown in the following (partial) map, where a sharp sign ('#) represents a wall cell and 'a', 'b', and 'c' ghosts.



ab#
c##



The following four maps show the only possible positions of the ghosts after one step.



ab#      a b#     acb#     ab #
c##      #c##     # ##     #c##





Input

The input consists of at most 10 datasets, each of which represents a floor map of a house. The format of a dataset is as follows.


w h n
c11c12...c1w
c21c22...c2w
. . ..       .
..           .
.
..           .
ch1ch2...chw


w, h and n in the first line are integers, separated by a space. w and h are the floor width and height of the house, respectively. n is the number of ghosts. They satisfy the following constraints.

4 ≤ w ≤ 16

4 ≤ h ≤ 16

1 ≤ n ≤ 3

Subsequent h lines of w characters are the floor map. Each of cij is either:

* a '#' representing a wall cell,
* a lowercase letter representing a corridor cell which is the initial position of a ghost,
* an uppercase letter representing a corridor cell which is the position where the ghost corresponding to its lowercase letter is supposed to be, or
* a space representing a corridor cell that is none of the above.



In each map, each of the first n letters from a and the first n letters from A appears once and only once. Outermost cells of a map are walls; i.e. all characters of the first and last lines are sharps; and the first and last characters on each line are also sharps. All corridor cells in a map are connected; i.e. given a corridor cell, you can reach any other corridor cell by following corridor cells in the 4-neighborhoods. Similarly, all wall cells are connected. Any 2 × 2 area on any map has at least one sharp. You can assume that every map has a sequence of moves of ghosts that restores all ghosts to the positions where they are supposed to be.

The last dataset is followed by a line containing three zeros separated by a space.

Output

For each dataset in the input, one line containing the smallest number of steps to restore ghosts into the positions where they are supposed to be should be output. An output line should not contain extra characters such as spaces.

Examples

Input

5 5 2
#####
#A#B#
#   #
#b#a#
#####
16 4 3
################
## ########## ##
#    ABCcba    #
################
16 16 3
################
### ##    #   ##
##  #  ##   # c#
#  ## ########b#
# ##  # #   #  #
#  # ##   # # ##
##  a#  # # #  #
### ## #### ## #
##   #   #  #  #
#  ##### # ## ##
####   #B# #   #
##  C#   #   ###
#  # # ####### #
# ######  A##  #
#        #    ##
################
0 0 0


Output

7
36
77


Input

5 5 2

A#B#

b#a#

16 4 3


ABCcba    #

16 16 3


c#
b#


a#  # # #  #



B# #   #
C#   #   ###

A##  #


0 0 0


Output

7
36
77

Solution (in CPP):

Solution:
#include <iostream>
#include <array>
#include <vector>
#include <cctype>
#include <map>
#include <queue>
#include <cassert>
#define repeat(i,n) for (int i = 0; (i) < (n); ++(i))
#define repeat_from(i,m,n) for (int i = (m); (i) < (n); ++(i))
using namespace std;

struct point_t {
    int y, x;
};
point_t operator + (point_t const & a, point_t const & b) {
    return (point_t){ a.y + b.y, a.x + b.x };
}
bool operator == (point_t const & a, point_t const & b) {
    return make_pair(a.y, a.x) == make_pair(b.y, b.x);
}
bool operator < (point_t const & a, point_t const & b) {
    return make_pair(a.y, a.x) < make_pair(b.y, b.x);
}
static point_t dp[5] = { {-1,0}, {1,0}, {0,1}, {0,-1}, {0,0} };

struct state_t {
    array<point_t,3> a;
    int cost;
    int dist;
};
#define MAGIC_WEIGHT 1.3
bool operator < (state_t const & a, state_t const & b) {
    return MAGIC_WEIGHT * a.cost + a.dist > MAGIC_WEIGHT * b.cost + b.dist;
}
bool is_valid_move(int i, int j, array<point_t,3> const & s, array<point_t,3> const & t) {
    if (t[i] == t[j]) return false;
    if (t[i] == s[j] and t[j] == s[i]) return false;
    return true;
}

int main() {
    while (true) {
        int w, h, n; cin >> w >> h >> n; cin.ignore();
        if (w == 0 and h == 0 and n == 0) break;
        assert (1 <= n and n <= 3);
        vector<string> c(h);
        repeat (i,h) getline(cin, c[i]);

        array<point_t,3> start;
        array<point_t,3> goal;
        map<point_t,int> ix;
        repeat (y,h) repeat (x,w) {
            if (islower(c[y][x])) {
                start[c[y][x] - 'a'] = { y, x };
            } else if (isupper(c[y][x])) {
                goal[c[y][x] - 'A'] = { y, x };
            }
            if (c[y][x] != '#') {
                int i = ix.size();
                ix[(point_t){ y, x }] = i;
            }
        }
        repeat_from (i,n,3) {
            start[i] = ix.begin()->first; // fill with some non-wall cell
            goal[i]  = ix.begin()->first;
        }

        vector<vector<int> > dist[3]; // from goal
        repeat (i,3) {
            dist[i].resize(h, vector<int>(w, 1000000007));
            queue<point_t> que; // bfs
            que.push(goal[i]);
            dist[i][goal[i].y][goal[i].x] = 0;
            while (not que.empty()) {
                point_t p = que.front(); que.pop();
                repeat (j,4) {
                    auto q = p + dp[j];
                    if (c[q.y][q.x] == '#') continue;
                    if (dist[i][q.y][q.x] == 1000000007) {
                        dist[i][q.y][q.x] = dist[i][p.y][p.x] + 1;
                        que.push(q);
                    }
                }
            }
        }

        vector<vector<vector<bool> > > used(ix.size(), vector<vector<bool> >(ix.size(), vector<bool>(ix.size())));

        priority_queue<state_t> que; {
            state_t initial = { start, 0, 0 };
            repeat (i,3) initial.dist += dist[i][start[i].y][start[i].x];
            used[ix[start[0]]][ix[start[1]]][ix[start[2]]] = true;
            que.push(initial);
        }
        while (not que.empty()) {
            state_t st = que.top(); que.pop();
            array<point_t,3> & s = st.a;
            if (s == goal) {
                cout << st.cost << endl;
                break;
            }
            state_t tt = st;
            tt.cost += 1;
            array<point_t,3> & t = tt.a;
            repeat (i,5) { // a
                t[0] = s[0] + dp[i];
                if (c[t[0].y][t[0].x] == '#') continue;
                repeat (j,5) { // b
                    if (n < 2 and j != 4) continue;
                    t[1] = s[1] + dp[j];
                    if (c[t[1].y][t[1].x] == '#') continue;
                    if (not is_valid_move(0, 1, s, t)) continue;
                    repeat (k,5) { // c
                        if (n < 3 and k != 4) continue;
                        t[2] = s[2] + dp[k];
                        if (c[t[2].y][t[2].x] == '#') continue;
                        if (n == 3 and not is_valid_move(1, 2, s, t)) continue;
                        if (n == 3 and not is_valid_move(2, 0, s, t)) continue;
                        if (used[ix[t[0]]][ix[t[1]]][ix[t[2]]]) continue;
                        used[ix[t[0]]][ix[t[1]]][ix[t[2]]] = true;
                        tt.dist = 0;
                        repeat (i,3) tt.dist = max(tt.dist, dist[i][t[i].y][t[i].x]);
                        que.push(tt);
                    }
                }
            }
        }
    }
    return 0;
}