• + 0 comments
    #!/bin/python3
    
    import math
    import os
    import random
    import re
    import sys
    from itertools import combinations
    
    # Complete the 'twoPluses' function below.
    # The function is expected to return an INTEGER.
    # The function accepts STRING_ARRAY grid as parameter.
    
    def twoPluses(grid):
        h, w = len(grid), len(grid[0])
        pluses = []
    
        def is_good(r, c):
            return 0 <= r < h and 0 <= c < w and grid[r][c] == 'G'
    
        # Generate all valid pluses
        for r in range(h):
            for c in range(w):
                if not is_good(r, c):
                    continue
                size = 0
                occupied = {(r, c)}
                pluses.append((1, occupied.copy()))  # Add single cell plus
                while True:
                    size += 1
                    cells = {(r+i, c) for i in range(-size, size+1)} | {(r, c+i) for i in range(-size, size+1)}
                    if all(is_good(x, y) for x, y in cells):
                        pluses.append((len(cells), cells.copy()))
                    else:
                        break
    
        # Try all combinations of 2 pluses and find max area product of non-overlapping ones
        max_product = 0
        for (area1, cells1), (area2, cells2) in combinations(pluses, 2):
            if cells1.isdisjoint(cells2):
                max_product = max(max_product, area1 * area2)
    
        return max_product if max_product else 1  # At least one plus is guaranteed
    
    if __name__ == '__main__':
        fptr = open(os.environ['OUTPUT_PATH'], 'w')
    
        nm = input().split()
        n = int(nm[0])
        m = int(nm[1])
    
        grid = []
        for _ in range(n):
            grid_item = input()
            grid.append(grid_item)
    
        result = twoPluses(grid)
    
        fptr.write(str(result) + '\n')
        fptr.close()