I have a 2d array (a matrix) and I need to find the max sum that can be collected by starting at any position and going down right or down left until I reach an end. I must give an iterative solution.

This is my code
static int maxValue(double[][] field, int posR, int posC) {
int r = field.length;
int c = field[0].length;
int sum = 0;
double[][] temp = new double[r][c];
for (int i = posR; i < r; i++) {
for (int j = posC; j < c; j++) {
if (i == posR && j == posC) {
temp[i][j] = field[posR][posC];
posR++; posC++;
} else if (i == field.length-1) {
temp[i][j] = field[i][j];
break;
} else if (j == field.length-1) {
temp[i][j] = field[i][j];
break;
} else {
temp[i][j] = Math.max(field[i+1][j-1], field[i+1][j+1]);
}
}
}
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
sum += temp[i][j];
}
}
return sum;
}
}