import java.io.*; import java.util.*; import java.util.stream.*; public class Main { static int MOD; public static void main(String[] args) throws Exception { Scanner sc = new Scanner(); int h = sc.nextInt() - 2; int w = sc.nextInt(); int[][] field = new int[h][w]; for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { field[i][j] = sc.nextInt(); } } PriorityQueue queue = new PriorityQueue<>(); for (int i = 0; i < h; i++) { queue.add(new Path(i * w, 0)); } int[] costs = new int[h * w]; Arrays.fill(costs, Integer.MAX_VALUE); while (queue.size() > 0) { Path p = queue.poll(); int r = p.idx / w; int c = p.idx % w; if (field[r][c] < 0) { continue; } p.value += field[r][c]; if (costs[p.idx] <= p.value) { continue; } costs[p.idx] = p.value; if (c >= w - 1) { continue; } if (r > 0) { queue.add(new Path(p.idx - w + 1, p.value)); queue.add(new Path(p.idx - w, p.value)); } queue.add(new Path(p.idx + 1, p.value)); if (r < h - 1) { queue.add(new Path(p.idx + w + 1, p.value)); queue.add(new Path(p.idx + w, p.value)); } } int ans = Integer.MAX_VALUE; for (int i = w - 1; i < h * w; i += w) { ans = Math.min(ans, costs[i]); } if (ans == Integer.MAX_VALUE) { ans = -1; } System.out.println(ans); } static class Path implements Comparable { int idx; int value; public Path(int idx, int value) { this.idx = idx; this.value = value; } public int compareTo(Path another) { return value - another.value; } } } class Utilities { static String arrayToLineString(Object[] arr) { return Arrays.stream(arr).map(x -> x.toString()).collect(Collectors.joining("\n")); } static String arrayToLineString(int[] arr) { return String.join("\n", Arrays.stream(arr).mapToObj(String::valueOf).toArray(String[]::new)); } } class Scanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); StringBuilder sb = new StringBuilder(); public Scanner() throws Exception { } public int nextInt() throws Exception { return Integer.parseInt(next()); } public long nextLong() throws Exception { return Long.parseLong(next()); } public double nextDouble() throws Exception { return Double.parseDouble(next()); } public int[] nextIntArray() throws Exception { return Stream.of(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray(); } public String next() throws Exception { while (!st.hasMoreTokens()) { st = new StringTokenizer(br.readLine()); } return st.nextToken(); } }