#include<iostream>
#include<vector>
#include<queue>

void correct1(){
    int R,C,K;
    std::cin>>R>>C>>K;
    std::vector<std::vector<unsigned long long> > mat(R,std::vector<unsigned long long>(C,0));
    std::vector<std::vector<bool> > flags(R,std::vector<bool>(C,1));
    std::vector<std::vector<std::vector<unsigned long long> > > dp(R,std::vector< std::vector<unsigned long long> >(C,std::vector<unsigned long long>(4,0)));
    for(int i=0;i<K;++i){
        int r,c,v;
        std::cin>>r>>c>>v;
        mat[r-1][c-1]=v;
    }
    std::queue<std::pair<int,int> > searchQ;
    searchQ.push(std::pair<int,int>(0,0));
    flags[0][0]=0;
    // dp[0][0][1]=mat[0][0];
    while (!searchQ.empty()){
        std::pair<int,int> index=searchQ.front();
        int y=index.first,x=index.second;
        searchQ.pop();
        if(index.second+1 < C){
            for(int i=0;i<4;++i){
                if(dp[y][x+1][i]<dp[y][x][i]){
                    dp[y][x+1][i]=dp[y][x][i];
                }
            }
            if(mat[y][x]>0){
                for(int i=0;i<3;++i){
                    unsigned long long buf=(dp[y][x][i]+mat[y][x]);
                    if(dp[y][x+1][i+1]<buf){
                        dp[y][x+1][i+1]=buf;
                    }
                }
            }
            if(flags[y][x+1]==1){
                searchQ.push(std::pair<int,int>(y,x+1));
                flags[y][x+1]=0;
            }
        }
        if(index.first+1<R){
            unsigned long long max=0;
            for(int i=0;i<4;++i){
                unsigned long long buf=dp[y][x][i];
                if(mat[y][x]>0 && i<3){
                    buf+=mat[y][x];
                }
                if(max<buf){
                    max=buf;
                }
            }
            dp[y+1][x][0]=max;
            if(flags[y+1][x]==1){
                searchQ.push(std::pair<int,int>(y+1,x));
                flags[y+1][x]=0;
            }
        }
    }
    unsigned long long ans=0;
    for(int i=0;i<4;++i){
        unsigned long long buf=dp[R-1][C-1][i];
        if(mat[R-1][C-1] && i<3){
            buf+=mat[R-1][C-1];
        }
        if(ans<buf){
            ans=buf;
        }
    }
    std::cout<<ans<<std::endl;
}

// aim less memory
void correct2(){
    int R,C,K;
    std::cin>>R>>C>>K;
    int P=3+1;
    std::vector<unsigned long long> mat(R*C,0);
    std::vector<bool> flags(R*C,1);
    std::vector<unsigned long long> dp(R*C*P,0);
    for(int i=0;i<K;++i){
        int r,c;
        unsigned long long v;
        std::cin>>r>>c>>v;
        mat[(r-1)*C+(c-1)]=v;
    }
    std::queue<std::pair<int,int> > searchQ;
    searchQ.push(std::pair<int,int>(0,0));
    flags[0*C+0]=0;
    while (!searchQ.empty()){
        std::pair<int,int> index=searchQ.front();
        int y=index.first,x=index.second;
        searchQ.pop();
        if(index.second+1 < C){
            for(int i=0;i<4;++i){
                if(dp[y*C*P+(x+1)*P+i]<dp[y*C*P+x*P+i]){
                    dp[y*C*P+(x+1)*P+i]=dp[y*C*P+x*P+i];
                }
            }
            if(mat[y*C+x]>0){
                for(int i=0;i<3;++i){
                    unsigned long long buf=(dp[y*C*P+x*P+i]+mat[y*C+x]);
                    if(dp[y*C*P+(x+1)*P+(i+1)]<buf){
                        dp[y*C*P+(x+1)*P+(i+1)]=buf;
                    }
                }
            }
            if(flags[y*C+(x+1)]==1){
                searchQ.push(std::pair<int,int>(y,x+1));
                flags[y*C+(x+1)]=0;
            }
        }
        if(index.first+1<R){
            unsigned long long max=0;
            for(int i=0;i<4;++i){
                unsigned long long buf=dp[y*C*P+x*P+i];
                if(mat[y*C+x]>0 && i<3){
                    buf+=mat[y*C+x];
                }
                if(max<buf){
                    max=buf;
                }
            }
            dp[(y+1)*C*P+x*P+0]=max;
            if(flags[(y+1)*C+x]==1){
                searchQ.push(std::pair<int,int>(y+1,x));
                flags[(y+1)*C+x]=0;
            }
        }
    }
    unsigned long long ans=0;
    for(int i=0;i<4;++i){
        unsigned long long buf=dp[(R-1)*C*P+(C-1)*P+i];
        if(mat[(R-1)*C+(C-1)] && i<3){
            buf+=mat[(R-1)*C+(C-1)];
        }
        if(ans<buf){
            ans=buf;
        }
    }
    std::cout<<ans<<std::endl;
}

int main(){
    correct2();
}

Measured memory_bytes: 355780