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<bits/stdc++.h>
using namespace std;
#define s second
#define f first
int dx[]={1,0,1,-1};
int dy[]={0,1,1,1};
int main(){
  int n;
  while(cin>>n,n){
    string s[n];
    for(int i=0;i<n;i++)cin>>s[i];
    pair<pair<int,int>,pair<int,int> > a[n][n];
    for(int i=0;i<n;i++)
      for(int j=0;j<n;j++)
	a[i][j].f.f=a[i][j].f.s=a[i][j].s.f=a[i][j].s.s=s[i][j]-'0';    
    for(int i=0;i<n;i++)
      for(int j=0;j<n;j++)
	for(int k=0;k<4;k++){
	  int x=j+dx[k],y=i+dy[k];
	  if(x>=0&&y>=0&&y<n&&x<n){
	    if(k==0)a[y][x].f.f+=a[i][j].f.f;
	    if(k==1)a[y][x].f.s+=a[i][j].f.s;
	    if(k==2)a[y][x].s.f+=a[i][j].s.f;
	    if(k==3)a[y][x].s.s+=a[i][j].s.s;
	  }
	}
    int res=0;
    for(int i=0;i<n;i++)
      for(int j=0;j<n;j++){
	res=max(res,a[i][j].f.f);
	res=max(res,a[i][j].f.s);
	res=max(res,a[i][j].s.f);
	res=max(res,a[i][j].s.s);
      }
    cout<<res<<endl;
  }
}

C++: #include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;

void _memset();
void _cin();
void check1();
int _check();

#define MAX_N 8000

int n;
char x[MAX_N][MAX_N];
string tate[MAX_N], yoko[MAX_N], naname1[MAX_N * 10], naname2[MAX_N * 10];

void _memset() {
	memset(x, '0', sizeof(x));
	for (int i = 0; i < MAX_N; i++) {
		tate[i] = "";
	}
	for (int i = 0; i < MAX_N; i++) {
		yoko[i] = "";
	}
	for (int i = 0; i < MAX_N * 10; i++) {
		naname1[i] = "";
	}
	for (int i = 0; i < MAX_N * 10; i++) {
		naname2[i] = "";
	}
}

void _cin(int p) {
	for (int i = 0; i < p; i++) {
		for (int j = 0; j < p; j++) {
			cin >> x[i][j];
		}
	}
}

void check1(int p) {
	for (int i = 0; i < p; i++) {
		for (int j = 0; j < p; j++) {
			tate[i] += x[i][j];
			yoko[j] += x[i][j];
			naname1[i + j] += x[i][j];
			naname2[p + i - j] += x[i][j];
		}
	}
}

int maxn;

int _check(int p) {
	maxn = 0;

	//tate.
	int chain = 0;
	for (int i = 0; i < p; i++) {
		chain = 0;
		for (int j = 0; j < tate[i].size(); j++) {
			if (tate[i][j] == '1') {
				chain++;
			}
			else {
				maxn = max(chain, maxn);
				chain = 0;
			}
		}
	}

	maxn = max(chain, maxn);

	//yoko.
	for (int i = 0; i < p; i++) {
		chain = 0;
		for (int j = 0; j < yoko[i].size(); j++) {
			if (yoko[i][j] == '1') {
				chain++;
			}
			else {
				maxn = max(chain, maxn);
				chain = 0;
			}
		}
	}

	maxn = max(chain, maxn);

	//naname1.
	for (int i = 0; i < p * 10; i++) {
		chain = 0;
		for (int j = 0; j < naname1[i].size(); j++) {
			if (naname1[i][j] == '1') {
				chain++;
			}
			else {
				maxn = max(chain, maxn);
				chain = 0;
			}
		}
	}

	maxn = max(maxn, chain);

	//naname2.
	for (int i = 0; i < p * 10; i++) {
		chain = 0;
		for (int j = 0; j < naname2[i].size(); j++) {
			if (naname2[i][j] == '1') {
				chain++;
			}
			else {
				maxn = max(chain, maxn);
				chain = 0;
			}
		}
	}

	maxn = max(chain, maxn);

	_memset();

	return maxn;
}

int main() {
	while (true) {
		cin >> n;
		if (n == 0) {
			break;
		}
		_cin(n);
		check1(n);
		cout << _check(n) << endl;
	}
	return 0;
}

C++: #include <iostream>
#include <string>
#include <algorithm>

using namespace std;

string s[300];
int dx[8] = {0,0,1,-1,-1,-1,1,1};
int dy[8] = {1,-1,0,0,-1,1,-1,1}; 

int dfs( int i , int j , int n ){

  int cnt = 0;
  for( int k = 0 ; k < 8 ; k++ ){
    int cnt2 = 1;
    int x = i;
    int y = j;
    while( s[x][y] == '1' && 0 < x && x < n && 0 < y && y < n ){
      x += dx[k];
      y += dy[k];
      cnt2++;
    }
    cnt = max( cnt , cnt2 ); 
    //８方向動かして、カウント
  }
  return cnt;
}

int main(void){
  
  while(1){

    int n;
    cin >> n;
    if( n == 0 ) break;
    
    for( int i = 0 ; i < n ; i++ ){
      cin >> s[i];
    }
    
    int ans = 0;
    for( int i = 0 ; i < n ; i++ ){
      for( int j = 0 ; j < n ; j++ ){
	if( s[i][j] == '1' ){
	  ans = max( ans , dfs(i,j,n) );
	}
      }
    }
    cout << ans << endl;
  }
  return 0;
}

C++: #include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
char f[300][300];
bool used[300][300];
int n;
int ans;
int dx[4]={1,0,1,-1};
int dy[4]={0,1,-1,1};
void dfs(int x,int y,int d,int k)
{
	if(x<0||x>=n||y<0||y>=n)return;
	//if(used[x][y])return;
	if(f[x][y]=='0')return;
	ans=max(ans,d);
	dfs(x+dx[k],y+dy[k],d+1,k);
	return;
}
void solve(int k)
{
	memset(used,false,sizeof(used));
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<n;j++)
		{
			dfs(i,j,1,k);
		}
	}
	return ;
}
int main()
{
	while(1)
	{
		ans=0;
		cin >> n;
		if(n==0)break;
		for(int i=0;i<n;i++)
		{
			for(int j=0;j<n;j++)
			{
				cin >> f[i][j];
			}
		}
		for(int i=0;i<4;i++)solve(i);
		cout << ans << endl;
	}
	return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
int n;
bool data[255][255];    
int dp[4][255][255];

int check(int mode,int i,int j)
{
  if(dp[mode][i][j]!=-1)
    return dp[mode][i][j];
  if(!data[i][j])
    return 0;
  if(i<0 || i>=n ||
     j<0 || j>=n)
    return 0;
  if(mode==1)
    dp[mode][i][j]=check(1,i+1,j)+1;
  else if(mode==2)
    dp[mode][i][j]=check(2,i+1,j-1)+1;
  else if(mode==3)
    dp[mode][i][j]=check(3,i+1,j+1)+1;
  
  return dp[mode][i][j];
}

int main()
{

  for(;cin>>n,n;)
    {

      memset(dp,-1,sizeof(dp));
      //<->0, up-down,1,hidarisita2,migisita3
      int ans=0;
      for(int i=0;i<n;i++)
	for(int j=0;j<n;j++)
	  {
	    char tmp;
	    cin>>tmp;
	    data[i][j]=(tmp=='1');
	    if(data[i][j])
	      {
		if(j==0)
		  dp[0][i][j]=1;
		else
		  dp[0][i][j]=dp[0][i][j-1]+1;
	      }
	    else
	      dp[0][i][j]=0;
	    ans=max(ans,dp[0][i][j]);	    
	  }
      for(int i=0;i<n;i++)
	for(int j=0;j<n;j++)
	  {
	    ans=max(ans,check(1,i,j));
	    if(i<=n/2)
	      {
		if(j>=n/2)
		  ans=max(ans,check(2,i,j));
		if(j<=n/2)
		  ans=max(ans,check(3,i,j));
	      }
	  }
      cout<<ans<<endl;
    }
}