Under construction

Still being built. Things may change

Break Walls and Move 2

Interview

Time limit2sMemory limit512 MB

Summary
Find the shortest path length from the top-left to the bottom-right of an N by M grid, where you may break at most K walls along the way.
Level

Medium6 of 10

Topics
BFS, Graph, Shortest path, Array
Solved
No attempts yet

Problem

A map is given as an N×MN \times M matrix. In the map, 0 is a cell you can move into, and 1 is a cell with a wall that you cannot move into. You want to move from (1,1)(1, 1) to (N,M)(N, M) along a shortest path. A shortest path is a path that passes through the fewest cells of the map, counting both the starting cell and the ending cell.

If breaking walls along the way makes the path shorter, you may break up to KK walls while moving.

From a cell you can move to the cells adjacent to it up, down, left, or right.

Given the map, write a program that finds the length of a shortest path.

Input

The first line contains NN (1≤N≤1 0001 \le N \le 1\,000), MM (1≤M≤1 0001 \le M \le 1\,000), and KK (1≤K≤101 \le K \le 10). Each of the next NN lines contains one row of the map as MM digits with no spaces. (1,1)(1, 1) and (N,M)(N, M) are always 0.

Output

Print the length of a shortest path on the first line. If (N,M)(N, M) cannot be reached, print -1.

Examples3

  1. Example 1

    Input
    6 4 1
    0100
    1110
    1000
    0000
    0111
    0000
    
    Expected output
    15
    
  2. Example 2

    Input
    6 4 2
    0100
    1110
    1000
    0000
    0111
    0000
    
    Expected output
    9
    
  3. Example 3

    Input
    4 4 3
    0111
    1111
    1111
    1110
    
    Expected output
    -1