Problem:
Score : 600 points

Problem Statement

Snuke, a water strider, lives in a rectangular pond that can be seen as a grid with H east-west rows and W north-south columns. Let (i,j) be the square at the i-th row from the north and j-th column from the west.

Some of the squares have a lotus leaf on it and cannot be entered.
The square (i,j) has a lotus leaf on it if c_{ij} is @, and it does not if c_{ij} is ..

In one stroke, Snuke can move between 1 and K squares (inclusive) toward one of the four directions: north, east, south, and west.
The move may not pass through a square with a lotus leaf. Moving to such a square or out of the pond is also forbidden.

Find the minimum number of strokes Snuke takes to travel from the square (x_1,y_1) to (x_2,y_2).
If the travel from (x_1,y_1) to (x_2,y_2) is impossible, point out that fact.

Constraints

1 \leq H,W,K \leq 10^6

H \times W \leq 10^6

1 \leq x_1,x_2 \leq H

1 \leq y_1,y_2 \leq W

x_1 \neq x_2 or y_1 \neq y_2.

c_{i,j} is . or @.

c_{x_1,y_1} = .

c_{x_2,y_2} = .

All numbers in input are integers.

Input

Input is given from Standard Input in the following format:

H W K
x_1 y_1 x_2 y_2
c_{1,1}c_{1,2} .. c_{1,W}
c_{2,1}c_{2,2} .. c_{2,W}
:
c_{H,1}c_{H,2} .. c_{H,W}

Output

Print the minimum number of strokes Snuke takes to travel from the square (x_1,y_1) to (x_2,y_2), or print -1 if the travel is impossible.

Sample Input 1

3 5 2
3 2 3 4
.....
.@..@
..@..

Sample Output 1

5

Initially, Snuke is at the square (3,2).
He can reach the square (3, 4) by making five strokes as follows:

From (3, 2), go west one square to (3, 1).

From (3, 1), go north two squares to (1, 1).

From (1, 1), go east two squares to (1, 3).

From (1, 3), go east one square to (1, 4).

From (1, 4), go south two squares to (3, 4).

Sample Input 2

1 6 4
1 1 1 6
......

Sample Output 2

2

Sample Input 3

3 3 1
2 1 2 3
.@.
.@.
.@.

Sample Output 3

-1

Solution in Java (OpenJDK 1.8.0) (judge verdict: Accepted)

import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.Reader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.StringTokenizer;
import java.util.TreeSet;

public class Main {

    private static final long MODULO = (long) (1e9 + 7);
    private static final int INF = (int) 1e9;

    public static void main(String[] args) {
        BufferedScanner scanner = new BufferedScanner();
        PrintWriter writer = new PrintWriter(new BufferedOutputStream(System.out));

        int t = 1;//scanner.nextInt();
        for (int tc = 0; tc < t; tc++) {
            int h = scanner.nextInt();
            int w = scanner.nextInt();
            int k = scanner.nextInt();
            int x1 = scanner.nextInt() - 1;
            int y1 = scanner.nextInt() - 1;
            int x2 = scanner.nextInt() - 1;
            int y2 = scanner.nextInt() - 1;
            String[] grid = new String[h];
            for (int i = 0; i < h; i++) {
                grid[i] = scanner.next();
            }
            TreeSetInt[] rows = new TreeSetInt[h];
            for (int i = 0; i < h; i++) {
                rows[i] = new TreeSetInt();
                for (int j = 0; j < w; j++) {
                    if (grid[i].charAt(j) != '@') {
                        rows[i].add(j);
                    }
                }
            }
            TreeSetInt[] cols = new TreeSetInt[w];
            for (int j = 0; j < w; j++) {
                cols[j] = new TreeSetInt();
                for (int i = 0; i < h; i++) {
                    if (grid[i].charAt(j) != '@') {
                        cols[j].add(i);
                    }
                }
            }
            int[][] rowPref = new int[h][w + 1];
            for (int i = 0; i < h; i++) {
                rowPref[i][0] = 0;
                for (int j = 0; j < w; j++) {
                    rowPref[i][j + 1] = rowPref[i][j] + (grid[i].charAt(j) == '@' ? 1 : 0);
                }
            }
            int[][] colPref = new int[w][h + 1];
            for (int j = 0; j < w; j++) {
                colPref[j][0] = 0;
                for (int i = 0; i < h; i++) {
                    colPref[j][i + 1] = colPref[j][i] + (grid[i].charAt(j) == '@' ? 1 : 0);
                }
            }
            int[][] shortest = new int[h][w];
            for (int i = 0; i < h; i++) {
                Arrays.fill(shortest[i], INF);
            }
            shortest[x1][y1] = 0;
            List<Pair> q = new ArrayList<>();
            q.add(Pair.of(x1, y1));
            rows[x1].remove(y1);
            cols[y1].remove(x1);
            int p = 0;
            while (p < q.size()) {
                int x = q.get(p).first;
                int y = q.get(p).second;
                p++;
                if (x == x2 && y == y2) {
                    break;
                }
                {
                    Iterator<Integer> it = rows[x].tailSet(y, false).iterator();
                    while (it.hasNext()) {
                        int yy = it.next();
                        if (y + k < yy || rowPref[x][y] != rowPref[x][yy + 1]) {
                            break;
                        }
                        q.add(Pair.of(x, yy));
                        shortest[x][yy] = shortest[x][y] + 1;
                        it.remove();
                        cols[yy].remove(x);
                    }
                }
                {
                    Iterator<Integer> it = rows[x].headSet(y, false).descendingIterator();
                    while (it.hasNext()) {
                        int yy = it.next();
                        if (yy + k < y || rowPref[x][yy] != rowPref[x][y + 1]) {
                            break;
                        }
                        q.add(Pair.of(x, yy));
                        shortest[x][yy] = shortest[x][y] + 1;
                        it.remove();
                        cols[yy].remove(x);
                    }
                }
                {
                    Iterator<Integer> it = cols[y].tailSet(x, false).iterator();
                    while (it.hasNext()) {
                        int xx = it.next();
                        if (x + k < xx || colPref[y][x] != colPref[y][xx + 1]) {
                            break;
                        }
                        q.add(Pair.of(xx, y));
                        shortest[xx][y] = shortest[x][y] + 1;
                        it.remove();
                        rows[xx].remove(y);
                    }
                }
                {
                    Iterator<Integer> it = cols[y].headSet(x, false).descendingIterator();
                    while (it.hasNext()) {
                        int xx = it.next();
                        if (xx + k < x || colPref[y][xx] != colPref[y][x + 1]) {
                            break;
                        }
                        q.add(Pair.of(xx, y));
                        shortest[xx][y] = shortest[x][y] + 1;
                        it.remove();
                        rows[xx].remove(y);
                    }
                }
            }
            writer.println(shortest[x2][y2] == INF ? -1 : shortest[x2][y2]);
        }

        scanner.close();
        writer.flush();
        writer.close();
    }

    static class Pair {
        int first, second;

        static Pair of(int f, int s) {
            Pair ans = new Pair();
            ans.first = f;
            ans.second = s;
            return ans;
        }
    }

    static class TreeSetInt extends TreeSet<Integer> {}

    static class ListInt extends ArrayList<Integer> {}

    public static class BufferedScanner {
        BufferedReader br;
        StringTokenizer st;

        public BufferedScanner(Reader reader) {
            br = new BufferedReader(reader);
        }

        public BufferedScanner() {
            this(new InputStreamReader(System.in));
        }

        String next() {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt() {
            return Integer.parseInt(next());
        }

        long nextLong() {
            return Long.parseLong(next());
        }

        double nextDouble() {
            return Double.parseDouble(next());
        }

        String nextLine() {
            String str = "";
            try {
                str = br.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return str;
        }

        void close() {
            try {
                br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

    static long gcd(long a, long b) {
        if (a < b) {
            return gcd(b, a);
        }
        while (b > 0) {
            long tmp = b;
            b = a % b;
            a = tmp;
        }
        return a;
    }

    static long inverse(long a, long m) {
        long[] ans = extgcd(a, m);
        return ans[0] == 1 ? (ans[1] + m) % m : -1;
    }

    private static long[] extgcd(long a, long m) {
        if (m == 0) {
            return new long[]{a, 1, 0};
        } else {
            long[] ans = extgcd(m, a % m);
            long tmp = ans[1];
            ans[1] = ans[2];
            ans[2] = tmp;
            ans[2] -= ans[1] * (a / m);
            return ans;
        }
    }

    static long add(long a, long b) {
        a += b;
        if (a >= MODULO) {
            a -= MODULO;
        }
        return a;
    }

    static long sub(long a, long b) {
        a -= b;
        if (a < 0) {
            a += MODULO;
        }
        return a;
    }

    static long mul(long a, long b) {
        return a * b % MODULO;
    }

    static long div(long a, long b) {
        return a * inverse(b, MODULO) % MODULO;
    }

    static class Comb {
        final long modulo;
        final long[] fac, fnv;

        Comb(int limit, long modulo) {
            fac = new long[limit + 1];
            fnv = new long[limit + 1];
            fac[0] = 1;
            fnv[0] = 1;
            for (int i = 1; i <= limit; i++) {
                fac[i] = mul(fac[i - 1], i);
                fnv[i] = div(fnv[i - 1], i);
            }
            this.modulo = modulo;
        }

        long c(int total, int choose) {
            if (total < choose) {
                return 0;
            }
            if (total == 0 || total == choose) {
                return 1;
            }
            return mul(mul(fac[total], fnv[choose]), fnv[total - choose]);
        }
    }

}