#include <bits/stdc++.h>
using namespace std;
int n, m, a[1005][1005], dp[1005][1005],dp1[1005][1005];
int main() {
cin >> n >> m;
freopen("A.in", "r", stdin);
freopen("A.out", "w", stdout);
memset(dp, 0, sizeof(dp));
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
cin >> a[i][j];
}
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (i == 1)
dp[i][j] = dp[i][j - 1] + a[i][j];
else if (j == 1)
dp[i][j] = dp[i - 1][j] + a[i][j];
else
dp[i][j] = min(dp[i - 1][j], dp[i][j - 1]) + a[i][j];
}
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (i == 1)
dp1[i][j] = dp1[i][j - 1] + a[i][j];
else if (j == 1)
dp1[i][j] = dp1[i - 1][j] + a[i][j];
else
dp1[i][j] = max(dp1[i - 1][j], dp1[i][j - 1]) + a[i][j];
}
}
cout << dp[n][m]<<endl<<dp1[n][m];
return 0;
}