---
# System prepended metadata

title: Problem 4-2
tags: [week7, answer]

---

# Problem 4-2
###### tags: `week7` `answer`
1. 答案版
```python=1
def work(i,j,lol,alist):
    global m
    clol =[]
    for m1 in range(m):
        ac = lol[m1][:]
        clol.append(ac)
    stock = clol[i][j]
    clist = alist[:]
    clist.append(stock)
    clol[i][j]= '#'
    count = 0
    maxcompare = []
    if check(i-1,j):
        if lol[i-1][j] != '#':
            count+=1
            maxcompare.append(work(i-1,j,clol,clist))
    if check(i+1,j):
        if lol[i+1][j] != '#':
            count+=1
            maxcompare.append(work(i+1,j,clol,clist))
    if check(i,j-1):
        if lol[i][j-1] != '#':
            count+=1
            maxcompare.append(work(i,j-1,clol,clist))
    if check(i,j+1):
        if lol[i][j+1] != '#':
            count+=1
            maxcompare.append(work(i,j+1,clol,clist))
    if count ==0:
        num = int(''.join(clist))
        return num
    else:
        return max(maxcompare)
def check(i,j):
    global m,n
    if 0<=i and i<m:
        if 0<=j and j<n:
            return True
    return False
    
mlist =[]
[m,n] = list(map(int,input().split(' ')))
slol = []
for i in range(m):
    slol.append(list(input()))
O_O = input()
for i in range(m):
    for j in range(n):
        if slol[i][j] != '#':
            alist = []
            mlist.append(work(i,j,slol,alist))
print(max(mlist))
```
2.print版
```python=1
def work(i,j,lol,alist):
    global m,n
    clol =[]
    for m1 in range(m):
        ac = lol[m1][:]
        clol.append(ac)
    for g in clol:
        print(g)
    stock = clol[i][j]
    print(stock,alist) 
    clist = alist[:]
    clist.append(stock)
    clol[i][j]= '#'
    count = 0
    maxcompare = []
    if check(i-1,j):
        if lol[i-1][j] != '#':
            count+=1
            print('up')
            maxcompare.append(work(i-1,j,clol,clist))
    if check(i+1,j):
        if lol[i+1][j] != '#':
            count+=1
            print('down')
            maxcompare.append(work(i+1,j,clol,clist))
    if check(i,j-1):
        if lol[i][j-1] != '#':
            count+=1
            print('left')
            maxcompare.append(work(i,j-1,clol,clist))
    if check(i,j+1):
        if lol[i][j+1] != '#':
            count+=1
            print('right')
            maxcompare.append(work(i,j+1,clol,clist))
    if count ==0:
        print(clist)
        num = int(''.join(clist))
        return num
    else:
        print('maxcompare',maxcompare)
        return max(maxcompare)
def check(i,j):
    global m,n
    if 0<=i and i<m:
        if 0<=j and j<n:
            return True
    return False
    
mlist =[]
[m,n] = list(map(int,input().split(' ')))
slol = []
for i in range(m):
    slol.append(list(input()))
print(slol)
for i in range(m):
    for j in range(n):
        if slol[i][j] != '#':
            alist = []
            mlist.append(work(i,j,slol,alist))
print(max(mlist))
```