// Complete the maxRegion function below. static int maxRegion(int[][] matrix) { var xOffset = new[] { -1, 0, 1, -1 }; var yOffset = new[] { -1, -1, -1, 0 }; var ds = new DisjointSets(); var rows = matrix.Length; var cols = matrix[0].Length; var max = 0; Func <int, int, bool> isValid = (int x, int y) => { return(x >= 0 && x < cols && y >= 0 && y < rows); }; for (int y = 0; y < rows; y++) { for (int x = 0; x < cols; x++) { if (matrix[y][x] == 0) { continue; } var current = new CellKey(x, y); ds.AddNew(current); max = Math.Max(max, 1); for (int index = 0; index < xOffset.Length; index++) { var ox = x + xOffset[index]; var oy = y + yOffset[index]; if (!isValid(ox, oy)) { continue; } if (matrix[oy][ox] == 0) { continue; } var offset = new CellKey(ox, oy); if (ds.Find(current) != ds.Find(offset)) { max = Math.Max(max, ds.Union(current, offset)); } } } } return(max); }