Go: package main

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

func getScanner(fp *os.File) *bufio.Scanner {
	scanner := bufio.NewScanner(fp)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 1000005), 1000005)
	return scanner
}

func getNextString(scanner *bufio.Scanner) string {
	scanner.Scan()
	return scanner.Text()
}

func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}

func getNextInt64(scanner *bufio.Scanner) int64 {
	i, _ := strconv.ParseInt(getNextString(scanner), 10, 64)
	return i
}

func getNextUint64(scanner *bufio.Scanner) uint64 {
	i, _ := strconv.ParseUint(getNextString(scanner), 10, 64)
	return i
}

func getNextFloat64(scanner *bufio.Scanner) float64 {
	i, _ := strconv.ParseFloat(getNextString(scanner), 64)
	return i
}

func main() {
	fp := os.Stdin
	wfp := os.Stdout
	cnt := 0

	if os.Getenv("MASPY") == "ますピ" {
		fp, _ = os.Open(os.Getenv("BEET_THE_HARMONY_OF_PERFECT"))
		cnt = 0
	}
	if os.Getenv("MASPYPY") == "ますピッピ" {
		wfp, _ = os.Create(os.Getenv("NGTKANA_IS_GENIUS10"))
	}

	scanner := getScanner(fp)
	writer := bufio.NewWriter(wfp)
	for solve(scanner, writer) {

	}
	for i := 0; i < cnt; i++ {
		fmt.Fprintln(writer, "-----------------------------------")
		solve(scanner, writer)
	}
	writer.Flush()
}

func solve(scanner *bufio.Scanner, writer *bufio.Writer) bool {
	sol := solver{}
	sol.w = getNextInt(scanner)
	sol.h = getNextInt(scanner)
	if sol.w == 0 && sol.h == 0 {
		return false
	}
	sol.land = makeInts(sol.h, sol.w)
	for y := 0; y < sol.h; y++ {
		for x := 0; x < sol.w; x++ {
			sol.land[y][x] = getNextInt(scanner)
		}
	}
	ans := 0
	for y := 0; y < sol.h; y++ {
		for x := 0; x < sol.w; x++ {
			if sol.land[y][x] == 0 {
				continue
			}
			ans++
			sol.dfs(y, x)
		}
	}
	fmt.Fprintln(writer, ans)
	return true
}

type solver struct {
	w, h int
	land [][]int
}

func (sol *solver) dfs(y, x int) {
	if y < 0 {
		return
	}
	if y >= sol.h {
		return
	}
	if x < 0 {
		return
	}
	if x >= sol.w {
		return
	}
	if sol.land[y][x] == 0 {
		return
	}
	sol.land[y][x] = 0
	dy := [8]int{-1, -1, -1, 0, 0, 1, 1, 1}
	dx := [8]int{-1, 0, 1, -1, 1, -1, 0, 1}
	for i := 0; i < 8; i++ {
		sol.dfs(y+dy[i], x+dx[i])
	}
}
func makeInts(h, w int) [][]int {
	index := make([][]int, h, h)
	data := make([]int, h*w, h*w)
	for i := 0; i < h; i++ {
		index[i] = data[i*w : (i+1)*w]
	}
	return index
}

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

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

		vector<vector<int>>list(n, vector<int>(n, -1));
		int number;

		for (int i = 0;i < n;i++) {
			cin >> number;
			for (int j = 0;j < number;j++) {
				int codename;
				cin >> codename;
				list[i][codename - 1] = 1;
			}
		}//list[a][b]:a know b

		cin >> number;
		
		vector<int>ans(n,1);
		int leaked;
		for (int i = 0;i < number;i++) {
			cin >> leaked;
			for (int j = 0;j < n;j++) {
				if (list[j][leaked - 1] <= 0) ans[j] = 0;
			}
		}//who is imposible
		int sum = 0,telltale;

		for (int i = 0;i < n;i++) {
			sum += ans[i];
			if (ans[i] == 1) telltale = i + 1;
		}

		if (sum == 1)cout << telltale << endl;
		else cout << -1 << endl;

	}
	return 0;
}

C++: #include<iostream>
#define N 51
using namespace std;

int w, h, ans = 0, map[N][N];

void search_zahyou(int w, int h);

int main(){
	while(cin >>w >>h){
		ans = 0;
		if(w == 0 && h == 0) break;
		for(int i = 0; i < h; i++){
			for(int k = 0; k < w; k++){
				cin >>map[i][k];
			}
		}
		for(int i = 0; i < h; i++){
			for(int k = 0; k < w; k++){
				if(map[i][k] == 0){
					continue;
				}
				else{
					ans++;
					search_zahyou(i, k);
				}
			}
		}
		cout <<ans <<endl;
	}
	return 0;
}

void search_zahyou(int inh, int inw){
	int searchh[8] = {-1, -1, -1, 0, 1, 1, 1, 0}, searchw[8] = {-1, 0, 1, 1, 1, 0, -1, -1};
	if(h <= inh || w <= inw || h < 0 || w < 0){
	} else{
		map[inh][inw] = 0;
		for(int i = 0; i < 8; i++){
			if(map[inh + searchh[i]][inw + searchw[i]] == 1){
				search_zahyou(inh + searchh[i], inw + searchw[i]);
			}
		}
	}
}

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

int map[100][100];
int w=0,h=0;

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

void dfs(int x,int y);

int main(){
  while(1){
    cin>>w>>h;

    if(w==0&&h==0)
      break;

    for(int i=0;i<h;i++){
      for(int j=0;j<w;j++){
        cin>>map[i][j];
      }
    }

    int result=0;

    for(int i=0;i<h;i++){
      for(int j=0;j<w;j++){
	if(map[i][j]==1){
	  result++;
	  dfs(j,i);
	}
      }
    }
    cout<<result<<endl;
  }
    
  return 0;
}

void dfs(int x,int y){
  map[y][x]=0;
  for(int i=0;i<8;i++){
    int nx=x+dx[i];
    int ny=y+dy[i];
    if(nx>=0&&nx<w&&ny>=0&&ny<h&&map[ny][nx]==1){
      dfs(nx,ny);
    }
  }
}

C++: #include <stdio.h>
int map[100][100],n,m;
bool went[100][100];
void walk(int x,int y){
    if(x<0||y<0||x>=n||y>=m)return ;
    if(went[x][y])return ;
    if(map[x][y]==0)return ;
    went[x][y]=true;
    walk(x-1,y);
    walk(x+1,y);
    walk(x,y+1);
    walk(x,y-1);
    walk(x+1,y+1);
    walk(x+1,y-1);
    walk(x-1,y+1);
    walk(x-1,y-1);
    return ;
}
int main(){
    int ans;
    while(1){
        scanf("%d%d",&n,&m);
        if(n==0&&m==0)return 0;
        ans=0;
        for(int i=0;i<n;i++)for(int j=0;j<m;j++){
            scanf("%d",&map[i][j]);
            went[i][j]=false;
        }
        for(int i=0;i<n;i++)for(int j=0;j<m;j++)if(!went[i][j]){
            ans++;
            walk(i,j);
        }
        printf("%d\n",ans);
    }
}

C++: #include <iostream>
#include <queue>
using namespace std;
struct Point{int x,y;};
int main(){
  int w,h,p[51][51]={},v[51][51]={},i,j,k,o;
  queue <Point> q;
  Point n,b;
  cin >> w >> h;
  while(w!=0){
    for(j=0;j<h;j++){
      for(i=0;i<w;i++){
	cin >> p[i][j];
	v[i][j]=0;
      }
      
    }
    o=0;
    for(j=0;j<h;j++){
      for(i=0;i<w;i++){
	if(v[i][j]==0&&p[i][j]==1){
	  o++;
	  n.x=i;n.y=j;
	  q.push(n);
	  while(!q.empty()){
	    n=q.front();
	    q.pop();
	    v[n.x][n.y]=1;
	    if(n.x+1<w&&v[n.x+1][n.y]==0&&p[n.x+1][n.y]==1) {
	      b.x=n.x+1;b.y=n.y;
	      q.push(b);
	    }
	    if(n.y+1<h&&v[n.x][n.y+1]==0&&p[n.x][n.y+1]==1) {
	      b.x=n.x;b.y=n.y+1;
	      q.push(b);
	    }
	    if(n.x-1>=0&&v[n.x-1][n.y]==0&&p[n.x-1][n.y]==1) {
	      b.x=n.x-1;b.y=n.y;
	      q.push(b);
	    }
	    if(n.y-1>=0&&v[n.x][n.y-1]==0&&p[n.x][n.y-1]==1) {
	      b.x=n.x;b.y=n.y-1;
	      q.push(b);
	    }
	    if(n.x+1<w&&n.y+1<h&&v[n.x+1][n.y+1]==0&&p[n.x+1][n.y+1]==1) {
	      b.x=n.x+1;b.y=n.y+1;
	      q.push(b);
	    }
	    if(n.x-1>=0&&n.y-1>=0&&v[n.x-1][n.y-1]==0&&p[n.x-1][n.y-1]==1) {
	      b.x=n.x-1;b.y=n.y-1;
	      q.push(b);
	    }
	    if(n.x+1<w&&n.y-1>=0&&v[n.x+1][n.y-1]==0&&p[n.x+1][n.y-1]==1) {
	      b.x=n.x+1;b.y=n.y-1;
	      q.push(b);
	    }
	    if(n.x-1>=0&&n.y+1<h&&v[n.x-1][n.y+1]==0&&p[n.x-1][n.y+1]==1) {
	      b.x=n.x-1;b.y=n.y+1;
	      q.push(b);
	    }
	  }
	}
      }
    }
    cout << o << endl;
    cin >> w >> h;
  }
  return 0;
}