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 dp = Array.init rr (fun _ -> Array.init cc (fun _ -> Array.make 5 0)) in
    dp.(0).(0).(1) <- mat.(0).(0);
    for y = 0 to rr - 1 do
        for x = 0 to cc - 1 do
            for c = 0 to 3 do
                let v = mat.(y).(x) in
                if c = 0 || v = 0 then (
                    let q = if y > 0 then dp.(y - 1).(x).(4) else 0 in
                    let q = max q (if x > 0 then dp.(y).(x - 1).(c) else 0) in
                    dp.(y).(x).(c) <- max dp.(y).(x).(c) q
                ) else (
                    let q =        if y > 0 then max dp.(y - 1).(x).(4) (dp.(y - 1).(x).(4) + v) else 0 in
                    let q = max q (if x > 0 then max dp.(y).(x - 1).(c) (dp.(y).(x - 1).(c - 1) + v) else 0) in
                    dp.(y).(x).(c) <- max dp.(y).(x).(c) q
                )
            done
        done;
        for x = 0 to cc - 1 do
            dp.(y).(x).(4) <- Array.fold_left max 0 dp.(y).(x)
        done
    done;
    Printf.printf "%d\n" (dp.(rr - 1).(cc - 1).(4))
)