Scanf.scanf "%d %d %d" (fun rr cc k ->
    let mat = Array.make_matrix rr cc 0 in
    for i = 1 to k do
        Scanf.scanf " %d %d %d" (fun rr cc v ->
            mat.(rr - 1).(cc - 1) <- v
        )
    done;
    let max (a:int) (b:int) = if a < b then b else a in

    let prev = Array.make cc 0 in
    for y = 0 to rr - 1 do
        let m = mat.(y) in
        let rec loop x work0 work1 work2 work3 =
            if x < cc then (
                let work0 = max work0 prev.(x) in
                let v = m.(x) in
                let work3 = max work3 (work2 + v) in
                let work2 = max work2 (work1 + v) in
                let work1 = max work1 (work0 + v) in
                prev.(x) <- max (max work3 work2) (max work1 work0);
                loop (x + 1) work0 work1 work2 work3
            )
        in
        loop 0 0 0 0 0
    done;
    Printf.printf "%d\n" (prev.(cc - 1))
)