1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
| #include <iostream>
#include <vector>
#include <string>
#include <cstring>
#include <algorithm>
#include <sstream>
#include <map>
#include <set>
#define REP(i,k,n) for(int i=k;i<n;i++)
#define rep(i,n) for(int i=0;i<n;i++)
#define INF 1<<30
#define pb push_back
#define mp make_pair
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
int w,h;
int dx[4] = {1,0,-1,0};
int dy[4] = {0,1,0,-1};
int f[100][100];
int ans = 0;
bool can(int y,int x) {
if(0 <= y && y < h && 0 <= x && x < w) return true;
return false;
}
void dfs(set<int> S, int y, int x) {
bool flag = true;
rep(i, 4) {
int ny = y + dy[i];
int nx = x + dx[i];
int id = ny * h + nx;
if(can(ny, nx) && f[ny][nx] == 1 && S.find(id) == S.end() ) {
flag = false;
S.insert(id);
dfs(S, ny, nx);
S.erase(id);
}
}
if(flag) {
ans = max(ans, (int)S.size());
}
}
int main() {
while(cin >> w >> h) {
if(w == 0 && h == 0) break;
rep(i, h) {
rep(j, w) cin >> f[i][j];
}
ans = 0;
rep(i, h) {
rep(j, w) {
if(f[i][j]){
set<int> S;
S.insert(i * h + j);
dfs(S, i, j);
}
}
}
cout << ans << endl;
}
return 0;
}
|