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 <array>
#include <list>
#include <string>
#include <stack>
#include <queue>
#include <deque>
#include <unordered_map>
#include <unordered_set>
#include <tuple>
#include <memory>
#include <cmath>
#include <algorithm>
#include <functional>
#include <iomanip>
#include <numeric>
#include <climits>
#include <cfloat>
#include <cassert>
#include <random>

struct Coordinate {
	int x, y;
	std::vector<Coordinate> neighbors() const {
		return std::vector<Coordinate>{ Coordinate{x + 1, y}, Coordinate{x - 1, y}, Coordinate{x, y + 1}, Coordinate{x, y - 1} };
	}
};
std::vector<std::vector<int>> min_step(const Coordinate start, const std::vector<std::string>& state) {
	auto comparator = [](const std::pair<Coordinate, int>& a, const std::pair<Coordinate, int>& b) {return a.second > b.second; };
	std::priority_queue<std::pair<Coordinate, int>, std::vector<std::pair<Coordinate, int>>, decltype(comparator)> queue(comparator);
	std::vector<std::vector<int>> result(state.size(), std::vector<int>(state.front().size(), INT_MAX));
	queue.emplace(start, 0);
	result[start.y][start.x] = 0;
	while (!queue.empty()) {
		const auto top = queue.top(); queue.pop();
		if (result[top.first.y][top.first.x] == top.second) {
			for (const auto next : top.first.neighbors()) if (0 <= next.y && next.y < result.size() && 0 <= next.x && next.x < result[next.y].size()) {
				if (state[next.y][next.x] != '#' && result[next.y][next.x] > top.second + 1) {
					result[next.y][next.x] = top.second + 1;
					queue.emplace(next, top.second + 1);
				}
			}
		}
	}
	return result;
}
int bit_count(int x) {
	int res = 0;
	while (x > 0) {
		res += x % 2;
		x >>= 1;
	}
	return res;
}
int main() {
	int height, width, k; std::cin >> height >> width >> k;
	std::vector<std::string> state(height); for (auto& line : state) std::cin >> line;
	std::vector<Coordinate> apples; 
	Coordinate e, s;
	for (auto i = 0; i < height; ++i) for (auto j = 0; j < width; ++j) {
		if (state[i][j] == 'e') {
			e = Coordinate{ j, i };
		}
		else if (state[i][j] == 's') {
			s = Coordinate{ j, i };
		}
		else if (state[i][j] == 'a') {
			apples.push_back(Coordinate{ j, i });
		}
	}
	const auto from_start = min_step(e, state);
	const auto to_goal = min_step(s, state);
	apples.erase(std::remove_if(apples.begin(), apples.end(), [&from_start, &to_goal](const Coordinate& c) {return from_start[c.y][c.x] == INT_MAX || to_goal[c.y][c.x] == INT_MAX; }), apples.end());
	std::vector<std::vector<std::vector<int>>> min_steps; std::transform(apples.begin(), apples.end(), std::back_inserter(min_steps), [&state](const Coordinate from) {return min_step(from, state); });
	std::vector<std::vector<int>> min_distance(1 << apples.size(), std::vector<int>(apples.size(), INT_MAX));
	for (auto i = 0; i < apples.size(); ++i) {
		min_distance[1 << i][i] = from_start[apples[i].y][apples[i].x];
	}
	int min = INT_MAX;
	for (auto i = 0; i < min_distance.size(); ++i) {
		if (bit_count(i) >= k) {
			int m = INT_MAX;
			for (auto j = 0; j < apples.size(); ++j) if (min_distance[i][j] != INT_MAX) m = std::min(m, min_distance[i][j] + to_goal[apples[j].y][apples[j].x]);
			min = std::min(min, m);
			continue;
		}
		for (auto j = 0; j < apples.size(); ++j) if (min_distance[i][j] != INT_MAX) {
			for (auto k = 0; k < apples.size(); ++k) if ((i & (1 << k)) == 0 && min_steps[j][apples[k].y][apples[k].x] != INT_MAX) {
				if (min_distance[i | (1 << k)][k] > min_distance[i][j] + min_steps[j][apples[k].y][apples[k].x]) {
					min_distance[i | (1 << k)][k] = min_distance[i][j] + min_steps[j][apples[k].y][apples[k].x];
				}
			}
		}
	}
	std::cout << (min == INT_MAX ? -1 : min) << std::endl;
}