let h = 5
let w = 5

let grid = [|
  [| 1; 1; 0; 1; 1 |];
  [| 1; 0; 0; 0; 1 |];
  [| 0; 0; 1; 0; 0 |];
  [| 1; 1; 1; 1; 0 |];
  [| 0; 0; 0; 1; 1 |]
|]

let rec flood visited r c =
  if r < 0 || r >= h || c < 0 || c >= w then 0
  else if visited.(r).(c) || grid.(r).(c) = 0 then 0
  else begin
    visited.(r).(c) <- true;
    1 + flood visited (r - 1) c
      + flood visited (r + 1) c
      + flood visited r (c - 1)
      + flood visited r (c + 1)
  end

let largest_component () =
  let visited = Array.init h (fun _ -> Array.make w false) in
  let best = ref 0 in
  for r = 0 to h - 1 do
    for c = 0 to w - 1 do
      if grid.(r).(c) = 1 && not visited.(r).(c) then begin
        let s = flood visited r c in
        if s > !best then best := s
      end
    done
  done;
  !best

;;

largest_component ()
