const
  inf = 1000000000;
  dx : array[1..4] of integer = (-1, 1, 0, 0);
  dy : array[1..4] of integer = (0, 0, -1, 1);
  maxn = 50;
  maxp = maxn * maxn * maxn * maxn;

var
  n, m, cx, cy, i, j, pos, b, e, x, y, npos, hx, hy, dir : integer;
  q, d : array[0..maxp - 1] of integer;
  s : array[1..maxn] of string[maxn];

function code(cx, cy, hx, hy : integer) : integer;
begin
  result := cx - 1;
  result := result * m;
  result := result + cy - 1;
  result := result * n;
  result := result + hx - 1;
  result := result * m;
  result := result + hy - 1;
end;

procedure decode(c : integer);
begin
  hy := c mod m + 1;
  c := c div m;
  hx := c mod n + 1;
  c := c div n;
  cy := c mod m + 1;
  c := c div m;
  cx := c mod n + 1;
end;

begin
  assign(input, 'parking.in');
  reset(input);
  assign(output, 'parking.out');
  rewrite(output);
  readln(n, m);
  cx := -1;
  cy := -1;
  hx := -1;
  hy := -1;
  for i := 1 to n do begin
    readln(s[i]);
    for j := 1 to m do begin
      if (s[i][j] = 'X') then begin
        cx := i;
        cy := j;
      end;
      if (s[i][j] = '.') then begin
        hx := i;
        hy := j;
      end;
    end;
  end;
  pos := code(cx, cy, 1, 1);
  for i := 0 to maxp - 1 do begin
    d[i] := inf;
  end;
  d[pos] := 0;
  q[0] := pos;
  b := 0;
  e := 1;
  while (b <> e) do begin
    pos := q[b];
    b := b + 1;
    decode(pos);
    if (cx = 1) and (cy = 1) then begin
      writeln(d[pos]);
      halt;
    end;
    for dir := 1 to 4 do begin
      x := hx + dx[dir];
      y := hy + dy[dir];
      if (x < 1) or (y < 1) or (x > n) or (y > m) or (s[x][y] = '#') then continue;
      if (x = cx) and (y = cy) then begin
        npos := code(hx, hy, cx, cy);
      end else begin
        npos := code(cx, cy, x, y);
      end;
      if (d[npos] > d[pos] + 1) then begin
        d[npos] := d[pos] + 1;
        q[e] := npos;
        e := e + 1;
      end;
    end;
  end;
  writeln('Impossible');
end.      
