#include <iostream>
#include <vector>
#include <algorithm>
/*2 26
1 1
abcdefghijklmnopqrstuvwxyz
abtxyzutalkhfdyutxzbzhhawj
nut
*/
struct tchk {
int x, y, amount;
bool operator<(const int &r){
return this->amount < r;
}
};
int n, m;
std::string target;
std::vector<std::vector<char>> mas(n);
int find(std::vector<std::vector<int>>& g){
int sum = 0;
tchk mn = { 0, 0, INT_MAX };
for (auto k : target) {
int x = mn.x;
int y = mn.y;
g[x][y] = 0;
for (int k = 0; k < n * m; ++k)
for (int i = 0; i < n * m; ++i)
for (int j = 0; j < n * m; ++j)
if (g[i][j] > g[i][k] + g[k][j])
g[i][j] = g[i][k] + g[k][j];
int i, j;
mn.amount = INT16_MAX;
std::cout << "i want find " << k << '\n';
for (i = 0; i < n; i++)
for (j = 0; j < m; j++) {
int temp = g[y][i * m + j];
mn = mn;
if (mas[i][j] == k)
if (mn.amount > temp) {
mn.x = x * m + y;
mn.y = i * m + j;
mn.amount = temp;
}
}
sum += mn.amount;
std::cout << sum << '\n';
g.resize(n * m, std::vector<int>(n * m, INT_MAX));
for (int i = 0; i < n * m; i++)
for (int j = 0; j < m * n; j++) {
if (((i - j == 1 or j - i == 1) and (j % m != 0 and i % m != 0) and (i != 0 or j != 0)) or i - j == m or j - i == m)
g[i][j] = g[j][i] = 1;
g[1][0] = 1;
g[0][1] = 1;
}
for (int i = 0; i < n * m; ++i)
g[i][i] = 0;
//std::cout << k;
}
return sum;
}
int main() {
int x, y;
std::cin >> n >> m >> x >> y;
--x;
--y;
std::vector<std::vector<int>> g(n * m, std::vector<int>(n * m, INT16_MAX));
g[x][y] = 0;
for (int i = 0; i < n; i++) {
std::string h;
std::cin >> h;
std::vector<char>mm;
for (auto temp : h) {
mm.push_back(temp);
}
mas.push_back(mm);
}
/*for (auto k : mas) {
for (auto j : k)
std::cout << j << ' ';
std::cout << '\n';
}*/
//std::string target;
std::cin >> target;
for (int i = 0; i < n * m; i++)
for (int j = 0; j < m * n; j++) {
if (((i - j == 1 or j - i == 1) and (j % m != 0 and i % m != 0) and (i != 0 or j != 0)) or i - j == m or j - i == m)
g[i][j] = g[j][i] = 1;
g[1][0] = 1;
g[0][1] = 1;
}
/* for (auto k : g) {
for (auto j : k)
std::cout << j << ' ';
std::cout << '\n';
}*/
for (int i = 0; i < n * m; ++i)
g[i][i] = 0;
//int temp;
//std::cin >> temp;
std::cout << find(g);
return 0;
}#include <iostream>
#include <vector>
#include <algorithm>
/*2 26
1 1
abcdefghijklmnopqrstuvwxyz
abtxyzutalkhfdyutxzbzhhawj
nut
*/
struct tchk {
int x, y, amount;
bool operator<(const int &r){
return this->amount < r;
}
};
int n, m;
std::string target;
std::vector<std::vector<char>> mas(n);
int find(std::vector<std::vector<int>>& g){
int sum = 0;
tchk mn = { 0, 0, INT_MAX };
for (auto k : target) {
int x = mn.x;
int y = mn.y;
g[x][y] = 0;
for (int k = 0; k < n * m; ++k)
for (int i = 0; i < n * m; ++i)
for (int j = 0; j < n * m; ++j)
if (g[i][j] > g[i][k] + g[k][j])
g[i][j] = g[i][k] + g[k][j];
int i, j;
mn.amount = INT16_MAX;
std::cout << "i want find " << k << '\n';
for (i = 0; i < n; i++)
for (j = 0; j < m; j++) {
int temp = g[y][i * m + j];
mn = mn;
if (mas[i][j] == k)
if (mn.amount > temp) {
mn.x = x * m + y;
mn.y = i * m + j;
mn.amount = temp;
}
}
sum += mn.amount;
std::cout << sum << '\n';
g.resize(n * m, std::vector<int>(n * m, INT_MAX));
for (int i = 0; i < n * m; i++)
for (int j = 0; j < m * n; j++) {
if (((i - j == 1 or j - i == 1) and (j % m != 0 and i % m != 0) and (i != 0 or j != 0)) or i - j == m or j - i == m)
g[i][j] = g[j][i] = 1;
g[1][0] = 1;
g[0][1] = 1;
}
for (int i = 0; i < n * m; ++i)
g[i][i] = 0;
//std::cout << k;
}
return sum;
}
int main() {
int x, y;
std::cin >> n >> m >> x >> y;
--x;
--y;
std::vector<std::vector<int>> g(n * m, std::vector<int>(n * m, INT16_MAX));
g[x][y] = 0;
for (int i = 0; i < n; i++) {
std::string h;
std::cin >> h;
std::vector<char>mm;
for (auto temp : h) {
mm.push_back(temp);
}
mas.push_back(mm);
}
/*for (auto k : mas) {
for (auto j : k)
std::cout << j << ' ';
std::cout << '\n';
}*/
//std::string target;
std::cin >> target;
for (int i = 0; i < n * m; i++)
for (int j = 0; j < m * n; j++) {
if (((i - j == 1 or j - i == 1) and (j % m != 0 and i % m != 0) and (i != 0 or j != 0)) or i - j == m or j - i == m)
g[i][j] = g[j][i] = 1;
g[1][0] = 1;
g[0][1] = 1;
}
/* for (auto k : g) {
for (auto j : k)
std::cout << j << ' ';
std::cout << '\n';
}*/
for (int i = 0; i < n * m; ++i)
g[i][i] = 0;
//int temp;
//std::cin >> temp;
std::cout << find(g);
return 0;
}