You can use the Range.SpecialCells method on the sheet to find the last cell. Then you can modify your loop to exit when you move beyond the last row or column. The following snippet will give your the row and column number of the last cell. You may still process some empty cells, but you won't be going all the way down to row 1048574.
Excel.Range last = Globals.Sheet1.Cells.SpecialCells(Excel.XlCellType.xlCellTypeLastCell, Type.Missing);
int lastRowNumber = last.Row;
int lastColumnNumber = last.Column;