#include <bits/stdc++.h>

using namespace std;

int dp[1010][1010];

int main(){
   int n, m; cin >> n >> m;
   vector<vector<int>> matrix(n, vector<int>(m, 0));

   for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
         cin >> matrix[i][j];
      }
   }

   memset(dp, 0, sizeof(dp));

   dp[0][0] = matrix[0][0];
   for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
         if (i == 0 && j == 0) continue;

         int min_top = INT_MAX, min_left = INT_MAX;
         if (i - 1 >= 0) {
            min_top = dp[i - 1][j];
         }
         if (j - 1 >= 0) {
            min_left = dp[i][j - 1];
         }

         dp[i][j] = min(min_top, min_left) + matrix[i][j];
      }
   }

   cout << dp[n - 1][m - 1] << endl;

   return 0;
}