r, c = gets.split.map(&:to_i)
fld = r.times.map{ gets.split('').map(&:to_i) }
(1...r).each{ |y| fld[y][0] += fld[y-1][0] }
(1...c).each{ |x| fld[0][x] += fld[0][x-1] }
(1...r).each do |y|
  (1...c).each do |x|
    fld[y][x] += [fld[y][x-1], fld[y-1][x]].min
  end
end
p fld[r-1][c-1]