public static int FindMarioWay(int[][] matrix)
int rows = matrix.Length;
int cols = matrix[0].Length;
int[,] d = new int[rows, cols];
for (int i = 1; i < rows; i++)
d[i, 0] = d[i - 1, 0] + matrix[i][0];
for (int i = 1; i < cols; i++)
d[0, i] = d[0, i - 1] + matrix[0][i];
for (int i = 1; i < rows; i++)
for (int j = 1; j < cols; j++)
d[i, j] = Math.Min(d[i - 1, j], d[i, j - 1]) + matrix[i][j];
return d[rows - 1, cols - 1];
public static void Main()
Console.WriteLine("UniLecs");
new int[] { 5, 9, 4, 3 },
new int[] { 3, 1, 6, 9 },
new int[] { 8, 6, 8, 12 }
Console.WriteLine(FindMarioWay(matrix));