import java.util.Arrays;
import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt(), m = sc.nextInt();
String[][] arr = new String[n][m];
sc.nextLine();
for (int i = 0; i < n; i++) {
String s = sc.nextLine();
for(int j = 0;j<m;j++) {
arr[i][j] = s.charAt(j)+"";
}
}
int[][] dp = new int[n][m];
//dp[0][0] = arr[0][0].charAt(0);
for(int i = 0;i<n;i++) {
for(int j = 0;j<m;j++) {
dp[i][j] = arr[i][j].charAt(0);
}
}
for(int i = 1;i<n;i++) {
dp[i][0] = dp[i-1][0] + arr[i][0].charAt(0);
arr[i][0] = arr[i-1][0] + arr[i][0];
}
for(int i = 1;i<m;i++) {
dp[0][i] = dp[0][i-1] + arr[0][i].charAt(0);
arr[0][i] = arr[0][i-1] + arr[0][i];
}
for(int i = 1;i<n;i++) {
for(int j =1;j<m;j++) {
if(dp[i-1][j]<dp[i][j-1]) {
arr[i][j] = arr[i-1][j] + arr[i][j];
}else {
arr[i][j] = arr[i][j-1] + arr[i][j];
}
dp[i][j] += Math.min(dp[i-1][j], dp[i][j-1]);
}
}
System.out.println(arr[n-1][m-1]);
}
}