fun main(args:Array<String>?): Unit {
    val (h, _) = readLine()!!.trim().split(' ').map(String::toInt)
    val info = Array(h){ readLine()!!.trim().toCharArray().map{it - '0'}.toIntArray()}
    for (y in info.indices.reversed())
        for (x in info[y].indices.reversed())
            info[y][x] += listOfNotNull(if (x + 1 in info[y].indices) info[y][x + 1] else null, if (y + 1 in info.indices) info[y + 1][x] else null).min() ?: 0
    println(info.first().first())
}
