from heapq import heapify, heappop, heappush h, w = map(int, input().split()) S = [list(input()) for _ in range(h)] Directions = [(1, 0), (0, 1)] H = [(S[0][0], 0, 0)] heapify(H) while H: res, ci, cj = heappop(H) if ci == h - 1 and cj == w - 1: print(res) break for di, dj in Directions: ni = ci + di nj = cj + dj if ni < h and nj < w: heappush(H, (res + S[ni][nj], ni, nj))