Go: package main

import (
	"bufio"
	"os"
	"strconv"
	"fmt"
	"strings"
	"sort"
)

var scanner= bufio.NewScanner(os.Stdin)

func main() {
	for scanner.Scan() {
		n, _ := strconv.Atoi(scanner.Text())
		if n == 0 {
			break
		}
		mp := make([]string, n)
		for i := range mp {
			scanner.Scan()
			mp[i] = "0" + scanner.Text() + "0"
		}
		tmp := make([]string, n+2)
		for i := range tmp {
			tmp[i] = "0"
		}
		mp = append([]string{strings.Join(tmp, "")}, mp...)
		mp = append(mp, strings.Join(tmp, ""))
		score := make([][][]int, n+2)
		for i := range score {
			score[i] = make([][]int, n+2)
			for j := range score[i] {
				score[i][j] = make([]int, 4)
			}
		}
		max_score := 0
		for i := 1; i < n+1; i++ {
			for j := 1; j < n+1; j++ {
				tmps := mp[i]
				if string(tmps[j]) == "1" {
					score[i][j][0] = score[i - 1][j][0] + 1
					score[i][j][1] = score[i][j - 1][1] + 1
					score[i][j][2] = score[i - 1][j - 1][2] + 1
					score[i][j][3] = score[i - 1][j + 1][3] + 1
					max_score = max(max_score, score[i][j][0], score[i][j][1], score[i][j][2], score[i][j][3])
				}
			}
		}
		fmt.Println(max_score)
	}
}

func max(a, b, c, d, e int) int {
	buf := []int{a, b, c, d, e}
	sort.Ints(buf)
	return buf[len(buf)-1]
}

C++: #include <iostream>
#include <algorithm>
using namespace std;
const int MAX_N = 300;

int n,cnt;
int wx[]={1,-1},hy[]={1,-1};
int x1[]={1,-1},y1[]={1,-1};
int x2[]={1,-1},y2[]={-1,1};
int in[MAX_N][MAX_N];
int s[MAX_N][MAX_N];

void Wcheck(int x,int y){
    if(0 > x || n <= x) return;
    s[x][y] = 0;
    cnt++;
    
    for(int i = 0 ; i < 2 ; i++){
        int nx = x + wx[i];
        if(0 <= nx && nx < n && s[nx][y] == 1) Wcheck(nx,y);
    }
}

void Hcheck(int x,int y){
    if(0 > y || n <= y) return;
    s[x][y] = 0;
    cnt++;
    
    for(int i = 0 ; i < 2 ; i++){
        int ny = y + hy[i];
        if(0 <= ny && ny < n && s[x][ny] == 1) Hcheck(x,ny);
    }
}

void t1(int x,int y){
    if(0 > x || n <= x || 0 > y || n <= y) return;
    s[x][y] = 0;
    cnt++;
    
    for(int i = 0 ; i < 2 ; i++){
        int nx = x + x1[i];
        int ny = y + y1[i];
        
        if(0 <= nx && nx < n && 0 <= ny && ny < n && s[nx][ny] == 1){
            t1(nx,ny);
        }
    }
}

void t2(int x,int y){
    if(0 > x || n <= x || 0 > y || n <= y) return;
    s[x][y] = 0;
    cnt++;
    
    for(int i = 0 ; i < 2 ; i++){
        int nx = x + x2[i];
        int ny = y + y2[i];
        
        if(0 <= nx && nx < n && 0 <= ny && ny < n && s[nx][ny] == 1){
            t2(nx,ny);
        }
    }
}
void cp_str(){
    for(int i = 0 ; i < n ; i++){
        for(int j = 0 ; j < n ; j++){
            s[i][j] = in[i][j];
        }
    }
}

int main(){
    char ch;
    while(cin >> n ,n){
        int a = 0,b = 0,c = 0,d = 0;
        for(int i = 0 ; i < n ; i++){
            for(int j = 0 ; j < n ; j++){
                cin >> ch;
                in[i][j] = ch - '0';
            }
        }
        
        cp_str();
        for(int i = 0 ; i < n ; i++){
            for(int j = 0 ; j < n ; j++){
                cnt = 0;
                if(s[i][j] == 1) Wcheck(i,j);
                a = max(a,cnt);
            }
        }
        
        cp_str();
        for(int i = 0 ; i < n ; i++){
            for(int j = 0 ; j < n ; j++){
                cnt = 0;
                if(s[i][j] == 1) Hcheck(i,j);
                b = max(b,cnt);
            }
        }

        cp_str();
        for(int i = 0 ; i < n ; i++){
            for(int j = 0 ; j < n ; j++){
                cnt = 0;
                if(s[i][j] == 1) t1(i,j);
                c = max(c,cnt);
            }
        }
        
        cp_str();
        for(int i = 0 ; i < n ; i++){
            for(int j = 0 ; j < n ; j++){
                cnt = 0;
                if(s[i][j] == 1) t2(i,j);
                d = max(d,cnt);
            }
        }
        
        cout << max(max(a,b),max(b,d)) << endl;
    }
    
    return 0;
}

C++: //2013/11/01(金) 動的計画

#include <iostream>
using namespace std;

int main()
{
	int n;
	char X[257][257];
	int count;

	while(true){	

		count=0;

		cin >> n;
		
		if(n==0)
			break;

		for(int j=1; j<=n; j++)
			for(int k=1; k<=n; k++)
				cin >> X[j][k];

		int DP[4][257][257];

		for(int i=0; i<4; i++)
			for(int j=0; j<=n+1; j++)
				for(int k=0; k<=n+1; k++)
					DP[i][j][k] = 0;

		for(int j=1; j<=n; j++){
			for(int k=1; k<=n; k++){
				if(X[j][k] == '1'){
					 DP[0][j][k] = DP[0][j-1][k]+1;
              DP[1][j][k] = DP[1][j][k-1]+1;
              DP[2][j][k] = DP[2][j-1][k-1]+1;
              DP[3][j][k] = DP[3][j-1][k+1]+1;
					
					for(int i=0; i<4; i++)
						if(count < DP[i][j][k])
							count = DP[i][j][k];
				}
			}
		}

		cout << count << endl;
	}	
	return 0;
}

C++: #include <iostream>
#include <iomanip>
#include <vector>
#include <string>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <algorithm>
#include <sstream>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <numeric>
#include <cmath>
#include <climits>
#include <limits>
#include <cfloat>
#include <fstream>
using namespace std;

int main()
{
//	cut here before submit 
//	freopen ("testcase.grid2", "r", stdin );
	string str = "";
	int n;

	while (getline (cin, str ) ) {
		stringstream ssn(str);
		ssn >> n;
		
		if (n == 0 ){
			break;
		} // end if

		vector <vector <int> > grid (n, vector <int> (n, 0 ) );
 
		int i,j,k;

		for (i = 0; i < n; ++i ){
			getline (cin, str );
			for (j = 0; j < n; ++j ){
				grid[i][j] = str[j] - '0';
			} // end for
		} // end for

		int res = 0;
		int sum = 0;
		bool found = false;

		for (i = 0; i < n; ++i ){
			sum = 0;
			found = false;

			for (j = 0; j < n; ++j ){
				if (grid[i][j] == 1 ){
					if (!found ){
						sum = 0;
						found = true;
					} // end if
					++sum;
				}else{
					res = max (res, sum );
					found = false;
					sum = 0;
				} // end if
			} // end for
			res = max (res, sum );
		} // end for

		for (j = 0; j < n; ++j){
			sum = 0;
			found = false;

			for (i = 0; i < n; ++i ){
				if (grid[i][j] == 1){
					if (!found ){
						sum = 0;
						found = true;
					} // end if
					++sum;
				}else{
					res = max (res, sum );
					found = false;
					sum = 0;
				} // end if
			} // end for
			res = max (res, sum );
		} // end for

		for (k = -n + 1; k < n; ++k){
			sum = 0;
			found = false;
			int prev_i = -1000;
			int prev_j = -1000;

			for (i = 0; i < n; ++i ){
				for (j = 0; j < n; ++j ){
					if (i - j == k ){
						if (grid[i][j] == 1 ){
							if (!found ){
								sum = 1;
								found = true;
							}else{
							 	if (i == prev_i + 1 
								 && j == prev_j + 1 ){
									++sum;
								}else{
									sum = 0;
									found = false;
								} // end if
							} // end if
							prev_i = i;
							prev_j = j;
						}else{
							res = max (res, sum );
							sum = 0;
							found = false;
						} // end if
					} // end if
				} // end for
			} // end for
			if (found ){
				res = max (res, sum );
			} // end if
		} // end for

		for (k = 0; k <= 2*(n-1); ++k){
			sum = 0;
			found = false;
			int prev_i = 1000;
			int prev_j = 1000;

			for (i = 0; i < n; ++i){
				for (j = 0; j < n; ++j){	
					if (i + j == k ){
						if (grid[i][j] == 1 ){
							if (!found ){
								sum = 1;
								found = true;
							}else{
							 	if (i == prev_i + 1 
								 && j == prev_j - 1 ){
									++sum;
								}else{
									sum = 0;
									found = false;
								} // end if
							} // end if
							prev_i = i;
							prev_j = j;
						}else{
							res = max (res, sum );
							sum = 0;
							found = false;
						} // end if
					} // end if
				} // end for
			} // end for
			if (found ){
				res = max (res, sum );
			} // end if
		} // end for	

		cout << res << endl;	
	} // end loop

		
	return 0;
}