#include
#include
int main()
{
// read floor dimensions
int n; std::cin >> n; // number of rows
int m; std::cin >> m; // number of columns
// define a twodimensional array of dimensions
// (n+2) x (m+2) to hold the floor plus extra walls around
std::vector > floor (n+2, std::vector(m+2));
// target coordinates, set upon reading 'T'
int tr = 0;
int tc = 0;
// assign initial floor values from input:
// source: 'S' -> 0 (source reached in 0 steps)
// target: 'T' -> -1 (number of steps still unknown)
// wall: 'X' -> -2
// empty cell: '-' -> -1 (number of steps still unknown)
for (int r=1; r> entry;
if (entry == 'S') floor[r][c] = 0;
else if (entry == 'T') floor[tr = r][tc = c] = -1;
else if (entry == 'X') floor[r][c] = -2;
else if (entry == '-') floor[r][c] = -1;
}
// add surrounding walls
for (int r=0; r 0) {
const int d = floor[r][c] - 1; // distance one less
floor[r][c] = -3; // mark cell as being on shortest path
// go to some neighbor with distance d
if (floor[r-1][c] == d) --r;
else if (floor[r+1][c] == d) ++r;
else if (floor[r][c-1] == d) --c;
else ++c; // (floor[r][c+1] == d)
}
// print floor with shortest path
for (int r=1; r