Problem: Apple adventure

square1001 and E869120 got lost in the grid world of $ H $ rows and $ W $ rows!

Said the god of this world.

"When you collect $ K $ apples and they meet, you'll be back in the original world."

Upon hearing this word, square1001 decided to collect more than $ K $ of apples and head to the square where E869120 is.




Here, each cell in the grid is represented as follows.

's': square1001 This is the square where you are.

'e': E869120 This is the square where you are.

'a': A square with one apple on it. You can get an apple the first time you visit this trout. There are no more than 20 squares on this grid.

'#': It's a wall. You cannot visit this square.

'.': A square with nothing. You can visit this square.




square1001 You try to achieve your goal by repeatedly moving from the square you are in to the squares that are adjacent to each other up, down, left, and right. However, you cannot get out of the grid.

square1001 Find the minimum number of moves you need to achieve your goal.

However, it is assumed that E869120 will not move. Also, square1001 shall be capable of carrying $ K $ or more of apples.

If the goal cannot be achieved, output "-1".

input

Input is given from standard input in the following format.

Let $ A_ {i, j} $ be the characters in the $ i $ square from the top of the grid and the $ j $ square from the left.


$ H $ $ W $ $ K $
$ A_ {1,1} A_ {1,2} A_ {1,3} \ cdots A_ {1, W} $
$ A_ {2,1} A_ {2,2} A_ {2,3} \ cdots A_ {2, W} $
$ A_ {3,1} A_ {3,2} A_ {3,3} \ cdots A_ {3, W} $
$ \ ldots $
$ A_ {H, 1} A_ {H, 2} A_ {H, 3} \ cdots A_ {H, W} $


output

square1001 Find the minimum number of moves you need to reach your goal. However, if this is not possible, output "-1".

However, insert a line break at the end.

Constraint

* $ 1 \ leq H \ leq 1000 $
* $ 1 \ leq W \ leq 1000 $
* $ 1 \ leq K \ leq 20 $
* $ H, W, K $ are integers.
* $ A_ {i, j} $ is one of's','e','a','#','.'.
* The grid contains only one's' and one'e'.
* The number of'a'in the grid is greater than or equal to $ K $ and less than or equal to $ 20 $.



Input example 1


5 5 2
s .. # a
. # ...
a # e. #
... # a
. # ...


Output example 1


14


Input example 2


7 7 3
.......
.s ... a.
a ## ... a
.. ### ..
.a # e # .a
. ### ..
a .. # .. a


Output example 2


-1


If the purpose cannot be achieved, output "-1".

Input example 3


12 12 10
. ##### ......
.## ..... # ...
.... a.a # a .. #
. # .. # a ......
..... a # s ..
..a ###. ##. #
.e #. #. #. # A ..
.. # a # ..... #.
.. ## a ......
.a ... a.a .. #.
a .... # a.aa ..
... a. # ... # a.


Output example 3


30






Example

Input

5 5 2
s..#a
.#...
a#e.#
...#a
.#...


Output

14

Solution (in CPP):

Solution:
#include <iostream>
#include <vector>
#include <queue>
#include <string>
#include <algorithm>

using namespace std;

struct pos {
    int x, y;
};
int h, w;

bool inRange(pos p) {
    return 0 <= p.x && p.x < h && 0 <= p.y && p.y < w;
}

const int INF = 514810114;
const vector<pos> moves{{1,  0},
                        {-1, 0},
                        {0,  1},
                        {0,  -1}};
vector<vector<bool>> can;

vector<vector<int>> bfs(pos start) {
    vector<vector<int>> ret(h, vector<int>(w, INF));
    queue<pos> que;
    que.push(start), que.push({-1, -1});

    int depth = 0;
    while (true) {
        pos now = que.front();
        que.pop();
        if (now.x < 0) {
            if (que.empty())break;
            que.push({-1, -1}), depth++;
        } else {
            ret[now.x][now.y] = depth;
            for (auto move:moves) {
                pos next = {now.x + move.x, now.y + move.y};
                if (inRange(next) && ret[next.x][next.y] == INF && can[next.x][next.y]) {
                    ret[next.x][next.y] = -1;
                    que.push(next);
                }
            }
        }
    }

    return ret;
}

int main() {
    int k;
    cin >> h >> w >> k;

    pos start, end;
    vector<pos> apples;
    can = vector<vector<bool>>(h, vector<bool>(w));
    for (int i = 0; i < h; i++) {
        string s;
        cin >> s;
        for (int j = 0; j < w; j++) {
            switch (s[j]) {
                case 's': {
                    start = {i, j};
                    break;
                }
                case 'e': {
                    end = {i, j};
                    break;
                }
                case 'a': {
                    apples.push_back({i, j});
                    break;
                }
                default: {
                    break;
                }
            }
            if (s[j] != '#')can[i][j] = true;
        }
    }

    int count = apples.size();
    vector<vector<int>> fromStart = bfs(start);
    vector<vector<int>> toEnd = bfs(end);
    vector<vector<vector<int>>> applesdis(count);
    for (int i = 0; i < count; i++)applesdis[i] = bfs(apples[i]);

    vector<vector<int>> dp(1 << count, vector<int>(count, 1145141919));
    for (int i = 0; i < count; i++) {
        dp[1 << i][i] = fromStart[apples[i].x][apples[i].y];
    }

    int ret = INF;
    for (int i = 0; i < (1 << count); i++) {
        vector<bool> avail(count);
        int tmp = 0;
        for (int j = 0; j < count; j++) {
            if (i & (1 << j)) {
                avail[j] = true;
                tmp++;
            }
        }
        if (tmp < 1)continue;

        if (tmp >= k) {
            for (int last = 0; last < count; last++) {
                if (avail[last]) {
                    int uku = dp[i][last];
                    int tapu = toEnd[apples[last].x][apples[last].y];
                    ret = min(ret, uku + tapu);
                }
            }
        }

        for (int prev = 0; prev < count; prev++) {
            if (!avail[prev])continue;
            for (int now = 0; now < count; now++) {
                if (avail[now])continue;

                int nbit = i | (1 << now);
                int cost = applesdis[prev][apples[now].x][apples[now].y];
                dp[nbit][now] = min(dp[nbit][now], dp[i][prev] + cost);
            }
        }
    }

    cout << (ret < INF ? ret : -1) << endl;
    return 0;
}