program sample
    implicit none
    character(100)::s
    integer(8) :: i,j,l,m,n,a,b,c,k,a1,a2,v,cou(4)
    integer(8),allocatable :: x(:,:),y1(:),y2(:),ans(:)
  
    read(*,*) a,b,k
    allocate(x(a,b),y1(b),y2(b),ans(b))
    x(:, :)=0
    do i=1,k
        read(*,*)a1,a2,v
        x(a1,a2)=v
    end do
    cou(:)=0
    c=1
    y1(1)=x(1,1)
    if(x(1,1)>0)then
        cou(1)=x(1,1)
        c=c+1
    endif
    do j=2,b
        y1(j)=y1(j-1)+x(1,j)
        if(x(1,j)>0)then
            if(c>3)then
                cou(4)=x(1,j)
                y1(j)=y1(j)-minval(cou)
                do l=1,4
                    if(cou(l)==minval(cou))then
                        cou(l)=cou(4)
                    end if
                end do
            else
                cou(c)=x(1,j)
                c=c+1
            end if
        end if
    end do
    ans(:)=y1(:)
        
    do i=2,a
        do j=1,b
            y2(j)=ans(j)+x(i,j)
        end do

        
        cou(:)=0
        c=0
        ans(1)=y2(1)
        do j=2,b
            
            y1(j)=ans(j-1)+x(i,j)
            
            if(x(i,j)>0)then
                if(c>3)then
                    cou(4)=x(i,j)
                    y1(j)=y1(j)-minval(cou)
                    do l=1,4
                        if(cou(l)==minval(cou))then
                            cou(l)=cou(4)
                        end if
                    end do
                else
                    cou(c)=x(i,j)
                    c=c+1
                end if
            end if
            if(y2(j)>y1(j))then
                ans(j)=y2(j)
                c=1
                cou(:)=0
            else
                ans(j)=y1(j)
            end if
        end do
    end do
    write(*,*)ans(b)
    
  
    stop
end program sample
  

