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>
using namespace std;

#define SIZE 257

int table[256][256];

enum {
	RIGHT_DOWN = 0,
	DOWN,
	LEFT_DOWN
};

int current_counts[SIZE][3]; //1ツ篠淞：ツ暗環置ツ、2ツ篠淞：ツ古シツつォ
int next_counts[SIZE][3];
int max_count = 0;

void init_counts() {
	for (int i = 0; i < SIZE; ++i) {
		for (int j = 0; j < 3; ++j) {
			current_counts[i][j] = next_counts[i][j] = 0;
		}
	}
}

void check_max(int expect) {
	if(expect > max_count) {
		max_count = expect;
	}
}

int main() {
	while (true) {
		int n;
		cin >> n;
		if(n == 0) {
			break;
		}
		for (int i = 0; i < n; ++i) {
			for (int j = 0; j < n; ++j) {
				char o;

				cin >> o;
				table[i][j] = o - '0';
			}
		}

		init_counts();
		max_count = 0;
		int cont_vertical = 0;

		for (int i = 0; i < n; ++i) {
			for (int j = 0; j < n; ++j) {
				if(table[i][j] == 1) {
					cont_vertical++;

					next_counts[j][DOWN] = current_counts[j][DOWN] + 1;
					if(j+1 < n) {
						next_counts[j+1][RIGHT_DOWN] = current_counts[j][RIGHT_DOWN] + 1;
					}
					else {
						check_max(current_counts[j][RIGHT_DOWN] + 1);
					}
					if(j > 0) {
						next_counts[j-1][LEFT_DOWN] = current_counts[j][LEFT_DOWN] + 1;
					}
					else {
						check_max(current_counts[j][LEFT_DOWN] + 1);
					}
				}
				else {
					check_max(cont_vertical);
					cont_vertical = 0;
					for (int k = 0; k < 3; ++k) {
						check_max(current_counts[j][k]);
					}
				}
			}
			check_max(cont_vertical);
			cont_vertical = 0;
			for (int j = 0; j < n; ++j) {
				for (int k = 0; k < 3; ++k) {
					current_counts[j][k] = next_counts[j][k];
					next_counts[j][k] = 0;
				}
			}
		}
		for (int i = 0; i < n; ++i) {
			for (int j = 0; j < 3; ++j) {
				check_max(current_counts[i][j]);
			}
		}

		cout << max_count << endl;
	}
	return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
int underR(int y,int x);
int underL(int y,int x);
char maps[260][260];
int check[260][260];
int n;
int gyomax[260]={0};
int gyoans=0;
int gyonum[260]={0};
int retumax[260]={0};
int retuans=0;
int retunum[260]={0};
int ans=0;
main(){
  while(1){
    ans=0;
    memset(check,0,sizeof(check));
    memset(gyomax,0,sizeof(gyomax));
    memset(retumax,0,sizeof(retumax));
    gyoans=0;
    retuans=0;
    memset(gyonum,0,sizeof(gyonum));
    memset(retunum,0,sizeof(retunum));
    cin>>n;
    if(n==0)break;
    for(int i=0;i<n;i++){
      for(int j=0;j<n;j++){
	cin>>maps[i][j];
	if(maps[i][j]=='1'){
	  gyonum[i]++;
	  gyomax[i]=max(gyomax[i],gyonum[i]);
	  retunum[j]++;
	  retumax[j]=max(retumax[j],retunum[j]);
	}
	else{
	  gyonum[i]=0;
	  retunum[j]=0;
	}
      }
    }
    gyoans=*(max_element(gyomax,gyomax+n));
    retuans=*(max_element(retumax,retumax+n));
    //cout<<gyoans<<" "<<retuans<<endl;
    ans=max(gyoans,retuans);
    for(int i=0;i<n;i++){
      for(int j=0;j<n;j++){
	if(maps[i][j]=='1'&&check[i][j]==0){
	  check[i][j]=1;
	  ans=max(ans,underR(i,j));
	  ans=max(ans,underL(i,j));
	}
      }
    }
    cout<<ans<<endl;
  }
}
int underR(int y,int x){
  int num=0;
  if(maps[y][x]=='0'){
    return 0;
  }
  else{
    num++;
    if(y+1<n&&x+1<n){
      num+=underR(y+1,x+1);
    }
    return num;
  }
}
int underL(int y,int x){
  int num=0;
  if(maps[y][x]=='0'){
    return 0;
  }
  else{
    num++;
    if(y+1<n&&x-1>=0){
      num+=underR(y+1,x-1);
    }
    return num;
  }
}

C++: #include <cstdio>
#include <vector>
#include <algorithm>
#include <string>

int testcase_ends() {
  size_t n;
  scanf("%zu", &n);
  if (n == 0) return 1;

  std::vector<std::string> s(n);
  for (size_t i=0; i<n; ++i) {
    char buf[256];
    scanf("%s", buf);
    s[i] = buf;
  }

  size_t res=0;
  for (size_t i=0; i<n; ++i) {
    size_t cur=0;
    for (size_t j=0; j<n; ++j) {
      if (s[i][j] == '1') {
        ++cur;
      } else {
        res = std::max(res, cur);
        cur = 0;
      }
    }
    res = std::max(res, cur);
  }

  for (size_t i=0; i<n; ++i) {
    size_t cur=0;
    for (size_t j=0; j<n; ++j) {
      if (s[j][i] == '1') {
        ++cur;
      } else {
        res = std::max(res, cur);
        cur = 0;
      }
    }
    res = std::max(res, cur);
  }

  auto diag=[](const std::vector<std::string> &s)->size_t {
    size_t res=0, n=s.size();
    for (size_t i=0; i<n; ++i) {
      size_t cur=0;
      for (size_t j=0; j<=i; ++j) {
        if (s[j][i-j] == '1') {
          ++cur;
        } else {
          res = std::max(res, cur);
          cur = 0;
        }
      }

      res = std::max(res, cur);
    }
    return res;
  };
  std::vector<std::string> t=s;
  res = std::max(res, diag(s));

  std::reverse(s.begin(), s.end());
  res = std::max(res, diag(s));

  for (auto &si: s) std::reverse(si.begin(), si.end());
  res = std::max(res, diag(s));

  std::reverse(s.begin(), s.end());
  res = std::max(res, diag(s));

  printf("%zu\n", res);
  return 0;
}

int main() {
  while (!testcase_ends()) {}
}

C++: #include <iostream>
#include <algorithm>
using namespace std;
int dx[] = {-1,-1,-1,0,0,1,1,1},dy[] = {-1,0,1,-1,1,-1,0,1};
int ans;
int n;
char ma[300][300];

void cheak(int px,int dx,int py,int dy){
  int cnt = 1;

  while(1){
    px+=dx;
    py+=dy;
    if(!(0<=px && px < n && 0 <= py && py < n) || ma[py][px] != '1')break;
    cnt++;
  }
  ans = max(ans,cnt);
}

int main(){
  while( cin >> n,n){

    for(int i = 0 ; i < n ; i++ ){
      for(int j = 0 ;  j < n ; j++){
	cin >> ma[i][j];
      }
    }

    ans = 0;

    for(int i = 0 ; i < n ; i++ ){
      for(int j = 0 ;  j < n ; j++){

	if(ma[i][j] == '1'){
	  for(int l = 0 ; l < 8; l++){
	      cheak(j,dx[l],i,dy[l]);
	  }
	}

      }
    }
    cout << ans << endl;
  }
}