Largest Plus Formed by All Ones in a Binary Square Matrix

Last Updated : 16 Jun, 2026

Given an n × n binary matrix mat[][] consisting of 0s and 1s, find the size of the largest ‘+’ shape that can be formed using only 1s. If no ‘+’ can be formed, return 0.

Note: A ‘+’ shape is formed by a central cell from which four arms extend in the up, down, left, and right directions, while staying within the matrix boundaries. The size of the ‘+’ shape is defined as the total number of cells that make up the structure, including the center cell and all the cells in its four arms.

Examples:

Input: mat[][] = [[0, 1, 1, 1], [0, 1, 1, 1], [0, 0, 1, 1], [0, 0, 1, 0]]
Output: 5
Explanation: Largest ‘+’ would be formed by highlighted part of size 5.

2056958347

Input: mat[][] = [[0,1], [1,0]]
Output: 1
Explanation: Largest ‘+’ would be formed by highlighted part of size 5.

Input: mat = [[0]]
Output: 0
Explanation: No ‘+’ sign can be formed.

[Naive Approach] Check Every Cell as Center - O(n ^ 3) Time and O(1) Space

Treat every cell containing 1 as the center of a '+' and expand simultaneously in all four directions while the cells contain 1s. The largest valid expansion determines the size of the '+' centered at that cell. Repeat this for all cells and keep track of the maximum size found.

C++
#include <iostream>
#include <vector>
using namespace std;

int findLargestPlus(vector<vector<int>>& mat) {
    int n = mat.size();
    int ans = 0;

    // Try every cell as the center of '+'.
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            if (mat[i][j] == 0) continue;

            int arm = 1;

            // Expand in all four directions.
            while (i - arm >= 0 && i + arm < n &&
                   j - arm >= 0 && j + arm < n &&
                   mat[i - arm][j] == 1 &&
                   mat[i + arm][j] == 1 &&
                   mat[i][j - arm] == 1 &&
                   mat[i][j + arm] == 1) {
                arm++;
            }

            ans = max(ans, 4 * arm - 3);
        }
    }

    return ans;
}

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

    cout << findLargestPlus(mat);

    return 0;
}
Java
class GFG {

    static int findLargestPlus(int[][] mat) {
        int n = mat.length;
        int ans = 0;

        // Try every cell as the center of '+'.
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                if (mat[i][j] == 0) continue;

                int arm = 1;

                // Expand in all four directions.
                while (i - arm >= 0 && i + arm < n &&
                       j - arm >= 0 && j + arm < n &&
                       mat[i - arm][j] == 1 &&
                       mat[i + arm][j] == 1 &&
                       mat[i][j - arm] == 1 &&
                       mat[i][j + arm] == 1) {
                    arm++;
                }

                ans = Math.max(ans, 4 * arm - 3);
            }
        }

        return ans;
    }

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

        System.out.println(findLargestPlus(mat));
    }
}
Python
def findLargestPlus(mat):
    n = len(mat)
    ans = 0

    # Try every cell as the center of '+'.
    for i in range(n):
        for j in range(n):
            if mat[i][j] == 0:
                continue

            arm = 1

            # Expand in all four directions.
            while (i - arm >= 0 and i + arm < n and
                   j - arm >= 0 and j + arm < n and
                   mat[i - arm][j] == 1 and
                   mat[i + arm][j] == 1 and
                   mat[i][j - arm] == 1 and
                   mat[i][j + arm] == 1):
                arm += 1

            ans = max(ans, 4 * arm - 3)

    return ans


mat = [
    [0, 1, 1, 1],
    [0, 1, 1, 1],
    [0, 0, 1, 1],
    [0, 0, 1, 0]
]

print(findLargestPlus(mat))
C#
using System;

class GFG
{
    static int FindLargestPlus(int[][] mat)
    {
        int n = mat.Length;
        int ans = 0;

        // Try every cell as the center of '+'.
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < n; j++)
            {
                if (mat[i][j] == 0) continue;

                int arm = 1;

                // Expand in all four directions.
                while (i - arm >= 0 && i + arm < n &&
                       j - arm >= 0 && j + arm < n &&
                       mat[i - arm][j] == 1 &&
                       mat[i + arm][j] == 1 &&
                       mat[i][j - arm] == 1 &&
                       mat[i][j + arm] == 1)
                {
                    arm++;
                }

                ans = Math.Max(ans, 4 * arm - 3);
            }
        }

        return ans;
    }

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

        Console.WriteLine(FindLargestPlus(mat));
    }
}
JavaScript
function findLargestPlus(mat) {
    const n = mat.length;
    let ans = 0;

    // Try every cell as the center of '+'.
    for (let i = 0; i < n; i++) {
        for (let j = 0; j < n; j++) {
            if (mat[i][j] === 0) continue;

            let arm = 1;

            // Expand in all four directions.
            while (i - arm >= 0 && i + arm < n &&
                   j - arm >= 0 && j + arm < n &&
                   mat[i - arm][j] === 1 &&
                   mat[i + arm][j] === 1 &&
                   mat[i][j - arm] === 1 &&
                   mat[i][j + arm] === 1) {
                arm++;
            }

            ans = Math.max(ans, 4 * arm - 3);
        }
    }

    return ans;
}

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

console.log(findLargestPlus(mat));

Output
5

[Expected Approach] Using Dynamic Programming - O(n ^ 2) Time and O(n ^ 2) Space

For each cell, precompute the number of consecutive 1s extending left, right, up, and down (including the cell itself). The maximum possible arm length of a '+' centered at a cell is the minimum of these four values. Compute this for every cell and return the largest '+' size found.

Step By Step Implementation:

  • We use four auxiliary matrices: left[][], right[][], top[][] and bottom[][].
  • left[i][j] stores the number of consecutive 1s to the left of cell (i, j), including the cell itself.
  • right[i][j] stores the number of consecutive 1s to the right of cell (i, j), including the cell itself.
  • top[i][j] stores the number of consecutive 1s above cell (i, j), including the cell itself.
  • bottom[i][j] stores the number of consecutive 1s below cell (i, j), including the cell itself.
  • For every cell, the maximum possible arm length of a '+' centered at that cell is the minimum of left[i][j], right[i][j], top[i][j], and bottom[i][j]. We compute this value for all cells and use the largest one to determine the size of the largest '+'.
C++
#include <iostream>
#include <vector>
using namespace std;

int findLargestPlus(vector<vector<int>>& mat) {
    int n = mat.size();

    vector<vector<int>> left(n, vector<int>(n, 0));
    vector<vector<int>> right(n, vector<int>(n, 0));
    vector<vector<int>> top(n, vector<int>(n, 0));
    vector<vector<int>> bottom(n, vector<int>(n, 0));

    // Compute consecutive 1s towards left and top.
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            if (mat[i][j] == 1) {
                left[i][j] = 1 + (j > 0 ? left[i][j - 1] : 0);
                top[i][j] = 1 + (i > 0 ? top[i - 1][j] : 0);
            }
        }
    }

    // Compute consecutive 1s towards right and bottom.
    for (int i = n - 1; i >= 0; i--) {
        for (int j = n - 1; j >= 0; j--) {
            if (mat[i][j] == 1) {
                right[i][j] = 1 + (j + 1 < n ? right[i][j + 1] : 0);
                bottom[i][j] = 1 + (i + 1 < n ? bottom[i + 1][j] : 0);
            }
        }
    }

    int ans = 0;

    // Find the largest '+' centered at each cell.
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            int arm = min({left[i][j], right[i][j], top[i][j], bottom[i][j]});
            ans = max(ans, 4 * arm - 3);
        }
    }

    return ans;
}

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

    cout << findLargestPlus(mat);

    return 0;
}
Java
class GFG {

    static int findLargestPlus(int[][] mat) {
        int n = mat.length;

        int[][] left = new int[n][n];
        int[][] right = new int[n][n];
        int[][] top = new int[n][n];
        int[][] bottom = new int[n][n];

        // Compute consecutive 1s towards left and top.
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                if (mat[i][j] == 1) {
                    left[i][j] = 1 + (j > 0 ? left[i][j - 1] : 0);
                    top[i][j] = 1 + (i > 0 ? top[i - 1][j] : 0);
                }
            }
        }

        // Compute consecutive 1s towards right and bottom.
        for (int i = n - 1; i >= 0; i--) {
            for (int j = n - 1; j >= 0; j--) {
                if (mat[i][j] == 1) {
                    right[i][j] = 1 + (j + 1 < n ? right[i][j + 1] : 0);
                    bottom[i][j] = 1 + (i + 1 < n ? bottom[i + 1][j] : 0);
                }
            }
        }

        int ans = 0;

        // Find the largest '+' centered at each cell.
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                int arm = Math.min(Math.min(left[i][j], right[i][j]),
                                   Math.min(top[i][j], bottom[i][j]));

                ans = Math.max(ans, 4 * arm - 3);
            }
        }

        return ans;
    }

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

        System.out.println(findLargestPlus(mat));
    }
}
Python
def findLargestPlus(mat):
    n = len(mat)

    left = [[0] * n for _ in range(n)]
    right = [[0] * n for _ in range(n)]
    top = [[0] * n for _ in range(n)]
    bottom = [[0] * n for _ in range(n)]

    # Compute consecutive 1s towards left and top.
    for i in range(n):
        for j in range(n):
            if mat[i][j] == 1:
                left[i][j] = 1 + (left[i][j - 1] if j > 0 else 0)
                top[i][j] = 1 + (top[i - 1][j] if i > 0 else 0)

    # Compute consecutive 1s towards right and bottom.
    for i in range(n - 1, -1, -1):
        for j in range(n - 1, -1, -1):
            if mat[i][j] == 1:
                right[i][j] = 1 + (right[i][j + 1] if j + 1 < n else 0)
                bottom[i][j] = 1 + (bottom[i + 1][j] if i + 1 < n else 0)

    ans = 0

    # Find the largest '+' centered at each cell.
    for i in range(n):
        for j in range(n):
            arm = min(left[i][j], right[i][j], top[i][j], bottom[i][j])
            ans = max(ans, 4 * arm - 3)

    return ans


mat = [
    [0, 1, 1, 1],
    [0, 1, 1, 1],
    [0, 0, 1, 1],
    [0, 0, 1, 0]
]

print(findLargestPlus(mat))
C#
using System;

class GFG
{
    static int FindLargestPlus(int[][] mat)
    {
        int n = mat.Length;

        int[][] left = new int[n][];
        int[][] right = new int[n][];
        int[][] top = new int[n][];
        int[][] bottom = new int[n][];

        for (int i = 0; i < n; i++)
        {
            left[i] = new int[n];
            right[i] = new int[n];
            top[i] = new int[n];
            bottom[i] = new int[n];
        }

        // Compute consecutive 1s towards left and top.
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < n; j++)
            {
                if (mat[i][j] == 1)
                {
                    left[i][j] = 1 + (j > 0 ? left[i][j - 1] : 0);
                    top[i][j] = 1 + (i > 0 ? top[i - 1][j] : 0);
                }
            }
        }

        // Compute consecutive 1s towards right and bottom.
        for (int i = n - 1; i >= 0; i--)
        {
            for (int j = n - 1; j >= 0; j--)
            {
                if (mat[i][j] == 1)
                {
                    right[i][j] = 1 + (j + 1 < n ? right[i][j + 1] : 0);
                    bottom[i][j] = 1 + (i + 1 < n ? bottom[i + 1][j] : 0);
                }
            }
        }

        int ans = 0;

        // Find the largest '+' centered at each cell.
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < n; j++)
            {
                int arm = Math.Min(Math.Min(left[i][j], right[i][j]),
                                   Math.Min(top[i][j], bottom[i][j]));

                ans = Math.Max(ans, 4 * arm - 3);
            }
        }

        return ans;
    }

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

        Console.WriteLine(FindLargestPlus(mat));
    }
}
JavaScript
function findLargestPlus(mat) {
    const n = mat.length;

    const left = Array.from({ length: n }, () => Array(n).fill(0));
    const right = Array.from({ length: n }, () => Array(n).fill(0));
    const top = Array.from({ length: n }, () => Array(n).fill(0));
    const bottom = Array.from({ length: n }, () => Array(n).fill(0));

    // Compute consecutive 1s towards left and top.
    for (let i = 0; i < n; i++) {
        for (let j = 0; j < n; j++) {
            if (mat[i][j] === 1) {
                left[i][j] = 1 + (j > 0 ? left[i][j - 1] : 0);
                top[i][j] = 1 + (i > 0 ? top[i - 1][j] : 0);
            }
        }
    }

    // Compute consecutive 1s towards right and bottom.
    for (let i = n - 1; i >= 0; i--) {
        for (let j = n - 1; j >= 0; j--) {
            if (mat[i][j] === 1) {
                right[i][j] = 1 + (j + 1 < n ? right[i][j + 1] : 0);
                bottom[i][j] = 1 + (i + 1 < n ? bottom[i + 1][j] : 0);
            }
        }
    }

    let ans = 0;

    // Find the largest '+' centered at each cell.
    for (let i = 0; i < n; i++) {
        for (let j = 0; j < n; j++) {
            const arm = Math.min(left[i][j], right[i][j], top[i][j], bottom[i][j]);
            ans = Math.max(ans, 4 * arm - 3);
        }
    }

    return ans;
}

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

console.log(findLargestPlus(mat));

Output
5
Comment