Go: ///
// File:  1160.go
// Author: ymiyamoto
//
// Created on Sun Mar  4 20:45:57 2018
//
package main

import (
	"fmt"
)

var w, h int
var graph [][]string

func dfs(visited [][]bool, x, y int) {
	visited[x][y] = true

	for i := -1; i <= 1; i++ {
		for j := -1; j <= 1; j++ {
			dx := x + i
			dy := y + j
			if 0 <= dx && dx < h && 0 <= dy && dy < w && graph[dx][dy] == "1" && !visited[dx][dy] {
				dfs(visited, dx, dy)
			}
		}
	}
}

func main() {
	for {
		fmt.Scan(&w, &h)
		if w == 0 && h == 0 {
			break
		}

		graph = make([][]string, h)
		for i := range graph {
			graph[i] = make([]string, w)
		}

		for i := 0; i < h; i++ {
			for j := 0; j < w; j++ {
				var s string
				fmt.Scan(&s)
				graph[i][j] = s
			}
		}

		visited := make([][]bool, h)
		for i := range visited {
			visited[i] = make([]bool, w)
		}

		count := 0
		for i := range graph {
			for j := range graph[i] {
				if graph[i][j] == "1" && !visited[i][j] {
					dfs(visited, i, j)
					count++
				}
			}
		}

		fmt.Println(count)
	}
}

C++: #include<iostream>
#include<fstream>

using namespace std;

	ifstream fin;
	ofstream fout;

	//http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1160&lang=jp

	int gomi = 0;

	int a[52][52] = {};
	int check(int, int);

	int main(){

		fin.open("input.txt");
		fout.open("output.txt");

		//		int w = 0, h = 0;
		int width = 0, height = 0;
		int count = 0;


		while (1){
			for (int i = 0; i < 50; i++)
				for (int j = 0; j < 50; j++)
					a[i + 1][j + 1] = 0;

			fin >> width >> height;
			if (width == 0 && height == 0)break;

			for (int i = 0; i < height; i++){
				for (int j = 0; j < width; j++){
					//初期化
					gomi = a[i + 1][j + 1] = 0;

					fin >> a[i + 1][j + 1];
				}
			}

			for (int i = 0; i < height; i++){
				for (int j = 0; j < width; j++){
					if (a[i + 1][j + 1] == 1){
						check(i + 1, j + 1);
//						a[i + 1][j + 1] = 1;
						count++;
//						cout << i + 1 << "  " << j + 1 << endl;
					}
				}
			}
			cout << count << endl;
			fout << count << endl;

			//初期化
			count = 0;
			for (int i = 0; i < height; i++){
				for (int j = 0; j < width; j++){
					a[i + 1][j + 1] = 0;
				}
			}
		}
	}

	int check(int y, int x){
		for (int i = -1; i <= 1; i++){
			for (int j = -1; j <= 1; j++){
				if (a[y + i][x + j] == 1){
					if (i + j != 0 || i - j != 0){
						a[y + i][x + j] = 0;
						check(y + i, x + j);
					}
				}
			}
		}
		return 0;
	}

/*	int check(int y, int x){
		if (a[y + 1][x] == 1){
			a[y + 1][x] = 0;
			check(y + 1, x);
		}
		//-----------------------------
		if (a[y][x + 1] == 1){
			a[y][x + 1] = 0;
			check(y, x + 1);
		}
		//-----------------------------
		if (a[y - 1][x] == 1){
			a[y - 1][x] = 0;
			check(y - 1, x);
		}
		//-----------------------------
		if (a[y][x - 1] == 1){
			a[y][x - 1] = 0;
			check(y, x - 1);
		}
		//-----------------------------
		if (a[y + 1][x + 1] == 1){
			a[y + 1][x + 1] = 0;
			check(y + 1, x + 1);
		}
		//-----------------------------
		if (a[y + 1][x - 1] == 1){
			a[y + 1][x - 1] = 0;
			check(y + 1, x - 1);
		}
		//-----------------------------
		if (a[y - 1][x + 1] == 1){
			a[y - 1][x + 1] = 0;
			check(y - 1, x + 1);
		}
		//-----------------------------
		if (a[y - 1][x - 1] == 1){
			a[y - 1][x - 1] = 0;
			check(y - 1, x - 1);
		}
		//-----------------------------
		return 0;
	}*/

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

int w,h;
bool walked[50][50];
int map[50][50];

// top t-r right r-d down l-d left t-l
const int dy[8] = {-1, -1, 0, 1, 1, 1, 0, -1};
const int dx[8] = {0, 1, 1, 1, 0, -1, -1, -1};

void dfs(int x, int y) {
  walked[y][x] = true;
  for (int i = 0; i < 8; i += 1) {
    int tx = x + dx[i];
    int ty = y + dy[i];
    if (tx < 0 || tx >= w) continue;
    if (ty < 0 || ty >= h) continue;
    if (map[ty][tx]==1 && !walked[ty][tx])
    dfs(tx, ty);
  }
}

int main(int argc, char const* argv[]) {
  while (true) {
    cin>>w>>h;
    if (w == 0 && h == 0) break;
    int res = 0;
    memset(walked, false, sizeof walked);
    memset(map, 0, sizeof map);
    for (int i = 0; i < h; i += 1) {
      for (int j = 0; j < w; j += 1) {
        cin>>map[i][j];
      }
    }    
    for (int i = 0; i < h; i += 1) {
      for (int j = 0; j < w; j += 1) {
        if (map[i][j]==1 && !walked[i][j]) {
          dfs(j,i);
          res++;
        }
      }
    }
    cout<<res<<endl;
  }
  return 0;
}

C++: #include <cstdio>
#define f(i,n)for(int i=0;i<n;i++)
#define s (scanf("%d",&e),e)
int main(){int a=0,w,n,m,b,e,d[99];f(i,99)d[i]=0;n=s;m=s;if(n==0)return 0;f(i,m){w=b=0;f(j,n){s;if(e)f(k,3){e=d[j+k];if(e&&e-w){if(w){f(l,n+2)d[l]=d[l]==e?w:d[l];a--;}else w=e;}}if(w==0)a++,w=i*n+j+1;}else w=0;d[j]=b;b=w;}d[n]=b;w=0;}printf("%d\n",a);main();}

C++: #include<stdio.h>
void printmap();
void draw(int x,int y);
int w,h,c[50][50];

int main(){
	int i,j;
	int count;


	scanf("%d %d",&w,&h);


	while((w!=0)&&(h!=0)){
		count=0;
	
		for(j=0;j<h;j++){
			for(i=0;i<w;i++){
				scanf("%d",&c[j][i]);
			}
		}
		printf("\n");
		//printmap();
	
		for(i=0;i<w;i++){
			c[h][i]=0;
		}
		for(i=0;i<h;i++){
			c[i][w]=0;
		}

		for(i=0;i<w;i++){
			for(j=0;j<h;j++){
				if(c[j][i]==1){
					count++;
					//printf("count up count=%d\n",count);
					draw(j,i);
				}
			}
		}

		printf("%d\n",count);
		scanf("%d %d",&w,&h);
	}
	return 0;
}

void draw(int x,int y){
	//printf("delete %d %d\n",x,y);
	c[x][y]=0;
	if(c[x+1][y]==1){
		draw(x+1,y);
	}else if(c[x-1][y]==1){
		draw(x-1,y);
	}else if(c[x][y-1]==1){
		draw(x,y-1);
	}else if(c[x][y+1]==1){
		draw(x,y+1);
	}

}

void printmap(){
	int i,j;
	for(j=0;j<h;j++){
		for(i=0;i<w;i++){
			printf("%d ",c[j][i]);
		}
		printf("\n");
	}
}