#include #include using namespace std; static vector> memo; static vector> visited; void rec(vector>& pole, int larvy, int x, int y, int n, int m) { if (x >= n || x < 0 || y >= m || y < 0) return; if (visited[x][y]) return; int larvyteraz = larvy + pole[x][y]; if (x == n - 1 && y == m - 1) { if (larvyteraz > memo[x][y]) memo[x][y] = larvyteraz; return; } if (larvyteraz > memo[x][y]) { memo[x][y] = larvyteraz; } visited[x][y] = true; rec(pole, larvyteraz, x - 1, y, n, m); rec(pole, larvyteraz, x, y + 1, n, m); rec(pole, larvyteraz, x + 1, y, n, m); rec(pole, larvyteraz, x, y - 1, n, m); visited[x][y] = false; return; } int main() { int n, m; cin >> n >> m; vector> pole = vector>(n,vector(m)); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { cin >> pole[i][j]; } } visited = vector>(n, vector(m)); memo = vector>(n, vector(m)); rec(pole, 0, 0, 0, n, m); cout << memo[n - 1][m - 1]; return 0; }