|
| 1 | +class Grid(object): |
| 2 | + def __init__(self, rows, m, n): |
| 3 | + self.rows = rows |
| 4 | + self.m = m |
| 5 | + self.n = n |
| 6 | + def largestConnectedComponent(self): |
| 7 | + self.unexplored = [] |
| 8 | + for i in xrange(self.m): |
| 9 | + self.unexplored.append([]) |
| 10 | + for j in xrange(self.n): |
| 11 | + self.unexplored[i].append(bool(self.rows[i][j])) |
| 12 | + maxConn = 0 |
| 13 | + for i in xrange(self.m): |
| 14 | + for j in xrange(self.n): |
| 15 | + if self.unexplored[i][j]: |
| 16 | + maxConn = max(maxConn, self.lccHelper(i, j)) |
| 17 | + return maxConn |
| 18 | + def lccHelper(self, i, j): |
| 19 | + if i < 0 or j < 0 or i >= self.m or j >= self.n: |
| 20 | + return 0 |
| 21 | + if not self.unexplored[i][j]: |
| 22 | + return 0 |
| 23 | + self.unexplored[i][j] = 0 |
| 24 | + return 1 + self.lccHelper(i-1, j-1) + self.lccHelper(i-1, j) + self.lccHelper(i-1, j+1) + \ |
| 25 | + self.lccHelper(i, j-1) + self.lccHelper(i, j+1) + \ |
| 26 | + self.lccHelper(i+1, j-1) + self.lccHelper(i+1, j) + self.lccHelper(i+1, j+1) |
| 27 | + |
| 28 | +def getInput(): |
| 29 | + numRows = input() |
| 30 | + numCols = input() |
| 31 | + rows = [] |
| 32 | + for m in xrange(numRows): |
| 33 | + row = [bool(int(num)) for num in raw_input().split(" ")] |
| 34 | + rows.append(row) |
| 35 | + return Grid(rows, numRows, numCols) |
| 36 | + |
| 37 | +if __name__ == "__main__": |
| 38 | + grid = getInput() |
| 39 | + print grid.largestConnectedComponent() |
0 commit comments