import sys
from itertools import islice


def resolve(in_):
    R, C, K = map(int, next(in_).split())
    _rcv = (map(int, line.split()) for line in islice(in_, K))

    R1 = R + 1
    C1 = C + 1
    R2 = R + 2
    C2 = C + 2
    limit_item = 3
    limit_item1 = limit_item + 1
    C2limit_item1 = C2 * limit_item1

    # grid = [[0] * C2 for _ in range(R2)]
    grid = [0] * (R2 * C2)
    for r, c, v in _rcv:
        # grid[r][c] = v
        grid[(r * C2) + c] = v

    
    # dp = [[[0] * limit_item1 for j in range(C2)] for i in range(R2)]
    dp = [0] * (R2 * C2 * limit_item1)
    # dp[1][1][1] = grid[1][1]
    dp[(1 * C2limit_item1) + (1 * limit_item1) + 1] = grid[(1 * C2) + 1]

    for i in range(1, R1):
        for j in range(1, C1):
            for k in range(limit_item1):
                # current = dp[i][j][k]
                current = dp[(i * C2limit_item1) + (j * limit_item1) + k]

                # left = grid[i][j + 1]
                left = grid[(i * C2) + (j + 1)]
                # dp[i][j + 1][k] = max(dp[i][j + 1][k], current)
                dp[(i * C2limit_item1) + ((j + 1) * limit_item1) + k] = max(dp[(i * C2limit_item1) + ((j + 1) * limit_item1) + k], current)
                if k < limit_item:
                    # dp[i][j + 1][k + 1] = max(dp[i][j + 1][k + 1], left + current)
                    dp[(i * C2limit_item1) + ((j + 1) * limit_item1) + (k + 1)] = max(dp[(i * C2limit_item1) + ((j + 1) * limit_item1) + (k + 1)], left + current)

                # top = grid[i + 1][j]
                top = grid[((i + 1) * C2) + j]
                # dp[i + 1][j][0] = max(dp[i + 1][j][0], current)
                dp[(i + 1) * C2limit_item1 + (j * limit_item1) + 0] = max(dp[(i + 1) * C2limit_item1 + (j * limit_item1) + 0], current)
                # dp[i + 1][j][1] = max(dp[i + 1][j][1], top + current)
                dp[(i + 1) * C2limit_item1 + (j * limit_item1) + 1] = max(dp[(i + 1) * C2limit_item1 + (j * limit_item1) + 1], top + current)
    # return max(dp[R][C])
    return max(dp[(R * C2limit_item1) + (C * limit_item1):((R + 1) * C2limit_item1) + ((C + 1) * limit_item1)])


def main():
    answer = resolve(sys.stdin.buffer)
    print(answer)


if __name__ == '__main__':
    main() import sys sys from itertools import islice def resolve(in_):
    R, C, K = map(int, next(in_).split())
    _rcv = (map(int, line.split()) for line in islice(in_, K))

    R1 = R + 1
    C1 = C + 1
    R2 = R + 2
    C2 = C + 2
    limit_item = 3
    limit_item1 = limit_item + 1
    C2limit_item1 = C2 * limit_item1

    # grid = [[0] * C2 for _ in range(R2)]
    grid = [0] * (R2 * C2)
    for r, c, v in _rcv:
        # grid[r][c] = v
        grid[(r * C2) + c] = v

    
    # dp = [[[0] * limit_item1 for j in range(C2)] for i in range(R2)]
    dp = [0] * (R2 * C2 * limit_item1)
    # dp[1][1][1] = grid[1][1]
    dp[(1 * C2limit_item1) + (1 * limit_item1) + 1] = grid[(1 * C2) + 1]

    for i in range(1, R1):
        for j in range(1, C1):
            for k in range(limit_item1):
                # current = dp[i][j][k]
                current = dp[(i * C2limit_item1) + (j * limit_item1) + k]

                # left = grid[i][j + 1]
                left = grid[(i * C2) + (j + 1)]
                # dp[i][j + 1][k] = max(dp[i][j + 1][k], current)
                dp[(i * C2limit_item1) + ((j + 1) * limit_item1) + k] = max(dp[(i * C2limit_item1) + ((j + 1) * limit_item1) + k], current)
                if k < limit_item:
                    # dp[i][j + 1][k + 1] = max(dp[i][j + 1][k + 1], left + current)
                    dp[(i * C2limit_item1) + ((j + 1) * limit_item1) + (k + 1)] = max(dp[(i * C2limit_item1) + ((j + 1) * limit_item1) + (k + 1)], left + current)

                # top = grid[i + 1][j]
                top = grid[((i + 1) * C2) + j]
                # dp[i + 1][j][0] = max(dp[i + 1][j][0], current)
                dp[(i + 1) * C2limit_item1 + (j * limit_item1) + 0] = max(dp[(i + 1) * C2limit_item1 + (j * limit_item1) + 0], current)
                # dp[i + 1][j][1] = max(dp[i + 1][j][1], top + current)
                dp[(i + 1) * C2limit_item1 + (j * limit_item1) + 1] = max(dp[(i + 1) * C2limit_item1 + (j * limit_item1) + 1], top + current)
    # return max(dp[R][C])
    return max(dp[(R * C2limit_item1) + (C * limit_item1):((R + 1) * C2limit_item1) + ((C + 1) * limit_item1)]) resolve R, C, K = map(int, next(in_).split()) R map(int, next(in_).split()) map int next(in_).split() (in_).split (in_) next in_ split C K map(int, line.split()) for line in islice(in_, K) for line in islice(in_, K) line islice(in_, K) islice in_ K for line in islice(in_, K) map(int, line.split()) map int line.split() line.split line split _rcv = (map(int, line.split()) for line in islice(in_, K)) _rcv (map(int, line.split()) for line in islice(in_, K)) R1 = R + 1 R1 R + 1 R 1 C1 = C + 1 C1 C + 1 C 1 R2 = R + 2 R2 R + 2 R 2 C2 = C + 2 C2 C + 2 C 2 limit_item = 3 limit_item 3 limit_item1 = limit_item + 1 limit_item1 limit_item + 1 limit_item 1 C2limit_item1 = C2 * limit_item1 C2limit_item1 C2 * limit_item1 C2 limit_item1 grid = [0] * (R2 * C2) grid [0] * (R2 * C2) [0] 0 R2 * C2 R2 C2 for r, c, v in _rcv:
        # grid[r][c] = v
        grid[(r * C2) + c] = v

    
    # dp = [[[0] * limit_item1 for j in range(C2)] for i in range(R2)]
     r c v _rcv grid[(r * C2) + c] = v grid[(r * C2) + c] grid (r * C2) + c r * C2 r C2 c v dp = [0] * (R2 * C2 * limit_item1) dp [0] * (R2 * C2 * limit_item1) [0] 0 R2 * C2 * limit_item1 R2 * C2 R2 C2 limit_item1 dp[(1 * C2limit_item1) + (1 * limit_item1) + 1] = grid[(1 * C2) + 1] dp[(1 * C2limit_item1) + (1 * limit_item1) + 1] dp (1 * C2limit_item1) + (1 * limit_item1) + 1 (1 * C2limit_item1) + (1 * limit_item1) 1 * C2limit_item1 1 C2limit_item1 1 * limit_item1 1 limit_item1 1 grid[(1 * C2) + 1] grid (1 * C2) + 1 1 * C2 1 C2 1 for i in range(1, R1):
        for j in range(1, C1):
            for k in range(limit_item1):
                # current = dp[i][j][k]
                current = dp[(i * C2limit_item1) + (j * limit_item1) + k]

                # left = grid[i][j + 1]
                left = grid[(i * C2) + (j + 1)]
                # dp[i][j + 1][k] = max(dp[i][j + 1][k], current)
                dp[(i * C2limit_item1) + ((j + 1) * limit_item1) + k] = max(dp[(i * C2limit_item1) + ((j + 1) * limit_item1) + k], current)
                if k < limit_item:
                    # dp[i][j + 1][k + 1] = max(dp[i][j + 1][k + 1], left + current)
                    dp[(i * C2limit_item1) + ((j + 1) * limit_item1) + (k + 1)] = max(dp[(i * C2limit_item1) + ((j + 1) * limit_item1) + (k + 1)], left + current)

                # top = grid[i + 1][j]
                top = grid[((i + 1) * C2) + j]
                # dp[i + 1][j][0] = max(dp[i + 1][j][0], current)
                dp[(i + 1) * C2limit_item1 + (j * limit_item1) + 0] = max(dp[(i + 1) * C2limit_item1 + (j * limit_item1) + 0], current)
                # dp[i + 1][j][1] = max(dp[i + 1][j][1], top + current)
                dp[(i + 1) * C2limit_item1 + (j * limit_item1) + 1] = max(dp[(i + 1) * C2limit_item1 + (j * limit_item1) + 1], top + current)
    # return max(dp[R][C])
     i range(1, R1) range 1 R1 for j in range(1, C1):
            for k in range(limit_item1):
                # current = dp[i][j][k]
                current = dp[(i * C2limit_item1) + (j * limit_item1) + k]

                # left = grid[i][j + 1]
                left = grid[(i * C2) + (j + 1)]
                # dp[i][j + 1][k] = max(dp[i][j + 1][k], current)
                dp[(i * C2limit_item1) + ((j + 1) * limit_item1) + k] = max(dp[(i * C2limit_item1) + ((j + 1) * limit_item1) + k], current)
                if k < limit_item:
                    # dp[i][j + 1][k + 1] = max(dp[i][j + 1][k + 1], left + current)
                    dp[(i * C2limit_item1) + ((j + 1) * limit_item1) + (k + 1)] = max(dp[(i * C2limit_item1) + ((j + 1) * limit_item1) + (k + 1)], left + current)

                # top = grid[i + 1][j]
                top = grid[((i + 1) * C2) + j]
                # dp[i + 1][j][0] = max(dp[i + 1][j][0], current)
                dp[(i + 1) * C2limit_item1 + (j * limit_item1) + 0] = max(dp[(i + 1) * C2limit_item1 + (j * limit_item1) + 0], current)
                # dp[i + 1][j][1] = max(dp[i + 1][j][1], top + current)
                dp[(i + 1) * C2limit_item1 + (j * limit_item1) + 1] = max(dp[(i + 1) * C2limit_item1 + (j * limit_item1) + 1], top + current)
    # return max(dp[R][C])
     j range(1, C1) range 1 C1 for k in range(limit_item1):
                # current = dp[i][j][k]
                current = dp[(i * C2limit_item1) + (j * limit_item1) + k]

                # left = grid[i][j + 1]
                left = grid[(i * C2) + (j + 1)]
                # dp[i][j + 1][k] = max(dp[i][j + 1][k], current)
                dp[(i * C2limit_item1) + ((j + 1) * limit_item1) + k] = max(dp[(i * C2limit_item1) + ((j + 1) * limit_item1) + k], current)
                if k < limit_item:
                    # dp[i][j + 1][k + 1] = max(dp[i][j + 1][k + 1], left + current)
                    dp[(i * C2limit_item1) + ((j + 1) * limit_item1) + (k + 1)] = max(dp[(i * C2limit_item1) + ((j + 1) * limit_item1) + (k + 1)], left + current)

                # top = grid[i + 1][j]
                top = grid[((i + 1) * C2) + j]
                # dp[i + 1][j][0] = max(dp[i + 1][j][0], current)
                dp[(i + 1) * C2limit_item1 + (j * limit_item1) + 0] = max(dp[(i + 1) * C2limit_item1 + (j * limit_item1) + 0], current)
                # dp[i + 1][j][1] = max(dp[i + 1][j][1], top + current)
                dp[(i + 1) * C2limit_item1 + (j * limit_item1) + 1] = max(dp[(i + 1) * C2limit_item1 + (j * limit_item1) + 1], top + current)
    # return max(dp[R][C])
     k range(limit_item1) range limit_item1 current = dp[(i * C2limit_item1) + (j * limit_item1) + k] current dp[(i * C2limit_item1) + (j * limit_item1) + k] dp (i * C2limit_item1) + (j * limit_item1) + k (i * C2limit_item1) + (j * limit_item1) i * C2limit_item1 i C2limit_item1 j * limit_item1 j limit_item1 k left = grid[(i * C2) + (j + 1)] left grid[(i * C2) + (j + 1)] grid (i * C2) + (j + 1) i * C2 i C2 j + 1 j 1 dp[(i * C2limit_item1) + ((j + 1) * limit_item1) + k] = max(dp[(i * C2limit_item1) + ((j + 1) * limit_item1) + k], current) dp[(i * C2limit_item1) + ((j + 1) * limit_item1) + k] dp (i * C2limit_item1) + ((j + 1) * limit_item1) + k (i * C2limit_item1) + ((j + 1) * limit_item1) i * C2limit_item1 i C2limit_item1 (j + 1) * limit_item1 j + 1 j 1 limit_item1 k max(dp[(i * C2limit_item1) + ((j + 1) * limit_item1) + k], current) max dp[(i * C2limit_item1) + ((j + 1) * limit_item1) + k] dp (i * C2limit_item1) + ((j + 1) * limit_item1) + k (i * C2limit_item1) + ((j + 1) * limit_item1) i * C2limit_item1 i C2limit_item1 (j + 1) * limit_item1 j + 1 j 1 limit_item1 k current if k < limit_item:
                    # dp[i][j + 1][k + 1] = max(dp[i][j + 1][k + 1], left + current)
                    dp[(i * C2limit_item1) + ((j + 1) * limit_item1) + (k + 1)] = max(dp[(i * C2limit_item1) + ((j + 1) * limit_item1) + (k + 1)], left + current)

                # top = grid[i + 1][j]
                 k < limit_item k limit_item dp[(i * C2limit_item1) + ((j + 1) * limit_item1) + (k + 1)] = max(dp[(i * C2limit_item1) + ((j + 1) * limit_item1) + (k + 1)], left + current) dp[(i * C2limit_item1) + ((j + 1) * limit_item1) + (k + 1)] dp (i * C2limit_item1) + ((j + 1) * limit_item1) + (k + 1) (i * C2limit_item1) + ((j + 1) * limit_item1) i * C2limit_item1 i C2limit_item1 (j + 1) * limit_item1 j + 1 j 1 limit_item1 k + 1 k 1 max(dp[(i * C2limit_item1) + ((j + 1) * limit_item1) + (k + 1)], left + current) max dp[(i * C2limit_item1) + ((j + 1) * limit_item1) + (k + 1)] dp (i * C2limit_item1) + ((j + 1) * limit_item1) + (k + 1) (i * C2limit_item1) + ((j + 1) * limit_item1) i * C2limit_item1 i C2limit_item1 (j + 1) * limit_item1 j + 1 j 1 limit_item1 k + 1 k 1 left + current left current top = grid[((i + 1) * C2) + j] top grid[((i + 1) * C2) + j] grid ((i + 1) * C2) + j (i + 1) * C2 i + 1 i 1 C2 j dp[(i + 1) * C2limit_item1 + (j * limit_item1) + 0] = max(dp[(i + 1) * C2limit_item1 + (j * limit_item1) + 0], current) dp[(i + 1) * C2limit_item1 + (j * limit_item1) + 0] dp (i + 1) * C2limit_item1 + (j * limit_item1) + 0 (i + 1) * C2limit_item1 + (j * limit_item1) (i + 1) * C2limit_item1 i + 1 i 1 C2limit_item1 j * limit_item1 j limit_item1 0 max(dp[(i + 1) * C2limit_item1 + (j * limit_item1) + 0], current) max dp[(i + 1) * C2limit_item1 + (j * limit_item1) + 0] dp (i + 1) * C2limit_item1 + (j * limit_item1) + 0 (i + 1) * C2limit_item1 + (j * limit_item1) (i + 1) * C2limit_item1 i + 1 i 1 C2limit_item1 j * limit_item1 j limit_item1 0 current dp[(i + 1) * C2limit_item1 + (j * limit_item1) + 1] = max(dp[(i + 1) * C2limit_item1 + (j * limit_item1) + 1], top + current) dp[(i + 1) * C2limit_item1 + (j * limit_item1) + 1] dp (i + 1) * C2limit_item1 + (j * limit_item1) + 1 (i + 1) * C2limit_item1 + (j * limit_item1) (i + 1) * C2limit_item1 i + 1 i 1 C2limit_item1 j * limit_item1 j limit_item1 1 max(dp[(i + 1) * C2limit_item1 + (j * limit_item1) + 1], top + current) max dp[(i + 1) * C2limit_item1 + (j * limit_item1) + 1] dp (i + 1) * C2limit_item1 + (j * limit_item1) + 1 (i + 1) * C2limit_item1 + (j * limit_item1) (i + 1) * C2limit_item1 i + 1 i 1 C2limit_item1 j * limit_item1 j limit_item1 1 top + current top current return max(dp[(R * C2limit_item1) + (C * limit_item1):((R + 1) * C2limit_item1) + ((C + 1) * limit_item1)]) max(dp[(R * C2limit_item1) + (C * limit_item1):((R + 1) * C2limit_item1) + ((C + 1) * limit_item1)]) max dp[(R * C2limit_item1) + (C * limit_item1):((R + 1) * C2limit_item1) + ((C + 1) * limit_item1)] dp (R * C2limit_item1) + (C * limit_item1):((R + 1) * C2limit_item1) + ((C + 1) * limit_item1) (R * C2limit_item1) + (C * limit_item1) R * C2limit_item1 R C2limit_item1 C * limit_item1 C limit_item1 ((R + 1) * C2limit_item1) + ((C + 1) * limit_item1) (R + 1) * C2limit_item1 R + 1 R 1 C2limit_item1 (C + 1) * limit_item1 C + 1 C 1 limit_item1 in_ in_ def main():
    answer = resolve(sys.stdin.buffer)
    print(answer) main answer = resolve(sys.stdin.buffer) answer resolve(sys.stdin.buffer) resolve sys.stdin.buffer sys.stdin sys stdin buffer print(answer) print answer if __name__ == '__main__':
    main() __name__ == '__main__' __name__ '__main__' main() main def main():
    answer = resolve(sys.stdin.buffer)
    print(answer) def main():
    answer = resolve(sys.stdin.buffer)
    print(answer) main def resolve(in_):
    R, C, K = map(int, next(in_).split())
    _rcv = (map(int, line.split()) for line in islice(in_, K))

    R1 = R + 1
    C1 = C + 1
    R2 = R + 2
    C2 = C + 2
    limit_item = 3
    limit_item1 = limit_item + 1
    C2limit_item1 = C2 * limit_item1

    # grid = [[0] * C2 for _ in range(R2)]
    grid = [0] * (R2 * C2)
    for r, c, v in _rcv:
        # grid[r][c] = v
        grid[(r * C2) + c] = v

    
    # dp = [[[0] * limit_item1 for j in range(C2)] for i in range(R2)]
    dp = [0] * (R2 * C2 * limit_item1)
    # dp[1][1][1] = grid[1][1]
    dp[(1 * C2limit_item1) + (1 * limit_item1) + 1] = grid[(1 * C2) + 1]

    for i in range(1, R1):
        for j in range(1, C1):
            for k in range(limit_item1):
                # current = dp[i][j][k]
                current = dp[(i * C2limit_item1) + (j * limit_item1) + k]

                # left = grid[i][j + 1]
                left = grid[(i * C2) + (j + 1)]
                # dp[i][j + 1][k] = max(dp[i][j + 1][k], current)
                dp[(i * C2limit_item1) + ((j + 1) * limit_item1) + k] = max(dp[(i * C2limit_item1) + ((j + 1) * limit_item1) + k], current)
                if k < limit_item:
                    # dp[i][j + 1][k + 1] = max(dp[i][j + 1][k + 1], left + current)
                    dp[(i * C2limit_item1) + ((j + 1) * limit_item1) + (k + 1)] = max(dp[(i * C2limit_item1) + ((j + 1) * limit_item1) + (k + 1)], left + current)

                # top = grid[i + 1][j]
                top = grid[((i + 1) * C2) + j]
                # dp[i + 1][j][0] = max(dp[i + 1][j][0], current)
                dp[(i + 1) * C2limit_item1 + (j * limit_item1) + 0] = max(dp[(i + 1) * C2limit_item1 + (j * limit_item1) + 0], current)
                # dp[i + 1][j][1] = max(dp[i + 1][j][1], top + current)
                dp[(i + 1) * C2limit_item1 + (j * limit_item1) + 1] = max(dp[(i + 1) * C2limit_item1 + (j * limit_item1) + 1], top + current)
    # return max(dp[R][C])
    return max(dp[(R * C2limit_item1) + (C * limit_item1):((R + 1) * C2limit_item1) + ((C + 1) * limit_item1)]) def resolve(in_):
    R, C, K = map(int, next(in_).split())
    _rcv = (map(int, line.split()) for line in islice(in_, K))

    R1 = R + 1
    C1 = C + 1
    R2 = R + 2
    C2 = C + 2
    limit_item = 3
    limit_item1 = limit_item + 1
    C2limit_item1 = C2 * limit_item1

    # grid = [[0] * C2 for _ in range(R2)]
    grid = [0] * (R2 * C2)
    for r, c, v in _rcv:
        # grid[r][c] = v
        grid[(r * C2) + c] = v

    
    # dp = [[[0] * limit_item1 for j in range(C2)] for i in range(R2)]
    dp = [0] * (R2 * C2 * limit_item1)
    # dp[1][1][1] = grid[1][1]
    dp[(1 * C2limit_item1) + (1 * limit_item1) + 1] = grid[(1 * C2) + 1]

    for i in range(1, R1):
        for j in range(1, C1):
            for k in range(limit_item1):
                # current = dp[i][j][k]
                current = dp[(i * C2limit_item1) + (j * limit_item1) + k]

                # left = grid[i][j + 1]
                left = grid[(i * C2) + (j + 1)]
                # dp[i][j + 1][k] = max(dp[i][j + 1][k], current)
                dp[(i * C2limit_item1) + ((j + 1) * limit_item1) + k] = max(dp[(i * C2limit_item1) + ((j + 1) * limit_item1) + k], current)
                if k < limit_item:
                    # dp[i][j + 1][k + 1] = max(dp[i][j + 1][k + 1], left + current)
                    dp[(i * C2limit_item1) + ((j + 1) * limit_item1) + (k + 1)] = max(dp[(i * C2limit_item1) + ((j + 1) * limit_item1) + (k + 1)], left + current)

                # top = grid[i + 1][j]
                top = grid[((i + 1) * C2) + j]
                # dp[i + 1][j][0] = max(dp[i + 1][j][0], current)
                dp[(i + 1) * C2limit_item1 + (j * limit_item1) + 0] = max(dp[(i + 1) * C2limit_item1 + (j * limit_item1) + 0], current)
                # dp[i + 1][j][1] = max(dp[i + 1][j][1], top + current)
                dp[(i + 1) * C2limit_item1 + (j * limit_item1) + 1] = max(dp[(i + 1) * C2limit_item1 + (j * limit_item1) + 1], top + current)
    # return max(dp[R][C])
    return max(dp[(R * C2limit_item1) + (C * limit_item1):((R + 1) * C2limit_item1) + ((C + 1) * limit_item1)]) resolve
import sys
from itertools import islice


def resolve(in_):
    R, C, K = map(int, next(in_).split())
    _rcv = (map(int, line.split()) for line in islice(in_, K))

    R1 = R + 1
    C1 = C + 1
    R2 = R + 2
    C2 = C + 2
    limit_item = 3
    limit_item1 = limit_item + 1
    C2limit_item1 = C2 * limit_item1

    # grid = [[0] * C2 for _ in range(R2)]
    grid = [0] * (R2 * C2)
    for r, c, v in _rcv:
        # grid[r][c] = v
        grid[(r * C2) + c] = v

    
    # dp = [[[0] * limit_item1 for j in range(C2)] for i in range(R2)]
    dp = [0] * (R2 * C2 * limit_item1)
    # dp[1][1][1] = grid[1][1]
    dp[(1 * C2limit_item1) + (1 * limit_item1) + 1] = grid[(1 * C2) + 1]

    for i in range(1, R1):
        for j in range(1, C1):
            for k in range(limit_item1):
                # current = dp[i][j][k]
                current = dp[(i * C2limit_item1) + (j * limit_item1) + k]

                # left = grid[i][j + 1]
                left = grid[(i * C2) + (j + 1)]
                # dp[i][j + 1][k] = max(dp[i][j + 1][k], current)
                dp[(i * C2limit_item1) + ((j + 1) * limit_item1) + k] = max(dp[(i * C2limit_item1) + ((j + 1) * limit_item1) + k], current)
                if k < limit_item:
                    # dp[i][j + 1][k + 1] = max(dp[i][j + 1][k + 1], left + current)
                    dp[(i * C2limit_item1) + ((j + 1) * limit_item1) + (k + 1)] = max(dp[(i * C2limit_item1) + ((j + 1) * limit_item1) + (k + 1)], left + current)

                # top = grid[i + 1][j]
                top = grid[((i + 1) * C2) + j]
                # dp[i + 1][j][0] = max(dp[i + 1][j][0], current)
                dp[(i + 1) * C2limit_item1 + (j * limit_item1) + 0] = max(dp[(i + 1) * C2limit_item1 + (j * limit_item1) + 0], current)
                # dp[i + 1][j][1] = max(dp[i + 1][j][1], top + current)
                dp[(i + 1) * C2limit_item1 + (j * limit_item1) + 1] = max(dp[(i + 1) * C2limit_item1 + (j * limit_item1) + 1], top + current)
    # return max(dp[R][C])
    return max(dp[(R * C2limit_item1) + (C * limit_item1):((R + 1) * C2limit_item1) + ((C + 1) * limit_item1)])


def main():
    answer = resolve(sys.stdin.buffer)
    print(answer)


if __name__ == '__main__':
    main()
