Problem:


Grid

There is a n &times; 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



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


Sample output:
4
8
1
0

Submission status: Accepted

Solution code:
while True:
    n=input()
    if n==0:break
    L=[map(int,list(raw_input())) for i in range(n)]
    Q=[[] for i in range(2*n-1)]
    for i in range(n):
        for j in range(n):
            Q[i+j].append((i,j))

    P=[[] for i in range(2*n-1)]
    for i in range(n):
        for j in range(n-1,-1,-1):
            P[(n-1)-i+j].append((i,j))

    maxInt=0
    for i in range(n):
        cnt=0
        for j in range(n):
            if L[i][j]==1:
                cnt+=1
            else:
                cnt=0
            maxInt=max(cnt,maxInt)
    for i in range(n):
        cnt=0
        for j in range(n):
            if L[j][i]==1:
                cnt+=1
            else:
                cnt=0
            maxInt=max(cnt,maxInt)
    for p in P:
        cnt=0
        for i,j in p:
            if L[i][j]==1:
                cnt+=1
            else:
                cnt=0
            maxInt=max(cnt,maxInt)
    for q in Q:
        cnt=0
        for i,j in q:
            if L[i][j]==1:
                cnt+=1
            else:
                cnt=0
            maxInt=max(cnt,maxInt)
    print maxInt

Submission status: Accepted

Solution code:
def ge(n):
    for i in xrange(n):
        yield [(i,j) for j in xrange(n)]
        yield [(j,i) for j in xrange(n)]
        yield [(j,i-j) for j in xrange(i+1)]
        yield [(n-1-i+j,n-1-j) for j in xrange(i+1)]
        yield [(j,n-1-i+j) for j in xrange(i+1)]
        yield [(n-1-i+j,j) for j in xrange(i+1)]

def f(L):
    ret,a = 0,0
    for i in L:
        if i == 0:
            ret = max(ret,a)
            a = 0
        else:
            a += 1
    return max(ret,a)

while True:
    n = input()
    if n == 0:
        break
    A = [map(int,list(raw_input())) for _ in xrange(n)]
    print max(map(f,[map(lambda (i,j): A[i][j], p) for p in ge(n)]))

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!

while True:
    n = int(input())
    if n == 0:
        break
    
    table = [[0 for i in range(n)] for j in range(n)]
    for l in range(n):
        line = input()
        for m in range(n):
            table[l][m] = int(line[m])

    t = 0
    ans = 0
    for i in range(n):
        for j in range(n):
            if table[i][j] > 0:
                t += 1
            else:
                ans = max(t, ans)
                t = 0
        ans = max(t, ans)
        t = 0

    for i in range(n):
        for j in range(n):
            if table[j][i] > 0:
                t += 1
            else:
                ans = max(t, ans)
                t = 0
        ans = max(t, ans)
        t = 0

    for c in range(n):
        for j in range(c+1):
            if table[c-j][j] > 0:
                t += 1
            else:
                ans = max(t, ans)
                t = 0
        ans = max(t, ans)
        t = 0
        

    for c in range(n):
        for j in range(c+1):
            if table[n-1-(c-j)][n-1-j] > 0:
                t += 1
            else:
                ans = max(t, ans)
                t = 0
        ans = max(t, ans)
        t = 0


    for c in range(n):
        for j in range(c+1):
            if table[n-1-(c-j)][j] > 0:
                t += 1
            else:
                ans = max(t, ans)
                t = 0
        ans = max(t, ans)
        t = 0


    for c in range(n):
        for j in range(c+1):
            if table[c-j][n-1-j] > 0:
                t += 1
            else:
                ans = max(t, ans)
                t = 0
        ans = max(t, ans)
        t = 0

        
    print(ans)

Submission status: Runtime Error

Solution code:
def f():
    max_c = 0
    c = 0
    for h in range(n):
        for w in range(n):
            if grid[h][w] == 1:
                    c += 1
            else:
                if  max_c < c:
                    max_c = c
                c = 0
        else:
            if  max_c < c:
                max_c = c
            c = 0
    
    for w in range(n):
        for h in range(n):
            if grid[h][w] == 1:
                c += 1
            else:
                if  max_c < c:
                    max_c = c
                c = 0
        else:
            if  max_c < c:
                max_c = c
            c = 0
    
    for p in range(n):
        for a in range(n-p):
            if grid[p+a][0+a] == 1:
                c += 1
            else:
                if  max_c < c:
                    max_c = c
                c = 0
        else:
            if  max_c < c:
                max_c = c
            c = 0
        
    for p in range(1,n):
        for a in range(n-p):
            if grid[0+a][p+a] == 1:
                c += 1
            else:
                if  max_c < c:
                    max_c = c
                c = 0
        else:
            if  max_c < c:
                max_c = c
                
    for p in range(n):
        for a in range(n-p):
            if grid[p+a][n-1-a] == 1:
                c += 1
            else:
                if  max_c < c:
                    max_c = c
                c = 0
        else:
            if  max_c < c:
                max_c = c
            c = 0
            
    for p in range(n, -1, -1):
        for a in range(p):
            if grid[0+a][p-a] == 1:
                c += 1
            else:
                if  max_c < c:
                    max_c = c
                c = 0
        else:
            if  max_c < c:
                max_c = c
            c = 0
                
    print max_c
        
while True:
    n = input()
    if n == 0: break
    grid = [map(int, raw_input()) for i in range(n)]
    f()