Problem:
薄氷渡り
問題
冬の寒いある日，JOI太郎君は広場にはった薄氷を割って遊ぶことにした．広場は長方形で，東西方向に m 個，南北方向に n 個，つまり， m × n の区画に区切られている．また，薄氷が有る区画と無い区画がある． JOI太郎君は，次のルールにしたがって，薄氷を割りながら区画を移動することにした．
薄氷があるどの区画からも薄氷を割り始めることができる．
東西南北のいずれかの方向に隣接し， まだ割られていない薄氷のある区画に移動できる．
移動した先の区画の薄氷をかならず割る．
JOI太郎君が薄氷を割りながら移動できる区画数の最大値を求めるプログラムを作成せよ．ただし， 1 ≤ m ≤ 90，1 ≤ n ≤ 90 である．与えられる入力データでは，移動方法は20万通りを超えない．
入力
入力は複数のデータセットからなる．各データセットは以下の形式で与えられる．
入力はn+2行ある． 1 行目には整数 m が書かれている． 2 行目には整数 n が書かれている． 3 行目から n+2 行目までの各行には、0 もしくは 1 が， 空白で区切られて m 個書かれており，各区画に薄氷があるか否かを表している．北から i 番目，西からj番目の区画を (i,j) と記述することにすると (1 ≤ i ≤ n, 1 ≤ j ≤ m)，第 i+2 行目の j 番目の値は，区画 (i,j) に薄氷が存在する場合は 1 となり，区画 (i,j) に薄氷が存在しない場合は 0 となる．
m, n がともに 0 のとき入力の終了を示す. データセットの数は 5 を超えない．
出力
データセットごとに, 移動できる区画数の最大値を１行に出力せよ．
入出力例
入力例
3
3
1 1 0
1 0 1
1 1 0
5
3
1 1 1 0 1
1 1 0 0 0
1 0 0 0 1
0
0
出力例
5
5
1つ目の入力例に対して，最大値を与える移動方法の例
2つ目の入力例に対して，最大値を与える移動方法の例
2つ目の入力例に対して，最大値を与えない移動方法の例
上記問題文と自動審判に使われるデータは、
情報オリンピック日本委員会
が作成し公開している問題文と採点用テストデータです。

Submitted solutions:

code_snippet: #include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

const int dx[4] = {0, 0, -1, 1};
const int dy[4] = {-1, 1, 0, 0};
int m, n;
int max_length;

void dfs(int y, int x, const vector<vector<int>>& grid, vector<vector<bool>>& visited, int depth) {
    max_length = max(max_length, depth);
    visited[y][x] = true;

    for (int dir = 0; dir < 4; ++dir) {
        int ny = y + dy[dir];
        int nx = x + dx[dir];

        if (ny < 0 || ny >= n || nx < 0 || nx >= m) continue;
        if (grid[ny][nx] == 0 || visited[ny][nx]) continue;

        dfs(ny, nx, grid, visited, depth + 1);
    }

    visited[y][x] = false; // バックトラック
}

int main() {
    while (cin >> m >> n) {
        if (m == 0 && n == 0) break;

        vector<vector<int>> grid(n, vector<int>(m));
        for (int i = 0; i < n; ++i)
            for (int j = 0; j < m; ++j)
                cin >> grid[i][j];

        max_length = -1;

        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < m; ++j) {
                if (grid[i][j] == 1) {
                    vector<vector<bool>> visited(n, vector<bool>(m, false));
                    dfs(i, j, grid, visited, 1);
                }
            }
        }

        cout << max_length << endl;
    }

    return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 3368
final_rank: 7

code_snippet: #pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
// #include <atcoder/modint>
// #include <atcoder/fenwicktree>
// #include <atcoder/segtree>
// #include <atcoder/scc>

#define int long long
#define all(x) begin(x), end(x)

using namespace std;
// using namespace atcoder;
// using mint = modint998244353;
// using mint = modint1000000007;
using ld = long double;
using pii = pair<int, int>;
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using vp = vector<pii>;
using vvp = vector<vp>;
using vs = vector<string>;
using vvc = vector<vector<char>>;

void debug(vector<int> a) {
    for (auto x : a) cout << x << ' ';
    cout << endl;
}

void debug(vector<vector<int>> a) {
    for (auto y : a) debug(y);
}

template <typename T1, typename T2>
inline bool chmax(T1 &a, T2 b) {return a < b and (a = b, true);}

template <typename T1, typename T2>
inline bool chmin(T1 &a, T2 b) {return a > b and (a = b, true);}

const int supl = LONG_LONG_MAX - 100;

int solve(int h, int w) {
    vvi as(h, vi(w)); for (auto& y : as) for (auto& x : y) cin >> x;

    int d[] = {-1, 1};
    auto in = [&](int y, int x) {
        return 0 <= y and y < h and 0 <= x and x < w;
    };

    int ans = 0;
    
    for (int i = 0 ; i < h ; i++) for (int j = 0 ; j < w ; j++) {
        vvi used(h, vi(w));
        function<int(int, int, int)> dfs = [&](int y, int x, int v) {
            used[y][x] = 1;
            int res = v;
            for (auto dy : d) if (in(y + dy, x)) if (!used[y + dy][x]) {
                // used[y + dy][x] = 1;
                if (as[y + dy][x]) {
                    chmax(res, dfs(y + dy, x, v + 1));
                    used[y + dy][x] = 0;
                }
            }
            for (auto dx : d) if (in(y, x + dx)) if (!used[y][x + dx]) {
                // used[y][x + dx] = 1;
                if (as[y][x + dx]) {
                    chmax(res, dfs(y, x + dx, v + 1));
                    used[y][x + dx] = 0;
                }
            }

            return res;
        };

        // cout << i << ' ' << j << endl;
        // used[i][j] = 1;
        if (as[i][j]) {
            chmax(ans, dfs(i, j, 1));
        }
    }

    return ans;
}

void main_() {
    while(1) {
        int w, h; cin >> w >> h;
        if (!w) break;
        cout << solve(h, w) << endl;
    }
}

signed main() {
    ios::sync_with_stdio(false);
    std::cin.tie(nullptr);

    main_();

    return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 3512
final_rank: 10

code_snippet: #include <bits/stdc++.h>

using namespace std;

using VI = vector<int>;
using VVI = vector<VI>;

VI dx = {-1, 0, 1, 0};
VI dy = {0, -1, 0, 1};  // 左、上、右、下

// 深さ優先探索
void one_stroke(VVI s, VI& v, int len, int y, int x) {
    if (s[y][x] == 0) {
        return;
    } else {
        ++len;
        s[y][x] = 0;
        for (int i = 0; i < 4; ++i) {
            if (s[y + dy[i]][x + dx[i]] == 1) {
                one_stroke(s, v, len, y + dy[i], x + dx[i]);
            }
        }
        v.push_back(len);
    }
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);

    while (true) {
        int m, n;
        cin >> m;
        cin >> n;
        if (n == 0 && m == 0) break;

        VVI square(n + 2, VI(m + 2, 0));
        for (int i = 1; i <= n; ++i) {
            for (int j = 1; j <= m; ++j) {
                cin >> square[i][j];
            }
        }

        int max_len = 0;
        for (int i = 1; i <= n; ++i) {
            for (int j = 1; j <= m; ++j) {
                VI vec;
                one_stroke(square, vec, 0, i, j);
                if (vec.size() > 0) {
                    max_len =
                        max(max_len, *max_element(vec.begin(), vec.end()));
                }
            }
        }
        cout << max_len << endl;
    }

    return 0;
}
accuracy: 1
time_ms: 550
memory_kb: 3992
final_rank: 13

code_snippet: #include "bits/stdc++.h"
using namespace std;
long long DFS(vector<vector<int>>& A, int x, int y, vector<vector<bool>>& reach) {
    if (A.at(y).at(x) == 0) return 0;
    if (reach.at(y).at(x)) return 0;
    long long ans = 0;
    reach.at(y).at(x) = true;
    ans = max(ans, DFS(A, x, y + 1, reach));
    ans = max(ans, DFS(A, x, y - 1, reach));
    ans = max(ans, DFS(A, x + 1, y, reach));
    ans = max(ans, DFS(A, x - 1, y, reach));
    reach.at(y).at(x) = false;
    return ans + 1;
}
int main() {
    int H, W;
    cin >> W >> H;
    while (W != 0 || H != 0) {
    vector<vector<int>>A(H + 2, vector<int>(W + 2));
    for (int i = 0; i < H + 2; i++) {
        for (int j = 0; j < W + 2; j++) {
            if (i == 0 || i == H + 1) {
                A.at(i).at(j) = 0;
            }
            else if (j == 0 || i == W + 1) {
                A.at(i).at(j) = 0;
            }
        }
    }
    for (int i = 1; i <= H; i++) {
        for (int j = 1; j <= W; j++) {
            cin >> A.at(i).at(j);
        }
    }
    long long ans = -1;
    for (int i = 1; i <= H; i++) {
        for (int j = 1; j <= W; j++) {
            vector<vector<bool>>reach(H + 2, vector<bool>(W + 2, false));
            ans = max(ans, DFS(A, j, i, reach));
        }
    }
    cout << ans << endl;
    cin >> W >> H;
    }
}
accuracy: 1
time_ms: 60
memory_kb: 3488
final_rank: 11

code_snippet: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, N) for (int i = 0; i < N; i++)

bool nm[100][100] = {};
bool isvisit[100][100];
int cnt = 0;
int ans = 0;

void dfs(int i, int j)
{
  //cout << " dfs i j " << i << " " << j << endl;
  if(isvisit[i][j]) return;
  isvisit[i][j] = true;
  cnt++;
  ans = max(ans, cnt);
  if (nm[i - 1][j])
  {
    dfs(i - 1, j);
  }
  if (nm[i][j - 1])
  {
    dfs(i, j - 1);
  }
  if (nm[i + 1][j])
  {
    dfs(i + 1, j);
  }
  if (nm[i][j + 1])
  {
    dfs(i, j + 1);
  }
  cnt--;
  isvisit[i][j] = false;
}

int main()
{
  while(1){


 nm[100][100] = {};

cnt = 0;
ans = 0;

  int m, n;
  cin >> m >> n;
  if(m==0 && n==0) return 0;
  rep(i, n) rep(j, m) cin >> nm[i + 1][j + 1];

  rep(i, n)
  {
    rep(j, m)
    {
      rep(ii,n){
        rep(jj,m){
          isvisit[ii][jj] = false;
        }
      }
      //i++;
     // j++;
      if (nm[i+1][j+1])
      {
        dfs(i+1, j+1);
      }
    }
  }
  cout << ans << endl;
  }
}
accuracy: 1
time_ms: 20
memory_kb: 3092
final_rank: 3

code_snippet: #include <bits/stdc++.h>
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define drep(i,j,n) for(int i=0;i<(int)(n-1);i++)for(int j=i+1;j<(int)(n);j++)
#define trep(i,j,k,n) for(int i=0;i<(int)(n-2);i++)for(int j=i+1;j<(int)(n-1);j++)for(int k=j+1;k<(int)(n);k++)
#define codefor int test;scanf("%d",&test);while(test--)
#define INT(...) int __VA_ARGS__;in(__VA_ARGS__)
#define LL(...) ll __VA_ARGS__;in(__VA_ARGS__)
#define yes(ans) if(ans)printf("yes\n");else printf("no\n")
#define Yes(ans) if(ans)printf("Yes\n");else printf("No\n")
#define YES(ans) if(ans)printf("YES\n");else printf("NO\n")
#define vector2d(type,name,h,...) vector<vector<type>>name(h,vector<type>(__VA_ARGS__))
#define vector3d(type,name,h,w,...) vector<vector<vector<type>>>name(h,vector<vector<type>>(w,vector<type>(__VA_ARGS__)))
#define umap unordered_map
#define uset unordered_set
using namespace std;
using ll = long long;
const int MOD=1000000007;
const int MOD2=998244353;
const int INF=1<<30;
const ll INF2=(ll)1<<60;
//入力系
void scan(int& a){scanf("%d",&a);}
void scan(long long& a){scanf("%lld",&a);}
template<class T,class L>void scan(pair<T, L>& p){scan(p.first);scan(p.second);}
template<class T> void scan(T& a){cin>>a;}
template<class T> void scan(vector<T>& vec){for(auto&& it:vec)scan(it);}
void in(){}
template <class Head, class... Tail> void in(Head& head, Tail&... tail){scan(head);in(tail...);}
//出力系
void print(const int& a){printf("%d",a);}
void print(const long long& a){printf("%lld",a);}
void print(const double& a){printf("%.15lf",a);}
template<class T,class L>void print(const pair<T, L>& p){print(p.first);putchar(' ');print(p.second);}
template<class T> void print(const T& a){cout<<a;}
template<class T> void print(const vector<T>& vec){if(vec.empty())return;print(vec[0]);for(auto it=vec.begin();++it!= vec.end();){putchar(' ');print(*it);}}
void out(){putchar('\n');}
template<class T> void out(const T& t){print(t);putchar('\n');}
template <class Head, class... Tail> void out(const Head& head,const Tail&... tail){print(head);putchar(' ');out(tail...);}
//デバッグ系
template<class T> void dprint(const T& a){cerr<<a;}
template<class T> void dprint(const vector<T>& vec){if(vec.empty())return;cerr<<vec[0];for(auto it=vec.begin();++it!= vec.end();){cerr<<" "<<*it;}}
void debug(){cerr<<endl;}
template<class T> void debug(const T& t){dprint(t);cerr<<endl;}
template <class Head, class... Tail> void debug(const Head& head, const Tail&... tail){dprint(head);cerr<<" ";debug(tail...);}
ll intpow(ll a, ll b){ ll ans = 1; while(b){ if(b & 1) ans *= a; a *= a; b /= 2; } return ans; }
ll modpow(ll a, ll b, ll p){ ll ans = 1; while(b){ if(b & 1) (ans *= a) %= p; (a *= a) %= p; b /= 2; } return ans; }
ll modinv(ll a, ll m) {ll b = m, u = 1, v = 0;while (b) {ll t = a / b;a -= t * b; swap(a, b);u -= t * v; swap(u, v);}u %= m;if (u < 0) u += m;return u;}
ll updivide(ll a,ll b){if(a%b==0) return a/b;else return (a/b)+1;}
template<class T> void chmax(T &a,const T b){if(b>a)a=b;}
template<class T> void chmin(T &a,const T b){if(b<a)a=b;}

int rec(int y,int x,vector<vector<bool>> visited,vector<vector<int>>& mat){
    int dist=1,dy,dx,h=mat.size(),w=mat[0].size();
    visited[y][x]=true;
    for(int i=0;i<4;i++){
        dy=(i==0)-(i==1);
        dx=(i==2)-(i==3);
        if(y+dy<0||x+dx<0||y+dy>=h||x+dx>=w)continue;
        if(mat[y+dy][x+dx]==1&&!visited[y+dy][x+dx]){
            chmax(dist,rec(y+dy,x+dx,visited,mat)+1);
        }
    }
    return dist;
}

int main(){
    while(1){
    INT(w,h);
    if(w==0&&h==0)return 0;
    vector2d(int,mat,h,w);
    vector2d(bool,visited,h,w,false);
    in(mat);
    int ans=0;
    rep(y,h){
        rep(x,w){
            if(mat[y][x]==1)chmax(ans,rec(y,x,visited,mat));
        }
    }
    out(ans);
    }
}
accuracy: 1
time_ms: 870
memory_kb: 3428
final_rank: 14

code_snippet: #include<iostream>
#include<vector>

const int dx[] = {-1, 0, +1, 0};
const int dy[] = {0, -1, 0, +1};

void dfs(std::vector<std::vector<int> > field, int n, int m, int cnt, int &ans){
    field[n][m] = 0;
    bool f = true;
    for(int i=0; i<4; ++i){
        int x = dx[i] + m;
        int y = dy[i] + n;
        if(field[y][x] != 0){
            dfs(field, y, x, cnt+1, ans);
            f = false;
        }
    }
    field[n][m] = 1;
    if(f && cnt+1 > ans){
        ans = cnt+1;
    }
    return;
}

int main(){
    int M,N;
    while(std::cin >> M >> N, M || N){
        std::vector<std::vector<int> > field(N+2, std::vector<int>(M+2,0));
        for(int n=1; n<=N; ++n){
            for(int m=1; m<=M; ++m){
                std::cin >> field[n][m];
            }
        }

        int ans = 0;
        for(int n=1; n<=N; ++n){
            for(int m=1; m<M; ++m){
                    if(field[n][m] == 1){
                    int tmp = 0;
                    dfs(field, n, m, 0, tmp);
                    ans = std::max(ans, tmp);
                }
            }
        }
        std::cout << ans << std::endl;
    }

    return 0;
}
accuracy: 1
time_ms: 870
memory_kb: 3904
final_rank: 18

code_snippet: #include<bits/stdc++.h>

#define _GLIBCXX_DEBUG
#define all(v) (v).begin(),(v).end()

using namespace std;

using ll=long long;
using pii=pair<int, int>;
using vi=vector<int>;

const ll INF=1LL<<60;
const int MOD=1e9+7;

int dx[4]={1, 0, -1, 0};
int dy[4]={0, 1, 0, -1};
vector<vector<bool>> visit;
int m, n;

int dfs(vector<vi> ice, int x, int y){
    int MAX=1;
    visit[x][y]=true;
    for(int i=0; i<4; i++){
        int nx=x+dx[i];
        int ny=y+dy[i];
        if(nx<0 || nx>=n || ny<0 || ny>=m) continue;
        if(ice[nx][ny] == 0 || visit[nx][ny]) continue;
        MAX=max(MAX, dfs(ice, nx, ny)+1);
    }
    visit[x][y]=false;
    return MAX;
}

int main(){
    while(cin >> m >> n){
        if(m==0 && n==0) break;
        visit.assign(n, vector<bool>(m));
        vector<vi> ice(n, vi(m));
        for(int i=0; i<n; i++){
            for(int j=0; j<m; j++){
                cin >> ice[i][j];
            }
        }
        int res=0;
        for(int i=0; i<n; i++){
            for(int j=0; j<m; j++){
                if(ice[i][j] == 0) continue;
                res=max(res,dfs(ice, i, j));
            }
        }
        cout << res << endl;
    }
    return 0;
}
accuracy: 1
time_ms: 870
memory_kb: 3884
final_rank: 17

code_snippet: #include <iostream>
#include <queue>

using namespace std;
const int MAX_N = 90;
const int MAX_M = 90;
struct map {
    int maze[MAX_N + 1][MAX_M + 1];
    int skcount[MAX_M];
}mapmap[5];
int X[4] = {0, 0, 1, -1};
int Y[4] = {1, -1, 0, 0};
int m[5];
int n[5];
int k, l;
int res = 0;
struct node{
    int x, y;
    int step;
    bool last[MAX_N][MAX_M];
}Node;
queue<node> q;

bool test(int x, int y, node S,int f){
    if(x >= n[f] || x < 0 || y >= m[f] || y < 0) return false;
    if(mapmap[f].maze[x][y] == 0) return false;
    if(S.last[x][y] == true) return false;
    return true;
}

void BFS(node S,int f){
    q.push(S);
    node top = q.front();
    q.pop();
    for(int i = 0; i < 4; i++) {
        int newX = top.x + X[i];
        int newY = top.y + Y[i];
        if (test(newX, newY, Node,f)) {
            Node.x = newX, Node.y = newY;
            Node.step = top.step + 1;
            mapmap[f].skcount[Node.step]++;
            Node.last[newX][newY] = true;
            BFS(Node,f);
        }
    }
    Node.last[top.x][top.y] = false;
    return;
}

int main() {
    for(int f = 0; f < 5; f++) {
        cin >> m[f];
        cin >> n[f];
        for (int i = 0; i < n[f]; i++) {
            for (int j = 0; j < m[f]; j++) {
                cin >> mapmap[f].maze[i][j];
            }
        }
        for (k = 0; k < n[f]; k++) {
            for (l = 0; l < m[f]; l++) {
                if (mapmap[f].maze[k][l] == 1) {
                    node S;
                    S.x = k;
                    S.y = l;
                    Node.last[k][l] = true;
                    S.step = 1;
                    mapmap[f].skcount[S.step]++;
                    BFS(S,f);
                }
            }
        }
        if(m[f] == 0 || n[f] == 0) break;
        res++;
    }
    for(int f = 0; f < res; f++){
        for (int i = 1; i < MAX_M; i++) {
            if (mapmap[f].skcount[i] == 0) {
                cout << i - 1;
                break;
            }
        }
        cout << '\n';
    }
    return 0;
}
accuracy: 1
time_ms: 220
memory_kb: 3780
final_rank: 12

code_snippet: #include<bits/stdc++.h>
using namespace std;

int solve(int x,int y,vector< vector<int> > a,int ma)
{
    ma++;
    int bx[4]={1,0,-1,0},by[4]={0,1,0,-1};
    int p=ma;
    for(int i=0;i<4;i++){
        if(a[x+bx[i]][y+by[i]]==1){
            a[x][y]=0;
            ma=max(ma,solve(x+bx[i],y+by[i],a,p));
        }
    }
    return ma;
}

int main()
{
    int n,m;
    for(;;){
        cin >> n >> m;
        if(n==0&&m==0) break;
        vector< vector<int> > a(m+2);
        for(int i=0;i<n+2;i++)
            a[0].push_back(0);
        int in;
        for(int i=1;i<m+1;i++){
            a[i].push_back(0);
            for(int j=1;j<n+1;j++){
                cin >> in;
                a[i].push_back(in);
            }
            a[i].push_back(0);
        }
        for(int i=0;i<n+2;i++)
            a[m+1].push_back(0);
        int p=0;
        for(int i=1;i<m+1;i++)
            for(int j=1;j<n+1;j++){
                int ma=0;
                if(a[i][j]==0) continue;
                p=max(p,solve(i,j,a,ma));
            }
        cout << p << endl;
    }
    return 0;
}
accuracy: 1
time_ms: 880
memory_kb: 3892
final_rank: 19

code_snippet: #include <bits/stdc++.h>
#define range(i, a, b) for(int i = (a); i < (b); i++)
#define rep(i, a) range(i, 0, a)
using namespace std;

int w, h;
vector <vector <int>> ice, used;

int rec(int y, int x) {
    if (x < 0 || w <= x || y < 0 || h <= y) return 0;

    int res = 0;

    int dy[] = { 1, 0, -1,  0},
        dx[] = { 0, 1,  0, -1};

    rep (i, 4) {
        int yy = y + dy[i], xx = x + dx[i];
        if (xx < 0 || w <= xx || yy < 0 || h <= yy) continue;
        if (!ice[yy][xx]) continue;
        if (used[yy][xx]) continue;
        used[yy][xx] = true;
        res = max(res, rec(yy, xx) + 1);
        used[yy][xx] = false;
    }

    return res;
}

int main() {
    while(cin >> w >> h, w) {
        ice  = vector <vector <int>> (w, vector <int> (h));
        rep (i, h) rep (j, w) cin >> ice[i][j];
        int ans = 0;
        rep (i, h) rep (j, w) {
            used = vector <vector <int>> (w, vector <int> (h, false));
            used[i][j] = true;
            ans = max(ans, rec(i, j));
        }
        cout << ans << endl;
    }
    return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 3200
final_rank: 6

code_snippet: #include <iostream>
#include <vector>
using namespace std;

int solve(vector<vector<int> > map, int x, int y, int walk) {
	int tmp = 1;
	int flag = 0;
	if (map[x - 1][y]) {
		map[x - 1][y] = 0;
		int result = solve(map, x - 1, y, walk + 1);
		if (tmp < result) tmp = result;
		map[x - 1][y] = 1;
		flag++;
	}
	if (map[x + 1][y]) {
		map[x + 1][y] = 0;
		int result = solve(map, x + 1, y, walk + 1);
		if (tmp < result) tmp = result;
		map[x + 1][y] = 1;
		flag++;
	}
	if (map[x][y + 1]) {
		map[x][y + 1] = 0;
		int result = solve(map, x, y + 1, walk + 1);
		if (tmp < result) tmp = result;
		map[x][y + 1] = 1;
		flag++;
	}
	if (map[x][y - 1]) {
		map[x][y - 1] = 0;
		int result = solve(map, x, y - 1, walk + 1);
		if (tmp < result) tmp = result;
		map[x][y - 1] = 1;
		flag++;
	}
	if (!flag)return walk;
	return tmp;
}

int main() {
	while (1) {
		int m, n;
		cin >> m >> n;
		if (m == 0 && n == 0)break;
		vector<vector<int> > map(m + 2, vector<int>(n + 2, 0));
		for (int i = 0; i < m; i++)for (int j = 0; j < n; j++) cin >> map[i + 1][j + 1];

		int max = 0;
		for (int i = 0; i < m; i++)for (int j = 0; j < n; j++) {
			if (map[i + 1][j + 1] == 0)continue;
			map[i + 1][j + 1] = 0;
			int result = solve(map, i + 1, j + 1, 1);
			if (max < result)max = result;
			map[i + 1][j + 1] = 1;
		}

		cout << max << endl;
	}
	return 0;
}
accuracy: 1
time_ms: 880
memory_kb: 3840
final_rank: 16

code_snippet: #include<stdio.h>

#include<algorithm>
#include<iostream>
#include<stack>

using namespace std ;

#define y first
#define x second

#define NM_MAX 90

int mx[] = { 0 ,1 ,-1 ,0 } ;
int my[] = { 1 ,0 ,0 ,-1 } ;

struct Data
{
	int cnt ;
	pair<int ,int> p ;
	bool map[ NM_MAX + 2 ][ NM_MAX + 2 ] ;
};


int main( void )
{
	Data init = { 0 ,make_pair(1 ,1) ,false } ;
	stack< pair<int ,int> > start ;

	int W ,H ;
	while( true )
	{
		scanf( "%d %d" ,&W ,&H ) ;

		if( W | H )
		{
			int ans = 0 ;

			int i ,j ;
			for( i = 1 ; i <= H ; ++i )
			{
				for( j = 1 ; j <= W ; ++j )
				{
					int input ;
					scanf( "%d" ,&input ) ;

					init.map[ i ][ j ] = (input == 0 ? false : true ) ;

					if( init.map[ i ][ j ] )
					{
						start.push( make_pair(i ,j) ) ;
					}
				}
			}

			stack< Data > st ;

			while( !start.empty() )
			{
				init.p = start.top() ;
				start.pop() ;

				st.push( init ) ;
				while( !st.empty() )
				{
					Data move = st.top() ;
					st.pop() ;

					int X = move.p.x ;
					int Y = move.p.y ;

					++move.cnt ;
					move.map[ Y ][ X ] = false ;
					ans = max( ans ,move.cnt ) ;

					Data copy = move ;

					for( i = 0 ; i < 4 ; ++i )
					{
						int nextX = X + mx[ i ] ;
						int nextY = Y + my[ i ] ;

						if( move.map[ nextY ][ nextX ] == true )
						{
							copy.p.x = nextX ;
							copy.p.y = nextY ;

							st.push( copy ) ;
						}
					}
				}
			}

			printf( "%d\n" ,ans ) ;
		}
		
		else
		{
			return 0 ;
		}
	}
}
accuracy: 1
time_ms: 80
memory_kb: 3384
final_rank: 9

code_snippet: #include <bits/stdc++.h>
using namespace std;

const int MAX = 90;

int a[MAX][MAX];
int c[MAX][MAX];

const int dx[] = { 1, 0, -1, 0 };
const int dy[] = { 0, 1, 0, -1 };

int m, n;

int dfs(int x, int y) {
	c[x][y] = 0;
	int ma = 0;
	for (int i = 0; i < 4; i++) {
		int tx = x + dx[i], ty = y + dy[i];
		if (0 <= tx && tx < n && 0 <= ty && ty < m && c[tx][ty]) {
			ma = max(ma, dfs(tx, ty));
		}
	}
	c[x][y] = 1;
	return ma + 1;
}

int main()
{
	while (cin >> m >> n, m | n) {
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < m; j++) {
				cin >> a[i][j];
			}
		}
		int res = 0;
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < m; j++) {
				if (a[i][j] == 1) {
					for (int x = 0; x < n; x++) {
						for (int y = 0; y < m; y++) {
							c[x][y] = a[x][y];
						}
					}
					res = max(res, dfs(i, j));
				}
			}
		}
		cout << res << endl;
	}
	return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3132
final_rank: 5

code_snippet: //http://judge.u-aizu.ac.jp/onlinejudge/review.jsp?rid=2676345#1
//http://judge.u-aizu.ac.jp/onlinejudge/review.jsp?rid=2676345#1
#include <iostream>
#include<cstdlib>
#include<queue>
#include<set>
#include<vector>
#include<string>
#include<algorithm>
#include<stack>
#include<map>
#include<cstdio>
#include<fstream>
#include<cstring>
using namespace std;
#define rep(i,a) for(int i=0;i<a;i++)
#define mp make_pair
#define P pair<int,int>
#define ll __int64

int m,n;
int table[100][100];
bool used[100][100];
int  u[100][100];
int ans=0;
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};

int dfs(set<P>jo,int y,int x){
	if(y<0||x<0||y>=n||x>=m||table[y][x]==0)return 0;
	if(u[y][x]==1)return 0;

	//cout<<y<<" "<<x<<" "<<jo.size()<<endl;
	int ma=0;
	jo.insert(mp(y,x));
	int ny,nx;
	rep(i,4){
			ny=y+dy[i];
			nx=x+dx[i];
			if(jo.find(mp(ny,nx))==jo.end()){

				ma=max(dfs(jo,ny,nx)+1,ma);
			}
		//ma=max(dfs(y+dy[i],x+dx[i])+1,ma);
	}

	return ma;
}


int main(){
while(cin>>m>>n,n||m){
rep(i,100)memset(used[i],0,sizeof used[i]);
rep(i,100)memset(u[i],0,sizeof u[i]);
rep(i,100)memset(table[i],0,sizeof table[i]);

rep(i,n){
	rep(j,m)cin>>table[i][j];
}
int ans=0;

for(int i=0;i<n;i++){
	for(int j=0;j<m;j++){
			if(table[i][j]==0)continue;
			set<P>jo;
			jo.clear();
			int now=dfs(jo,i,j);
			ans=max(ans,now);
			//cout<<i<<" "<<j<<" "<<now<<endl;

	}
}
cout<<ans<<endl;
}

	return 0;
}
accuracy: 1
time_ms: 210
memory_kb: 3124
final_rank: 8

code_snippet: #include<bits/stdc++.h>
using namespace std;

int m, n;
int field[92][92];
bool visited[92][92];
int max_dist;
int dist;

void dfs(int i, int j){
	visited[i][j] = true;
	if(dist > max_dist){
		max_dist = dist;
	}
	if(field[i + 1][j] == 1 && visited[i + 1][j] == false){
		dist++;
		dfs(i + 1, j);
		visited[i + 1][j] = false;
		dist--;
	}
	if(field[i][j + 1] == 1 && visited[i][j + 1] == false){
		dist++;
		dfs(i, j + 1);
		visited[i][j + 1] = false;
		dist--;
	}
	if(field[i - 1][j] == 1 && visited[i - 1][j] == false){
		dist++;
		dfs(i - 1, j);
		visited[i - 1][j] = false;
		dist--;
	}
	if(field[i][j - 1] == 1 && visited[i][j - 1] == false){
		dist++;
		dfs(i, j - 1);
		visited[i][j - 1] = false;
		dist--;
	}
	return;
}

int main(){
	for(;;){
		cin >> m >> n;
		if(m == 0) break;
		for(int i = 0; i <= m + 1; i++){
			for(int j = 0; j <= n + 1; j++){
				field[i][j] = 0;
			}
		}
		for(int i = 1; i <= m; i++){
			for(int j = 1; j <= n; j++){
				cin >> field[i][j];
			}
		}
		max_dist = 0;
		for(int i = 1; i <= m; i++){
			for(int j = 1; j <= n; j++){
				for(int k = 0; k <= m + 1; k++){
					for(int l = 0; l <= n + 1; l++){
						visited[k][l] = false;
					}
				}
				dist = 0;
				dfs(i, j);
			}
		}
		cout << max_dist << endl;
	}
	return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 3036
final_rank: 2

code_snippet: #include <iostream>
#include <algorithm>
using namespace std;

int m, n;
int dfs(int b[100][100], int y, int x){
	int a[100][100];
	for(int i = 0;i < 100;i++)
	for(int j = 0;j < 100;j++)a[i][j] = b[i][j];
	if(!a[y][x])return 0;
	int ans = a[y][x] = 0;
	if(a[y-1][x])ans = max(ans, dfs(a, y-1, x));
	if(a[y+1][x])ans = max(ans, dfs(a, y+1, x));
	if(a[y][x-1])ans = max(ans, dfs(a, y, x-1));
	if(a[y][x+1])ans = max(ans, dfs(a, y, x+1));
	return ans+1;
}

int main(){
	int a[100][100];
	while(cin >> m >> n, m){
		fill((int*)a, (int*)(a+100), 0);
		for(int i = 1;i <= n;i++)
		for(int j = 1;j <= m;j++)cin >> a[i][j];
		
		int ans = 0;
		for(int i = 1;i <= n;i++)
		for(int j = 1;j <= m;j++)ans = max(ans, dfs(a, i, j));
		cout << ans << endl;
		//cout << ans << endl;
	}
	return 0;
}
accuracy: 1
time_ms: 860
memory_kb: 4348
final_rank: 20

code_snippet: #include "iostream"
#include "climits"
#include "list"
#include "queue"
#include "stack"
#include "set"
#include "functional"
#include "algorithm"
#include "math.h"
#include "utility"
#include "string"
#include "map"
#include "unordered_map"
#include "iomanip"
#include "random"

using namespace std;
const long long int MOD = 1000000007;

int H, W;
int ans = 0;

void Search(int x, int y, vector<vector<bool> >flag, int dis) {
	flag[y][x] = false;
	int d[5] = { 1,0,-1,0,1 };
	for (int k = 0; k < 4; k++) {
		if (flag[y + d[k + 1]][x + d[k]]) {
			Search(x + d[k], y + d[k + 1], flag, dis + 1);
		}
	}
	ans = max(ans, dis);
	return;
}

int main() {
	cin >> W;
	cin >> H;
	while (W&&H) {
		ans = 0;
		vector<vector<bool> >v(H+2);
		for (int i = 1; i <= H; i++) {
			v[i].push_back(0);
			for (int j = 1; j <= W; j++) {
				bool box;
				cin >> box;
				v[i].push_back(box);
			}
			v[i].push_back(0);
		}
		for (int i = 0; i <= W + 1; i++) {
			v[0].push_back(0);
			v[H + 1].push_back(0);
		}
		for (int i = 1; i <= H; i++) {
			for (int j = 1; j <= W; j++) {
				if(v[i][j])	Search(j, i, v, 1);
			}
		}
		cout << ans << endl;
		cin >> W;
		cin >> H;
	}
	return 0;
}
accuracy: 1
time_ms: 950
memory_kb: 3348
final_rank: 15

code_snippet: #include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <queue>
#include <map>
#include <utility>
#include <functional>

using namespace std;

//????????????

int dx[] = { 0, 1, 0, -1 };
int dy[] = { -1, 0, 1, 0 };
int m, n, f[90][90], ans;
bool used[90][90];

void dfs(int y, int x, int d) {
	used[y][x] = true;
	ans = max(ans, d);
	for (int i = 0; i < 4; ++i) {
		int ny = y + dy[i];
		int nx = x + dx[i];
		if (ny < 0 || ny >= n || nx < 0 || nx >= m || f[ny][nx] == 0 || used[ny][nx] == true)
			continue;
		dfs(ny, nx, d + 1);
	}
	used[y][x] = false;
}

int main() {
	while (cin >> m >> n && m != 0) {
		for (int i = 0; i < n; ++i) {
			for (int j = 0; j < m; ++j) {
				cin >> f[i][j];
			}
		}

		ans = 0;
		for (int i = 0; i < n; ++i) {
			for (int j = 0; j < m; ++j) {
				if (f[i][j]) {
					fill(used[0], used[n], false);
					dfs(i, j, 1);
				}
			}
		}
		cout << ans << endl;
	}

	return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3116
final_rank: 4

code_snippet: #include<stdio.h>
int i,j,n,m,y[101][101],max,cnt,dx[4]={0,-1,0,1},dy[4]={-1,0,1,0};
void func(int s,int x)
{
	int ss,xx,i;
	for(i=0;i<4;i++){
		ss=s+dy[i]; xx=x+dx[i];
		if(y[ss][xx]==1&&0<=ss&&ss<n&&0<=xx&&xx<m){
			cnt++;
			y[ss][xx]=0;
			if(max<cnt) max=cnt;
			func(ss,xx);
			y[ss][xx]=1; cnt--;
		}
	}
}
int main(void)
{
	while(1){
		scanf("%d%d",&m,&n);
		if(m==0&&n==0) break;
		for(i=0;i<n;i++){
			for(j=0;j<m;j++){
				scanf("%d",&y[i][j]);
			}
		}
		max=0;
		for(i=0;i<n;i++){
			for(j=0;j<m;j++){
				cnt=0;
				func(i,j);
			}
		}
		printf("%d\n",max);
	}
	return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 2624
final_rank: 1