Instead, you can use a loop and define boundaries once. Sort of:
int startRow = max(row - 1, 0);
int endRow = min(row + 1, width - 1);
int startCol = max(col - 1, 0);
int endCol = min(col + 1, height - 1);
for (int r = startRow; r <= endRow; r++)
for (int c = startCol; c <= endCol; c++)
if (r != row || c != col)
ray[r][c] += 1;
Alternatively, if the operation is reversible (as in this code, you add 1), you can simply cancel the operation for the middle cell after the loop. This would be more efficient, since it eliminates (no more) 12 comparisons if the operation itself is simple:
int startRow = max(row - 1, 0);
int endRow = min(row + 1, width - 1);
int startCol = max(col - 1, 0);
int endCol = min(col + 1, height - 1);
for (int r = startRow; r <= endRow; r++)
for (int c = startCol; c <= endCol; c++)
ray[r][c] += 1;
ray[row][col] -= 1;