Max Area of Island - Largest in Boolean Matrix

Last Updated : 7 Sep, 2026

Given a grid of dimensions n x m containing 0's and 1's. Find the count of 1's in the largest region of 1's. A region of 1's is a group of 1's where two 1s can be adjacent to each other in any of the 8 directions (2 horizontal, 2 vertical and 4 diagonals). 

Examples:

Input: grid[][]= [[1, 0, 0, 0, 1, 0, 0], [0, 1, 0, 0, 1, 1, 0], [1, 1, 0, 0, 0, 0, 0], [1, 0, 0, 1, 1, 0, 0], [1, 0, 0, 1, 0, 1, 1]]
Output: 6
Explanation: The region in red has the largest area of 6 cells.

Max-Area-of-Island

Input: grid[][] = [[1, 1, 1, 0], [0, 0, 1, 0], [0, 0, 0, 1], [1, 1, 0, 0]]
Output: 5
Explanation: The largest region has five 1s.

Try It Yourself
redirect icon

Using DFS - O(n * m) Time and O(n * m) Space

The idea is to use DFS to explore each connected region of 1s in the grid. Since diagonal cells are also considered connected, we check all 8 neighbouring directions. Whenever we find an unvisited 1, we run DFS, count its cells, and keep track of the maximum region area.

  • Traverse every cell of the grid.
  • If the current cell contains 1, start a DFS from that cell.
  • Mark the visited cell as 0 and increment the current region's area.
  • Recursively visit all valid 1 cells among the 8 neighbouring directions.
  • After DFS completes, update maxArea with the current region's area.
  • Return maxArea after processing the entire grid.
C++
#include <bits/stdc++.h>
using namespace std;

// Checks whether the cell is valid and contains 1.
bool isSafe(vector<vector<int>> &grid, int r, int c, int rows, int cols)
{
    return r >= 0 && r < rows && c >= 0 && c < cols && grid[r][c] == 1;
}

// DFS to find the area of the current region.
void DFS(vector<vector<int>> &grid, int r, int c, int rows, int cols, int &area)
{
    // Directions for all 8 neighbouring cells.
    int dr[] = {-1, -1, -1, 0, 0, 1, 1, 1};
    int dc[] = {-1, 0, 1, -1, 1, -1, 0, 1};

    // Mark the current cell as visited.
    grid[r][c] = 0;

    // Increase the area of the current region.
    area++;

    // Visit all 8 neighbouring cells.
    for (int i = 0; i < 8; i++)
    {
        int nr = r + dr[i];
        int nc = c + dc[i];

        if (isSafe(grid, nr, nc, rows, cols))
            DFS(grid, nr, nc, rows, cols, area);
    }
}

// Returns the area of the largest region of 1s.
int largestRegion(vector<vector<int>> &grid)
{
    int rows = grid.size();
    int cols = grid[0].size();

    int maxArea = 0;

    // Traverse every cell of the grid.
    for (int r = 0; r < rows; r++)
    {
        for (int c = 0; c < cols; c++)
        {
            // Start DFS if an unvisited 1 is found.
            if (grid[r][c] == 1)
            {
                int area = 0;

                DFS(grid, r, c, rows, cols, area);

                // Update the maximum region area.
                maxArea = max(maxArea, area);
            }
        }
    }

    return maxArea;
}

int main()
{
    vector<vector<int>> grid = {{1, 0, 0, 0, 1, 0, 0},
                                {0, 1, 0, 0, 1, 1, 1},
                                {1, 1, 0, 0, 0, 0, 0},
                                {1, 0, 0, 1, 1, 0, 0},
                                {1, 0, 0, 1, 0, 1, 1}};

    cout << largestRegion(grid) << endl;

    return 0;
}
C
#include <stdio.h>

// Checks whether the cell is valid and contains 1.
int isSafe(int grid[][7], int r, int c, int rows, int cols)
{
    return r >= 0 && r < rows && c >= 0 && c < cols && grid[r][c] == 1;
}

// DFS to find the area of the current region.
void DFS(int grid[][7], int r, int c, int rows, int cols, int *area)
{
    // Directions for all 8 neighbouring cells.
    int dr[] = {-1, -1, -1, 0, 0, 1, 1, 1};
    int dc[] = {-1, 0, 1, -1, 1, -1, 0, 1};

    // Mark the current cell as visited.
    grid[r][c] = 0;

    // Increase the area of the current region.
    (*area)++;

    // Visit all 8 neighbouring cells.
    for (int i = 0; i < 8; i++)
    {
        int nr = r + dr[i];
        int nc = c + dc[i];

        if (isSafe(grid, nr, nc, rows, cols))
            DFS(grid, nr, nc, rows, cols, area);
    }
}

// Returns the area of the largest region of 1s.
int largestRegion(int grid[][7], int rows, int cols)
{
    int maxArea = 0;

    // Traverse every cell of the grid.
    for (int r = 0; r < rows; r++)
    {
        for (int c = 0; c < cols; c++)
        {
            // Start DFS if an unvisited 1 is found.
            if (grid[r][c] == 1)
            {
                int area = 0;

                DFS(grid, r, c, rows, cols, &area);

                // Update the maximum region area.
                if (area > maxArea)
                    maxArea = area;
            }
        }
    }

    return maxArea;
}

int main()
{
    int grid[5][7] = {{1, 0, 0, 0, 1, 0, 0},
                      {0, 1, 0, 0, 1, 1, 1},
                      {1, 1, 0, 0, 0, 0, 0},
                      {1, 0, 0, 1, 1, 0, 0},
                      {1, 0, 0, 1, 0, 1, 1}};

    printf("%d\n", largestRegion(grid, 5, 7));

    return 0;
}
Java
class GFG {
    
    // Checks whether the cell is valid and contains 1.
    static boolean isSafe(int[][] grid, int r, int c,
                          int rows, int cols)
    {
        return r >= 0 && r < rows && c >= 0 && c < cols
            && grid[r][c] == 1;
    }

    // DFS to find the area of the current region.
    static int DFS(int[][] grid, int r, int c, int rows,
                   int cols)
    {
        // Directions for all 8 neighbouring cells.
        int[] dr = { -1, -1, -1, 0, 0, 1, 1, 1 };
        int[] dc = { -1, 0, 1, -1, 1, -1, 0, 1 };

        // Mark the current cell as visited.
        grid[r][c] = 0;

        // Increase the area of the current region.
        int area = 1;

        // Visit all 8 neighbouring cells.
        for (int i = 0; i < 8; i++) {
            int nr = r + dr[i];
            int nc = c + dc[i];

            if (isSafe(grid, nr, nc, rows, cols))
                area += DFS(grid, nr, nc, rows, cols);
        }

        return area;
    }

    // Returns the area of the largest region of 1s.
    static int largestRegion(int[][] grid)
    {
        int rows = grid.length;
        int cols = grid[0].length;

        int maxArea = 0;

        // Traverse every cell of the grid.
        for (int r = 0; r < rows; r++) {
            for (int c = 0; c < cols; c++) {
                
                // Start DFS if an unvisited 1 is found.
                if (grid[r][c] == 1) {
                    int area = DFS(grid, r, c, rows, cols);

                    // Update the maximum region area.
                    maxArea = Math.max(maxArea, area);
                }
            }
        }

        return maxArea;
    }

    public static void main(String[] args)
    {
        int[][] grid = { { 1, 0, 0, 0, 1, 0, 0 },
                         { 0, 1, 0, 0, 1, 1, 1 },
                         { 1, 1, 0, 0, 0, 0, 0 },
                         { 1, 0, 0, 1, 1, 0, 0 },
                         { 1, 0, 0, 1, 0, 1, 1 } };

        System.out.println(largestRegion(grid));
    }
}
Python
# Checks whether the cell is valid and contains 1.
def isSafe(grid, r, c, rows, cols):
    return (r >= 0 and r < rows and
            c >= 0 and c < cols and
            grid[r][c] == 1)


# DFS to find the area of the current region.
def DFS(grid, r, c, rows, cols):

    # Directions for all 8 neighbouring cells.
    dr = [-1, -1, -1, 0, 0, 1, 1, 1]
    dc = [-1, 0, 1, -1, 1, -1, 0, 1]

    # Mark the current cell as visited.
    grid[r][c] = 0

    # Increase the area of the current region.
    area = 1

    # Visit all 8 neighbouring cells.
    for i in range(8):
        nr = r + dr[i]
        nc = c + dc[i]

        if isSafe(grid, nr, nc, rows, cols):
            area += DFS(grid, nr, nc, rows, cols)

    return area


# Returns the area of the largest region of 1s.
def largestRegion(grid):
    rows = len(grid)
    cols = len(grid[0])

    maxArea = 0

    # Traverse every cell of the grid.
    for r in range(rows):
        for c in range(cols):

            # Start DFS if an unvisited 1 is found.
            if grid[r][c] == 1:
                area = DFS(grid, r, c, rows, cols)

                # Update the maximum region area.
                maxArea = max(maxArea, area)

    return maxArea


# Driver Code
if __name__ == "__main__":
    grid = [
        [1, 0, 0, 0, 1, 0, 0],
        [0, 1, 0, 0, 1, 1, 1],
        [1, 1, 0, 0, 0, 0, 0],
        [1, 0, 0, 1, 1, 0, 0],
        [1, 0, 0, 1, 0, 1, 1]
    ]

    print(largestRegion(grid))
C#
using System;

class GFG {
    
    // Checks whether the cell is valid and contains 1.
    static bool isSafe(int[, ] grid, int r, int c, int rows, int cols)
    {
        return r >= 0 && r < rows && c >= 0 && c < cols
            && grid[r, c] == 1;
    }

    // DFS to find the area of the current region.
    static int DFS(int[, ] grid, int r, int c, int rows, int cols)
    {
        // Directions for all 8 neighbouring cells.
        int[] dr = { -1, -1, -1, 0, 0, 1, 1, 1 };
        int[] dc = { -1, 0, 1, -1, 1, -1, 0, 1 };

        // Mark the current cell as visited.
        grid[r, c] = 0;

        // Increase the area of the current region.
        int area = 1;

        // Visit all 8 neighbouring cells.
        for (int i = 0; i < 8; i++) {
            int nr = r + dr[i];
            int nc = c + dc[i];

            if (isSafe(grid, nr, nc, rows, cols))
                area += DFS(grid, nr, nc, rows, cols);
        }

        return area;
    }

    // Returns the area of the largest region of 1s.
    static int largestRegion(int[, ] grid)
    {
        int rows = grid.GetLength(0);
        int cols = grid.GetLength(1);

        int maxArea = 0;

        // Traverse every cell of the grid.
        for (int r = 0; r < rows; r++) {
            for (int c = 0; c < cols; c++) {
                
                // Start DFS if an unvisited 1 is found.
                if (grid[r, c] == 1) {
                    int area = DFS(grid, r, c, rows, cols);

                    // Update the maximum region area.
                    maxArea = Math.Max(maxArea, area);
                }
            }
        }

        return maxArea;
    }

    public static void Main()
    {
        int[, ] grid = { { 1, 0, 0, 0, 1, 0, 0 },
                         { 0, 1, 0, 0, 1, 1, 1 },
                         { 1, 1, 0, 0, 0, 0, 0 },
                         { 1, 0, 0, 1, 1, 0, 0 },
                         { 1, 0, 0, 1, 0, 1, 1 } };

        Console.WriteLine(largestRegion(grid));
    }
}
JavaScript
// Checks whether the cell is valid and contains 1.
function isSafe(grid, r, c, rows, cols)
{
    return r >= 0 && r < rows && c >= 0 && c < cols
           && grid[r][c] === 1;
}

// DFS to find the area of the current region.
function DFS(grid, r, c, rows, cols)
{
    // Directions for all 8 neighbouring cells.
    const dr = [ -1, -1, -1, 0, 0, 1, 1, 1 ];
    const dc = [ -1, 0, 1, -1, 1, -1, 0, 1 ];

    // Mark the current cell as visited.
    grid[r][c] = 0;

    // Increase the area of the current region.
    let area = 1;

    // Visit all 8 neighbouring cells.
    for (let i = 0; i < 8; i++) {
        const nr = r + dr[i];
        const nc = c + dc[i];

        if (isSafe(grid, nr, nc, rows, cols))
            area += DFS(grid, nr, nc, rows, cols);
    }

    return area;
}

// Returns the area of the largest region of 1s.
function largestRegion(grid)
{
    const rows = grid.length;
    const cols = grid[0].length;

    let maxArea = 0;

    // Traverse every cell of the grid.
    for (let r = 0; r < rows; r++) {
        for (let c = 0; c < cols; c++) {
            
            // Start DFS if an unvisited 1 is found.
            if (grid[r][c] === 1) {
                const area = DFS(grid, r, c, rows, cols);

                // Update the maximum region area.
                maxArea = Math.max(maxArea, area);
            }
        }
    }

    return maxArea;
}

// Driver Code
const grid = [
    [ 1, 0, 0, 0, 1, 0, 0 ], [ 0, 1, 0, 0, 1, 1, 1 ],
    [ 1, 1, 0, 0, 0, 0, 0 ], [ 1, 0, 0, 1, 1, 0, 0 ],
    [ 1, 0, 0, 1, 0, 1, 1 ]
];

console.log(largestRegion(grid));

Output
6

Using BFS - O(n * m) Time and O(n * m) Space

The idea is to use BFS to explore each connected region of 1s in the grid. Since diagonal cells are also connected, we check all 8 neighbouring directions. Whenever an unvisited 1 is found, BFS visits the complete region and counts its cells. Finally, keep track of the maximum area among all regions.

  • Traverse every cell of the grid.
  • If the cell contains an unvisited 1, start BFS from that cell.
  • Mark each visited 1 as 0 and add it to the queue.
  • For every cell, check all 8 neighbouring directions and visit valid 1s.
  • Count the number of cells visited in the current region.
  • Update maxArea and return it after traversing the entire grid.
C++
#include <bits/stdc++.h>
using namespace std;

// Checks whether the cell is valid and contains 1.
bool isSafe(vector<vector<int>> &grid, int r, int c, int rows, int cols)
{
    return r >= 0 && r < rows && c >= 0 && c < cols && grid[r][c] == 1;
}

// Breadth-First Search to find the area of the current region.
int BFS(vector<vector<int>> &grid, int r, int c, int rows, int cols)
{
    // Directions for all 8 neighbouring cells.
    int dr[] = {-1, -1, -1, 0, 0, 1, 1, 1};
    int dc[] = {-1, 0, 1, -1, 1, -1, 0, 1};

    int area = 0;

    // Create a queue for BFS traversal.
    queue<pair<int, int>> q;

    // Push the starting cell and mark it as visited.
    q.push({r, c});
    grid[r][c] = 0;

    while (!q.empty())
    {
        auto curr = q.front();
        q.pop();

        // Increment the area of the region.
        area++;

        // Visit all 8 neighbouring cells.
        for (int i = 0; i < 8; i++)
        {
            int nr = curr.first + dr[i];
            int nc = curr.second + dc[i];

            if (isSafe(grid, nr, nc, rows, cols))
            {
                // Mark the cell as visited.
                grid[nr][nc] = 0;

                // Add the cell to the queue.
                q.push({nr, nc});
            }
        }
    }

    return area;
}

// Returns the area of the largest region of 1s.
int largestRegion(vector<vector<int>> &grid)
{
    int rows = grid.size();
    int cols = grid[0].size();

    int maxArea = 0;

    // Traverse every cell of the grid.
    for (int r = 0; r < rows; r++)
    {
        for (int c = 0; c < cols; c++)
        {
            // Start BFS if an unvisited 1 is found.
            if (grid[r][c] == 1)
            {
                int area = BFS(grid, r, c, rows, cols);

                // Update the maximum region area.
                maxArea = max(maxArea, area);
            }
        }
    }

    return maxArea;
}

int main()
{
    vector<vector<int>> grid = {{1, 0, 0, 0, 1, 0, 0},
                                {0, 1, 0, 0, 1, 1, 1},
                                {1, 1, 0, 0, 0, 0, 0},
                                {1, 0, 0, 1, 1, 0, 0},
                                {1, 0, 0, 1, 0, 1, 1}};

    cout << largestRegion(grid) << endl;

    return 0;
}
C
#include <stdio.h>
#include <stdlib.h>

// Checks whether the cell is valid and contains 1.
int isSafe(int grid[][7], int r, int c, int rows, int cols)
{
    return r >= 0 && r < rows && c >= 0 && c < cols && grid[r][c] == 1;
}

// Breadth-First Search to find the area of the current region.
int BFS(int grid[][7], int r, int c, int rows, int cols)
{
    // Directions for all 8 neighbouring cells.
    int dr[] = {-1, -1, -1, 0, 0, 1, 1, 1};
    int dc[] = {-1, 0, 1, -1, 1, -1, 0, 1};

    int area = 0;

    // Create arrays for the BFS queue.
    int queue[100][2];
    int front = 0, rear = 0;

    // Push the starting cell and mark it as visited.
    queue[rear][0] = r;
    queue[rear][1] = c;
    rear++;

    grid[r][c] = 0;

    while (front < rear)
    {
        int currR = queue[front][0];
        int currC = queue[front][1];
        front++;

        // Increment the area of the region.
        area++;

        // Visit all 8 neighbouring cells.
        for (int i = 0; i < 8; i++)
        {
            int nr = currR + dr[i];
            int nc = currC + dc[i];

            if (isSafe(grid, nr, nc, rows, cols))
            {
                // Mark the cell as visited.
                grid[nr][nc] = 0;

                // Add the cell to the queue.
                queue[rear][0] = nr;
                queue[rear][1] = nc;
                rear++;
            }
        }
    }

    return area;
}

// Returns the area of the largest region of 1s.
int largestRegion(int grid[][7], int rows, int cols)
{
    int maxArea = 0;

    // Traverse every cell of the grid.
    for (int r = 0; r < rows; r++)
    {
        for (int c = 0; c < cols; c++)
        {
            // Start BFS if an unvisited 1 is found.
            if (grid[r][c] == 1)
            {
                int area = BFS(grid, r, c, rows, cols);

                // Update the maximum region area.
                if (area > maxArea)
                    maxArea = area;
            }
        }
    }

    return maxArea;
}

int main()
{
    int grid[5][7] = {{1, 0, 0, 0, 1, 0, 0},
                      {0, 1, 0, 0, 1, 1, 1},
                      {1, 1, 0, 0, 0, 0, 0},
                      {1, 0, 0, 1, 1, 0, 0},
                      {1, 0, 0, 1, 0, 1, 1}};

    printf("%d\n", largestRegion(grid, 5, 7));

    return 0;
}
Java
import java.util.*;

class GFG {

    // Checks whether the cell is valid and contains 1.
    static boolean isSafe(int[][] grid, int r, int c,
                          int rows, int cols)
    {
        return r >= 0 && r < rows && c >= 0 && c < cols
            && grid[r][c] == 1;
    }

    // Breadth-First Search to find the area of the current
    // region.
    static int BFS(int[][] grid, int r, int c, int rows,
                   int cols)
    {
        // Directions for all 8 neighbouring cells.
        int[] dr = { -1, -1, -1, 0, 0, 1, 1, 1 };
        int[] dc = { -1, 0, 1, -1, 1, -1, 0, 1 };

        int area = 0;

        // Create a queue for BFS traversal.
        Queue<int[]> q = new LinkedList<>();

        // Push the starting cell and mark it as visited.
        q.offer(new int[] { r, c });
        grid[r][c] = 0;

        while (!q.isEmpty()) {
            int[] curr = q.poll();

            // Increment the area of the region.
            area++;

            // Visit all 8 neighbouring cells.
            for (int i = 0; i < 8; i++) {
                int nr = curr[0] + dr[i];
                int nc = curr[1] + dc[i];

                if (isSafe(grid, nr, nc, rows, cols)) {

                    // Mark the cell as visited.
                    grid[nr][nc] = 0;

                    // Add the cell to the queue.
                    q.offer(new int[] { nr, nc });
                }
            }
        }

        return area;
    }

    // Returns the area of the largest region of 1s.
    static int largestRegion(int[][] grid)
    {
        int rows = grid.length;
        int cols = grid[0].length;

        int maxArea = 0;

        // Traverse every cell of the grid.
        for (int r = 0; r < rows; r++) {
            for (int c = 0; c < cols; c++) {

                // Start BFS if an unvisited 1 is found.
                if (grid[r][c] == 1) {
                    int area = BFS(grid, r, c, rows, cols);

                    // Update the maximum region area.
                    maxArea = Math.max(maxArea, area);
                }
            }
        }

        return maxArea;
    }

    public static void main(String[] args)
    {
        int[][] grid = { { 1, 0, 0, 0, 1, 0, 0 },
                         { 0, 1, 0, 0, 1, 1, 1 },
                         { 1, 1, 0, 0, 0, 0, 0 },
                         { 1, 0, 0, 1, 1, 0, 0 },
                         { 1, 0, 0, 1, 0, 1, 1 } };

        System.out.println(largestRegion(grid));
    }
}
Python
from collections import deque

# Checks whether the cell is valid and contains 1.
def isSafe(grid, r, c, rows, cols):
    return r >= 0 and r < rows and c >= 0 and c < cols and grid[r][c] == 1


# Breadth-First Search to find the area of the current region.
def BFS(grid, r, c, rows, cols):

    # Directions for all 8 neighbouring cells.
    dr = [-1, -1, -1, 0, 0, 1, 1, 1]
    dc = [-1, 0, 1, -1, 1, -1, 0, 1]

    area = 0

    # Create a queue for BFS traversal.
    q = deque()

    # Push the starting cell and mark it as visited.
    q.append((r, c))
    grid[r][c] = 0

    while q:

        curr = q.popleft()

        # Increment the area of the region.
        area += 1

        # Visit all 8 neighbouring cells.
        for i in range(8):
            nr = curr[0] + dr[i]
            nc = curr[1] + dc[i]

            if isSafe(grid, nr, nc, rows, cols):

                # Mark the cell as visited.
                grid[nr][nc] = 0

                # Add the cell to the queue.
                q.append((nr, nc))

    return area


# Returns the area of the largest region of 1s.
def largestRegion(grid):

    rows = len(grid)
    cols = len(grid[0])

    maxArea = 0

    # Traverse every cell of the grid.
    for r in range(rows):
        for c in range(cols):

            # Start BFS if an unvisited 1 is found.
            if grid[r][c] == 1:
                area = BFS(grid, r, c, rows, cols)

                # Update the maximum region area.
                maxArea = max(maxArea, area)

    return maxArea


# Driver Code
if __name__ == "__main__":
    grid = [
        [1, 0, 0, 0, 1, 0, 0],
        [0, 1, 0, 0, 1, 1, 1],
        [1, 1, 0, 0, 0, 0, 0],
        [1, 0, 0, 1, 1, 0, 0],
        [1, 0, 0, 1, 0, 1, 1]
    ]

    print(largestRegion(grid))
C#
using System;
using System.Collections.Generic;

class GFG {

    // Checks whether the cell is valid and contains 1.
    static bool isSafe(int[, ] grid, int r, int c, int rows,
                       int cols)
    {
        return r >= 0 && r < rows && c >= 0 && c < cols
            && grid[r, c] == 1;
    }

    // Breadth-First Search to find the area of the current
    // region.
    static int BFS(int[, ] grid, int r, int c, int rows,
                   int cols)
    {
        // Directions for all 8 neighbouring cells.
        int[] dr = { -1, -1, -1, 0, 0, 1, 1, 1 };
        int[] dc = { -1, 0, 1, -1, 1, -1, 0, 1 };

        int area = 0;

        // Create a queue for BFS traversal.
        Queue<(int, int)> q = new Queue<(int, int)>();

        // Push the starting cell and mark it as visited.
        q.Enqueue((r, c));
        grid[r, c] = 0;

        while (q.Count > 0) {
            var curr = q.Dequeue();

            // Increment the area of the region.
            area++;

            // Visit all 8 neighbouring cells.
            for (int i = 0; i < 8; i++) {
                int nr = curr.Item1 + dr[i];
                int nc = curr.Item2 + dc[i];

                if (isSafe(grid, nr, nc, rows, cols)) {
                    // Mark the cell as visited.
                    grid[nr, nc] = 0;

                    // Add the cell to the queue.
                    q.Enqueue((nr, nc));
                }
            }
        }

        return area;
    }

    // Returns the area of the largest region of 1s.
    static int largestRegion(int[, ] grid)
    {
        int rows = grid.GetLength(0);
        int cols = grid.GetLength(1);

        int maxArea = 0;

        // Traverse every cell of the grid.
        for (int r = 0; r < rows; r++) {
            for (int c = 0; c < cols; c++) {
                // Start BFS if an unvisited 1 is found.
                if (grid[r, c] == 1) {
                    int area = BFS(grid, r, c, rows, cols);

                    // Update the maximum region area.
                    maxArea = Math.Max(maxArea, area);
                }
            }
        }

        return maxArea;
    }

    static void Main()
    {
        int[, ] grid = { { 1, 0, 0, 0, 1, 0, 0 },
                         { 0, 1, 0, 0, 1, 1, 1 },
                         { 1, 1, 0, 0, 0, 0, 0 },
                         { 1, 0, 0, 1, 1, 0, 0 },
                         { 1, 0, 0, 1, 0, 1, 1 } };

        Console.WriteLine(largestRegion(grid));
    }
}
JavaScript
// Checks whether the cell is valid and contains 1.
function isSafe(grid, r, c, rows, cols)
{
    return r >= 0 && r < rows && c >= 0 && c < cols
           && grid[r][c] === 1;
}

// Breadth-First Search to find the area of the current
// region.
function BFS(grid, r, c, rows, cols)
{
    // Directions for all 8 neighbouring cells.
    const dr = [ -1, -1, -1, 0, 0, 1, 1, 1 ];
    const dc = [ -1, 0, 1, -1, 1, -1, 0, 1 ];

    let area = 0;

    // Create a queue for BFS traversal.
    const q = [];

    // Push the starting cell and mark it as visited.
    q.push([ r, c ]);
    grid[r][c] = 0;

    let front = 0;

    while (front < q.length) {
        const curr = q[front++];

        // Increment the area of the region.
        area++;

        // Visit all 8 neighbouring cells.
        for (let i = 0; i < 8; i++) {
            const nr = curr[0] + dr[i];
            const nc = curr[1] + dc[i];

            if (isSafe(grid, nr, nc, rows, cols)) {
                // Mark the cell as visited.
                grid[nr][nc] = 0;

                // Add the cell to the queue.
                q.push([ nr, nc ]);
            }
        }
    }

    return area;
}

// Returns the area of the largest region of 1s.
function largestRegion(grid)
{
    const rows = grid.length;
    const cols = grid[0].length;

    let maxArea = 0;

    // Traverse every cell of the grid.
    for (let r = 0; r < rows; r++) {
        for (let c = 0; c < cols; c++) {
            
            // Start BFS if an unvisited 1 is found.
            if (grid[r][c] === 1) {
                const area = BFS(grid, r, c, rows, cols);

                // Update the maximum region area.
                maxArea = Math.max(maxArea, area);
            }
        }
    }

    return maxArea;
}


// Driver Code
const grid = [
    [ 1, 0, 0, 0, 1, 0, 0 ], [ 0, 1, 0, 0, 1, 1, 1 ],
    [ 1, 1, 0, 0, 0, 0, 0 ], [ 1, 0, 0, 1, 1, 0, 0 ],
    [ 1, 0, 0, 1, 0, 1, 1 ]
];

console.log(largestRegion(grid));

Output
6
Comment