## C# || Max Area of Island – How To Find The Maximum Area Of An Island In A Grid Using C#

The following is a module with functions which demonstrates how to find the maximum area of an island in a grid using C#.

1. Max Area Of Island – Problem Statement

You are given an m x n binary matrix grid. An island is a group of 1‘s (representing land) connected 4-directionally (horizontal or vertical.) You may assume all four edges of the grid are surrounded by water.

The area of an island is the number of cells with a value 1 in the island.

Return the maximum area of an island in grid. If there is no island, return 0.

Example 1:

``` Input: grid = [[0,0,1,0,0,0,0,1,0,0,0,0,0],[0,0,0,0,0,0,0,1,1,1,0,0,0],[0,1,1,0,1,0,0,0,0,0,0,0,0],[0,1,0,0,1,1,0,0,1,0,1,0,0],[0,1,0,0,1,1,0,0,1,1,1,0,0],[0,0,0,0,0,0,0,0,0,0,1,0,0],[0,0,0,0,0,0,0,1,1,1,0,0,0],[0,0,0,0,0,0,0,1,1,0,0,0,0]] Output: 6 Explanation: The answer is not 11, because the island must be connected 4-directionally. ```

Example 2:

``` Input: grid = [[0,0,0,0,0,0,0,0]] Output: 0 ```

2. Max Area Of Island – Solution

The following is a solution which demonstrates how to find the maximum area of an island in a grid.

We want to know the area of each connected shape in the grid, then take the maximum of these.

If we are on a land square and explore every square connected to it 4-directionally (and recursively squares connected to those squares, and so on), then the total number of squares explored will be the area of that connected shape.

To ensure we don’t count squares in a shape more than once, we use ‘seen’ to keep track of squares we haven’t visited before. It will also prevent us from counting the same shape more than once.

``` 2. Max Area Of Island - Solution C# // ============================================================================ // Author: Kenneth Perkins // Date: Jul 28, 2022 // Taken From: http://programmingnotes.org/ // File: Solution.cs // Description: Demonstrates how to find the maximum area in a grid // ============================================================================ public class Solution { public int MaxAreaOfIsland(int[][] grid) { var seen = new bool[grid.Length][]; for (int row = 0; row < seen.Length; ++row) { seen[row] = new bool[grid[row].Length]; } var result = 0; for (int row = 0; row < grid.Length; ++row) { for (int col = 0; col < grid[row].Length; ++col) { result = Math.Max(result, Area(grid, row, col, seen)); } } return result; } private int Area(int[][] grid, int row, int col, bool[][] seen) { if (!IsValidCell(grid, row, col)) { return 0; } if (seen[row][col] || grid[row][col] == 0) { return 0; } seen[row][col] = true; return 1 + // Search Bottom Area(grid, row + 1, col, seen) + // Search Top Area(grid, row - 1, col, seen) + // Search Left Area(grid, row, col - 1, seen) + // Search Right Area(grid, row, col + 1, seen); } private bool IsValidCell(int[][] grid, int row, int col) { return row >= 0 && row < grid.Length && col >= 0 && col < grid[row].Length; } }// http://programmingnotes.org/ 123456789101112131415161718192021222324252627282930313233343536373839404142434445 // ============================================================================//    Author: Kenneth Perkins//    Date:   Jul 28, 2022//    Taken From: http://programmingnotes.org///    File:  Solution.cs//    Description: Demonstrates how to find the maximum area in a grid// ============================================================================public class Solution {    public int MaxAreaOfIsland(int[][] grid) {        var seen = new bool[grid.Length][];        for (int row = 0; row < seen.Length; ++row) {            seen[row] = new bool[grid[row].Length];        }        var result = 0;        for (int row = 0; row < grid.Length; ++row) {            for (int col = 0; col < grid[row].Length; ++col) {                result = Math.Max(result, Area(grid, row, col, seen));            }        }        return result;    }     private int Area(int[][] grid, int row, int col, bool[][] seen) {        if (!IsValidCell(grid, row, col)) {            return 0;        }        if (seen[row][col] || grid[row][col] == 0) {            return 0;        }        seen[row][col] = true;        return 1 +            // Search Bottom            Area(grid, row + 1, col, seen) +            // Search Top            Area(grid, row - 1, col, seen) +            // Search Left            Area(grid, row, col - 1, seen) +            // Search Right            Area(grid, row, col + 1, seen);    }     private bool IsValidCell(int[][] grid, int row, int col) {        return row >= 0 && row < grid.Length && col >= 0 && col < grid[row].Length;    }}// http://programmingnotes.org/ ```

QUICK NOTES:
The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

Once compiled, you should get this as your output for the example cases:

``` 6 0 ```