#!/usr/bin/env python
# -*- coding: utf-8 -*-

def f(grid):

    # 1 标定每个区域面程并标记
    # 定义与(i,j) 相邻的点
    def neighbor(grid, i, j):
        for x,y in [(i-1,j), (i+1, j), (i, j-1), (i,j+1)]:
            if 0<= x < len(grid) and 0<= y < len(grid[0]):
                yield (x,y)

    def mark(grid, i, j, num):
        """
        num -> 当前的标记
        """
        total = 1
        grid[i][j] = num
        queue = [(i,j)]
        while queue:
            a = queue.pop(0)
            for x,y in neighbor(grid, a[0], a[1]):
                if grid[x][y] == 1:
                    queue.append((x,y))
                    grid[x][y] = num
                    total += 1
        return total

    num = 2
    record = {}
    for i in range(len(grid)):
        for j in range(len(grid[0])):
            if grid[i][j] == 1:
                record[num] = mark(grid, i, j, num)
                num += 1

    # 2 计算最大面积
    print(grid)
    ans = max(record.values() or [0])
    for r in xrange(len(grid)):
        for c in xrange(len(grid[1])):
            if grid[r][c] == 0:
                seen = {grid[nr][nc] for nr, nc in neighbor(grid, r, c) if grid[nr][nc] > 1}
                ans = max(ans, 1 + sum(record[i] for i in seen))
    return ans



grid = [[1, 1], [1, 1]]
print(f(grid))
