# LC medium, matrix set zeros, accepted answer but not top

I've solved LC medium problem, set zeros. Below code is accepted and all test cases were passing. But my code came in the last 10% of accepted answers wrt performance. Can someone help me identify optimization opportunities? This is in C# and memory footprint is fine.

public class Solution {
public void SetZeroes(int[][] matrix) {

bool zeroRow = false;
bool zeroCol = false;

for (int i = 0;i<matrix.Length;++i)
{
for (int j = 0; j < matrix[0].Length; ++j)
{
if (matrix[i][j] == 0)
{
if (i == 0)
zeroRow = true;
if (j == 0)
zeroCol = true;

matrix[i][0] = 0;
matrix[0][j] = 0;
}

}

}

for (int i = 1; i < matrix.Length; ++i)
{
for (int j = 1; j < matrix[0].Length; ++j)
{
if (matrix[i][0] == 0 || matrix[0][j] == 0)
{
matrix[i][j] = 0;
}

}

}

Console.Write('[');
for (int i = 0; i < matrix.Length; i++)
{
if (zeroCol) { matrix[i][0] = 0; }
Console.Write('[');
for (int j = 0; j < matrix[0].Length; j++)
{
if (zeroRow) { matrix[0][j] = 0; }
Console.Write(matrix[i][j]);
if(j!=matrix[0].Length-1)
Console.Write(',');
}
Console.Write("]");
if (i != matrix.Length-1)
Console.Write(',');
}
Console.Write(']');
}
}

• The current question title, which states your concerns about the code, is too general to be useful here. Please edit to the site standard, which is for the title to simply state the task accomplished by the code. I'm afraid "set zeros" means pretty much nothing as a program requirement - can you be more specific? Please see How to get the best value out of Code Review: Asking Questions for guidance on writing good question titles. – Toby Speight Feb 27 at 17:44
• Are you talking about the following problem? leetcode.com/problems/set-matrix-zeroes if so, it looks like the matrix should be updated in memory so I am not sure why you need the Console.Write (that might affect performance) – Yumei De Armas Feb 27 at 21:35
• @YumeiDeArmas i think you might be right, let me try. – rplusg Feb 28 at 5:29
• @YumeiDeArmas That is the problem, thank you. – rplusg Feb 28 at 6:53