@vaibhav15 You are right. I think if I check the min distance for each result which can save the time. But for this question, I prefer the DP solution now. Please check this:

public class Solution
{
public int[,] UpdateMatrix(int[,] matrix)
{
var row = matrix.GetLength(0);
var col = matrix.GetLength(1);
var dp = new int[row, col];
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
dp[i, j] = int.MaxValue - 10000;
}
}
// TOP-LEFT
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
if (matrix[i, j] == 0)
{
dp[i, j] = 0;
}
else
{
if (i > 0) dp[i, j] = Math.Min(dp[i, j], dp[i-1, j] + 1);
if (j > 0) dp[i, j] = Math.Min(dp[i, j], dp[i, j-1] + 1);
}
}
}
// BOTTOM-RIGHT
for (int i = row - 1; i >= 0; i--)
{
for (int j = col - 1; j >= 0; j--)
{
if (i < row - 1) dp[i, j] = Math.Min(dp[i, j], dp[i+1, j] + 1);
if (j < col - 1) dp[i, j] = Math.Min(dp[i, j], dp[i, j+1] + 1);
}
}
return dp;
}
}