# k930. P7. 興建圍籬 (Fence)
## 題目連結: [k930](https://zerojudge.tw/ShowProblem?problemid=k930)
## 解題想法
* `table`紀錄格子是花地或空地
* `found`紀錄格子是否被找到
* 判斷要不要放圍籬的方法: 看花地旁邊的格子是不是空地(上下左右都要看)
-------------------------
1. 先按照順序找花地
2. 如果那格花地沒被找過,就進入`garden()`
3. 進入`garden()`後,判斷當前格子的上下左右是花地或空地
4. 如果上下左右是花地,就`garden()`那格格子,形成遞迴
5. 如果上下左右是空地,就代表要放圍籬了
6. 最後把`garden()`的回傳值加入到`total`陣列
7. `total`排序,照順序輸出
## 程式碼
```python=
def garden(x,y):
global found, table
count = 0
found[x][y] = 1
if table[x][y-1] == 0 and found[x][y-1] == 0:
count += 1
elif table[x][y-1] == 1 and found[x][y-1] == 0:
count += garden(x,y-1)
if table[x][y+1] == 0 and found[x][y+1] == 0:
count += 1
elif table[x][y+1] == 1 and found[x][y+1] == 0:
count += garden(x,y+1)
if table[x-1][y] == 0 and found[x-1][y] == 0:
count += 1
elif table[x-1][y] == 1 and found[x-1][y] == 0:
count += garden(x-1,y)
if table[x+1][y] == 0 and found[x+1][y] == 0:
count += 1
elif table[x+1][y] == 1 and found[x+1][y] == 0:
count += garden(x+1,y)
return count
#start
x,y = map(int,input().split())
found = [[0 for i in range(y)] for j in range(x)]
table = []
for i in range(x):
table.append([int(j) for j in input().split()])
total = []
for i in range(1,x):
for j in range(1,y):
if found[i][j] == 0 and table[i][j] == 1:
found[i][j] = 1
total.append(garden(i,j))
total.sort()
for i in total:
print(i,end = " ")
```
*(本題在Zero Judge上只通過了約一半的測資,剩下都TLE)*