
let () = Scanf.scanf "%d %d %d\n%d %d %d %d\n" @@ fun h w k x1 y1 x2 y2 ->
  let x1 = x1 - 1 in
  let x2 = x2 - 1 in
  let y1 = y1 - 1 in
  let y2 = y2 - 1 in
  let css = Array.init h @@ fun _ -> Scanf.scanf "%s\n" Fun.id in
  let d =
    Array.init h @@ fun _ ->
      Array.init w @@ fun _ ->
        Array.init 5 @@ fun _ -> (max_int, max_int) in
  let es (i, j, s, dir) = 
    List.filter_map (fun (i, j, dir') ->
      if i < 0 || h <= i || j < 0 || w <= j || css.(i).[j] = '@'
      then None
      else if dir = dir' && s < k
      then Some ((i, j, s + 1, dir'), false)
      else Some ((i, j, 1, dir'), true))
    [(i - 1, j, 1); (i + 1, j, 2); (i, j - 1, 3); (i, j + 1, 4)] in
  let rec bfs p = function
    | [] -> ()
    | vs -> bfs (p + 1) @@ List.fold_left (dfs p) [] vs
  and dfs p vs ((i, j, s, dir) as v) =
    if d.(i).(j).(dir) <= (p, s)
    then vs
    else begin
      d.(i).(j).(dir) <- (p, s);
      List.fold_left (fun vs (v, b) ->
        if b
        then v :: vs
        else dfs p vs v) vs (es v)
    end in
  bfs 0 [(x1, y1, 0, 0)];
  Printf.printf "%d\n" @@
  (fun x -> if max_int <= x then -1 else x) @@
  fst @@ Array.fold_left min (max_int, max_int) d.(x2).(y2)

