import sys
from collections import deque


def main():
    grid = {
        (x, y): z
        for y, line in enumerate(sys.stdin)
        for x, z in enumerate(line.strip())
    }

    [end] = [p for p, z in grid.items() if z == "E"]

    for starts in ["S", "Sa"]:
        queue = deque([(0, end, ord("z"))])
        visited = {end}

        while queue:
            step_count, position, elevation = queue.popleft()

            if grid[position] in starts:
                print(step_count)
                break

            x, y = position

            for dx, dy in [(-1, 0), (1, 0), (0, -1), (0, 1)]:
                new_position = x + dx, y + dy

                if new_position in visited or new_position not in grid:
                    continue

                c = grid[new_position]
                new_elevation = ord(c.replace(*"Sa").replace(*"Ez"))

                if new_elevation + 1 >= elevation:
                    visited.add(new_position)
                    queue.append((step_count + 1, new_position, new_elevation))


if __name__ == "__main__":
    main()
