H, W = map(int, input().split()) A = [list(map(int, input().split())) for _ in range(H - 2)] INF = 10 ** 9 dp = [INF] * (H - 2) for i in range(H - 2): if A[i][0] != -1: dp[i] = A[i][0] #print(dp) for i in range(1, W): dp2 = [INF] * (H - 2) for j in range(H - 2): a = A[j][i] if a != -1: dp2[j] = min(dp2[j], dp[j] + a) if 0 < j: dp2[j] = min(dp2[j], dp[j - 1] + a) dp2[j] = min(dp2[j], dp2[j - 1] + a) if j < H - 3: dp2[j] = min(dp2[j], dp[j + 1] + a) for j in reversed(range(H - 3)): a = A[j][i] if a != -1: dp2[j] = min(dp2[j], dp2[j + 1] + a) dp = dp2 # print(dp) if min(dp) < INF: print(min(dp)) else: print(-1)