Buggy python submission (verdict: Time Limit Exceeded):
from copy import deepcopy

while True:
    m = int(raw_input())
    n = int(raw_input())

    if m == 0 and n == 0:
        break

    begin = [[False] * m for y in xrange(n)]

    for y in xrange(n):
        line = map(int, raw_input().split(' '))
        for x in xrange(m):
            if line[x] == 1:
                begin[y][x] = True

    # (stage, current position(x, y), current point)
    que = []
    for x in xrange(m):
        for y in xrange(n):
            if begin[y][x]:
                stage = deepcopy(begin)
                stage[y][x] = False
                que.append((stage, (x, y), 1))


    dx = [1, 0, -1, 0]
    dy = [0, 1, 0, -1]

    ans = 0

    while que:
        stage = que.pop()
        for i in xrange(4):
            nx = dx[i] + stage[1][0]
            ny = dy[i] + stage[1][1]
            if 0 <= nx < m and 0 <= ny < n and stage[0][ny][nx]:
                field = deepcopy(stage[0])
                field[ny][nx] = False
                ans = max(ans, stage[2] + 1)
                que.append((field, (nx, ny), stage[2] + 1))

    print ans

Fixed submission:
from copy import deepcopy

while True:

    m = int(raw_input())
    n = int(raw_input())

    if m == 0 and n == 0:
        break

    field = [[False] * m for y in xrange(n)]

    for y in xrange(n):
        line = map(int, raw_input().split(' '))
        for x in xrange(m):
            if line[x] == 1:
                field[y][x] = True

    ans = 0

    dx = [1, 0, -1, 0]
    dy = [0, 1, 0, -1]

    def search(x, y, d):
        global ans

        if d > ans:
            ans = d

        for i in xrange(4):
            nx = x + dx[i]
            ny = y + dy[i]

            if 0 <= nx < m and 0 <= ny < n and field[ny][nx]:
                field[ny][nx] = False
                search(nx, ny, d + 1)
                field[ny][nx] = True

    for x in xrange(m):
        for y in xrange(n):
            if field[y][x]:
                field[y][x] = False
                search(x, y, 1)
                field[y][x] = True

    print ans

Buggy python submission (verdict: Time Limit Exceeded):
from copy import deepcopy
from array import array

while True:
    m = int(raw_input())
    n = int(raw_input())

    if m == 0 and n == 0:
        break

    begin = [array('b', [False] * m) for y in xrange(n)]

    for y in xrange(n):
        line = map(int, raw_input().split(' '))
        for x in xrange(m):
            if line[x] == 1:
                begin[y][x] = True

    # (stage, current position(x, y), current point)
    que = []
    for x in xrange(m):
        for y in xrange(n):
            if begin[y][x]:
                stage = deepcopy(begin)
                stage[y][x] = False
                que.append((stage, (x, y), 1))


    dx = [1, 0, -1, 0]
    dy = [0, 1, 0, -1]

    ans = 0

    while que:
        stage = que.pop()
        for i in xrange(4):
            nx = dx[i] + stage[1][0]
            ny = dy[i] + stage[1][1]
            if 0 <= nx < m and 0 <= ny < n and stage[0][ny][nx]:
                field = deepcopy(stage[0])
                field[ny][nx] = False
                ans = max(ans, stage[2] + 1)
                que.append((field, (nx, ny), stage[2] + 1))

    print ans

Fixed submission:
from copy import deepcopy

while True:

    m = int(raw_input())
    n = int(raw_input())

    if m == 0 and n == 0:
        break

    field = [[False] * m for y in xrange(n)]

    for y in xrange(n):
        line = map(int, raw_input().split(' '))
        for x in xrange(m):
            if line[x] == 1:
                field[y][x] = True

    ans = 0

    dx = [1, 0, -1, 0]
    dy = [0, 1, 0, -1]

    def search(x, y, d):
        global ans

        if d > ans:
            ans = d

        for i in xrange(4):
            nx = x + dx[i]
            ny = y + dy[i]

            if 0 <= nx < m and 0 <= ny < n and field[ny][nx]:
                field[ny][nx] = False
                search(nx, ny, d + 1)
                field[ny][nx] = True

    for x in xrange(m):
        for y in xrange(n):
            if field[y][x]:
                field[y][x] = False
                search(x, y, 1)
                field[y][x] = True

    print ans