Python代码
n=int(input())
m=[]
for i in range(n):
m.append(list(input()))
dx=[-1,0,1,0]
dy=[0,1,0,-1]
st=[[True for _ in range(n)]for _ in range(n)]
arr=[]
res=0
def dfs(x,y):
global res
if len(arr)%2==0:
flag=1
t=len(arr)//2
for i in range(len(arr)):
if i<t:
if arr[i]==")":
flag=0
if i>=t:
if arr[i]=="(":
flag=0
if flag==1:
res=max(res,len(arr))
for i in range(4):
a=x+dx[i]
b=y+dy[i]
if a>=0 and a<n and b>=0 and b<n and st[a][b]:
st[a][b]=False
arr.append(m[a][b])
dfs(a,b)
st[a][b]=True
arr.pop()
st[0][0]=False
arr.append(m[0][0])
dfs(0,0)
print(res)