from itertools import combinations, product from typing import Any def main(): H, W = map(int, input().split()) board = [list(map(int, input().split())) for _ in range(H)] beam_ranges: list[set[Any]] = [] beam_sum: list[int] = [] for i in range(H): mass = set() sum_ = 0 for j in range(W): mass.add((i, j)) sum_ += board[i][j] beam_ranges.append(mass) beam_sum.append(sum_) for j in range(W): mass = set() sum_ = 0 for i in range(H): mass.add((i, j)) sum_ += board[i][j] beam_ranges.append(mass) beam_sum.append(sum_) for i in range(H + W - 1): mass = set() sum_ = 0 if i < H: cur_h = i cur_w = 0 else: cur_h = H - 1 cur_w = i - H + 1 while cur_h >= 0 and cur_w < W: mass.add((cur_h, cur_w)) sum_ += board[cur_h][cur_w] cur_h -= 1 cur_w += 1 beam_ranges.append(mass) beam_sum.append(sum_) for i in range(H + W - 1): mass = set() sum_ = 0 if i < H: cur_h = H - 1 - i cur_w = 0 else: cur_h = 0 cur_w = i - H + 1 while cur_h < H and cur_w < W: mass.add((cur_h, cur_w)) sum_ += board[cur_h][cur_w] cur_h += 1 cur_w += 1 beam_ranges.append(mass) beam_sum.append(sum_) max_sum = max(beam_sum) for i, j in combinations(range(len(beam_ranges)), 2): tmp_sum = beam_sum[i] + beam_sum[j] multi = beam_ranges[i].intersection(beam_ranges[j]) for multi_mass in multi: tmp_sum -= board[multi_mass[0]][multi_mass[1]] max_sum = max(max_sum, tmp_sum) print(max_sum) if __name__ == "__main__": main()