high,wide = gets.chomp.split(" ").map{|x| x.to_i}
semi_map = Array.new.map!{Array.new}
(high).times do 
semi_map.push(gets.chomp.split(//).map{|x|x.to_i})
end
(high).times do |h|
  (wide).times do |w|
    if h>0 && w>0 
      semi_map[h][w] +=  semi_map[h-1][w] < semi_map[h][w-1] ? semi_map[h-1][w]: semi_map[h][w-1]
    elsif h>0 || w>0
      semi_map[h][w] += h>0 ? semi_map[h-1][w]: semi_map[h][w-1]
    end   
 end  
end
p semi_map[high-1][wide-1]