#include <bits/stdc++.h>
using namespace std;
struct p {
int x, y;
} q[2000], mov[305][305];
int n, m, head, tail = 1;
char a[305][305];
bool vis[305][305];
int d[4][2] = {{0, -1}, {-1, 0}, {0, 1}, {1, 0}}, t[305][305];
int main() {
cin >> n >> m;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
cin >> a[i][j];
if (a[i][j] == '@')
q[tail].x = i, q[tail].y = j;
if (a[i][j] <= 'Z' && a[i][j] >= 'A') {
int dx = mov[a[i][j]][0].x;
int dy = mov[a[i][j]][0].y;
if (dx) {
mov[i][j] = {dx, dy};
mov[dx][dy] = {i, j};
} else {
mov[a[i][j]][0] = {i, j};
}
}
}
}
vis[q[tail].x][q[tail].y] = 1;
while (head < tail) {
head++;
struct p h = q[head];
if (a[h.x][h.y] == '=') {
cout << t[h.x][h.y];
return 0;
}
if (a[h.x][h.y] <= 'Z' && a[h.x][h.y] >= 'A') {
cout << mov[h.x][h.y].x << ' ' << mov[h.x][h.y].y << endl;
h.x = mov[h.x][h.y].x;
h.y = mov[h.x][h.y].y;
cout << h.x << ' ' << h.y << endl;
}
for (int i = 0; i < 4; i++) {
int dx = d[i][0] + h.x;
int dy = d[i][1] + h.y;
if (dx > n || dx < 1 || dy < 1 || dy > m || a[dx][dy] == '#' || vis[dx][dy])
continue;
t[dx][dy] = t[h.x][h.y] + 1;
vis[dx][dy] = 1;
struct p c = {dx, dy};
q[++tail] = c;
}
}
return 0;
}