You are given an N x M matrix of ones and zeros, which represents an image of a rectangular metal plate taken from the hull of a ship. Each element of the matrix denotes an area of the plate: The ones denote areas where the plate has corroded visibly, whereas zeros are areas where the metal is still in good condition. The corroded areas (the matrix elements with ones) can be found at multiple points on the metal plate.
A collection of corroded areas that are next to each other is called a pit. Pits can come at different sizes, depending on how much they grew over the years. Pitting corrosion is important to quantify because it gives an idea of the reliability of the ship structure.
For this problem, a pit is defined more clearly as follows: Two corroded areas at [row R1, column C1] and [row R2, column C2] belong to the same pit if and only if max(abs(R1-R2),abs(C1-C2)) <= 1. In other words, if two corroded areas are adjacent horizontally, vertically, or diagonally, then both are considered to belong to the same pit.
The size of a pit is equal to the number of corroded areas (or ones) that belong to it.
Write a function that accepts a matrix X denoting the image in question. Output the size of the largest pit in this image. You are ensured that the size of the matrix is constrained at 1 <= N <= 20 and 1 <= M <= 20.
As an example, consider the following 10 x 10 image (left). The corroded areas that belong to the same pit are then labeled, showing that the largest pit is Pit 1, with a size of 6 (right). In contrast, Pits 2, 3, 4 and 5 have size 2, 2, 4, and 4, respectively.
Sample test case: X = [0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 1 1 0 0 0 1 1 0 0 0 1 1 0 0 0 4 4 0 0 0 0 1 0 0 0 1 1 0 0 0 0 1 0 0 0 4 4 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 2 0 0 0 0 5 0 0 0 0 0 1 0 0 0 1 1 0 0 0 0 2 0 0 0 5 5 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 5 0 0 0 0 0 1 1 0 0 0 0]; 0 0 0 0 3 3 0 0 0 0 The largest pit in this image is Pit 1, with size 6.
Back to basics 20 - singleton dimensions
245 Solvers
69 Solvers
Flag largest magnitude swings as they occur
697 Solvers
Generate N equally spaced intervals between -L and L
546 Solvers
Gauss Eliminate 2-by-2 example
142 Solvers