# 벽 부수고 이동하기2 - BOJ
from collections import deque
dy = [1, -1, 0, 0]
dx = [0, 0, -1, 1]
N, M, K = map(int, input().split())
board = [list(map(int,input())) for _ in range(N)]
visit = [[[0] * (K + 1) for _ in range(M)] for _ in range(N)]
flag = False
q = deque()
q.append([0, 0, 0, 1])
while q:
y, x, k, dist = q.popleft()
if y == N - 1 and x == M - 1:
flag = True
break
for i in range(4):
ny = y + dy[i]
nx = x + dx[i]
nk = k + 1
if ny < 0 or nx < 0 or ny >= N or nx >= M:
continue
if visit[ny][nx][k]:
continue
if not board[ny][nx]:
visit[ny][nx][k] = visit[y][x][k] + 1
q.append([ny, nx, k, dist + 1])
elif board[ny][nx] and k < K:
visit[ny][nx][nk] = visit[y][x][k] + 1
q.append([ny, nx, k + 1, dist + 1])
if flag:
print(dist)
else:
print(-1)