Problem:
Grid

There is a n × n grid D where each cell contains either 1 or 0.

Your task is to create a program that takes the gird data as input and computes the greatest number of consecutive 1s in either vertical, horizontal, or diagonal direction.

For example, the consecutive 1s with greatest number in the figure below is circled by the dashed-line.

The size of the grid n is an integer where 2 ≤ n ≤ 255.

Input

The input is a sequence of datasets. The end of the input is indicated by a line containing one zero. Each dataset is formatted as follows:

n
D11 D12 ... D1n
D21 D22 ... D2n
.
.
Dn1 Dn2 ... Dnn

Output

For each dataset, print the greatest number of consecutive 1s.

Sample Input

5
00011
00101
01000
10101
00010
8
11000001
10110111
01100111
01111010
11111111
01011010
10100010
10000001
2
01
00
3
000
000
000
0

Output for the Sample Input

4
8
1
0

Solution in Ruby (judge verdict: Accepted)

def get_max_length(grid, n)
  max_length = 0

  for y in 0...n
    length = 0
    for x in 0...n
      if grid[y][x] == 1
        length += 1
        max_length = length if length > max_length
      else
        length = 0
      end
    end
  end

  for x in 0...n
    length = 0
    for y in 0...n
      if grid[y][x] == 1
        length += 1
        max_length = length if length > max_length
      else
        length = 0
      end
    end
  end

  for i in 0..(n * 2 - 2)
    length = 0
    x = 0
    y = i - x
    while x < n && y >= 0
      if y < n && grid[y][x] == 1
        length += 1
        max_length = length if length > max_length
      else
        length = 0
      end
      x += 1
      y -= 1
    end
  end

#  for i in 0..(n * 2 - 2)
#    length = 0
#    x = 0
#    y = n - 1 - i - x
#    while x < n && y >= 0
#      if y < n && grid[y][x] == 1
#        length += 1
#        max_length = length if length > max_length
#      else
#        length = 0
#      end
#      y += 1
#      x += 1
#    end
#  end
  for i in -(n - 1)..(n - 1)
    length = 0
    x = 0
    y = i + x
    while x < n && y < n
      if y >= 0 && grid[y][x] == 1
        length += 1
        max_length = length if length > max_length
      else
        length = 0
      end
      x += 1
      y += 1
    end
  end

  max_length
end

while line = gets
  n = line.chomp.to_i
  break if n == 0

  grid = []
  n.times do
    grid << gets.chomp.split('').map(&:to_i)
  end

  puts get_max_length(grid, n)
end