【題目敘述】https://tioj.ck.tp.edu.tw/problems/1233
#include <iostream>
using namespace std;
int m, n, a[515][515], ans, cost[11] = {0, 1, 2, 4, 6, 10, 12, 14, 16, 18, 20};
string s;
int f(int x, int y){
if (x < 0 || y < 0 || x >= m || y >= n) return 0;
if (a[x][y] == 0) return 0;
a[x][y] = 0;
f(x+1, y+1);
f(x+1, y);
f(x+1, y-1);
f(x, y-1);
f(x-1, y-1);
f(x-1, y);
f(x-1, y+1);
f(x, y+1);
return 1;
}
int main() {
while (cin >> m >> n){
for (int i = 0; i < m; i++){
cin >> s;
for (int j = 0; j < n; j++){
a[i][j] = s[j]-'0';
}
}
for (int i = 0; i < m; i++){
for (int j = 0; j < n; j++){
ans += f(i, j);
}
}
if (ans > cost[10]) cout << 10 << "\n";
for (int i = 0; i <= 10; i++){
if (cost[i] >= ans){
cout << i << "\n";
break;
}
}
}
}