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 <cmath>
#include<iostream>
#include<vector>
#include<algorithm>
#include<functional>
#include<queue>
#include<set>
#include<tuple>
#include<bitset>
#include<iomanip>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
ll inf = 1000000007;
int dx[8] = { 1,-1,0,0,-1,-1,1,1 };
int dy[8] = { 0,0,1,-1,-1,1,-1,1 };
int dif[55][55];
char c[55][55];
int h, w;
void dfs(P p) {
	queue<P>que;
	que.push(p);
	while (!que.empty()) {
		int nowy = que.front().first;
		int nowx = que.front().second;
		que.pop();
		for (int i = 0; i < 8; i++) {
			int newy = nowy + dy[i];
			int newx = nowx + dx[i];
			if (newy >= 0 && newy < h && newx >= 0 && newx < w && dif[newy][newx] == inf && c[newy][newx] == '1') {
				dif[newy][newx] = 1;
				que.push(make_pair(newy, newx));
			}
		}

	}
	
}

int main() {
	vector<ll>ans;
	while (1) {
		
		ll count = 0;
		cin >> w >> h;
		if (h == 0 && w == 0) {
			break;
		}
		for (int i = 0; i < h; i++) {
			for (int j = 0; j < w; j++) {
				cin >> c[i][j];
				dif[i][j] = inf;
			}
		}
		
		for (int i = 0; i < h; i++) {
			for (int j = 0; j < w; j++) {
				if (dif[i][j] == inf&&c[i][j]=='1') {
					count++;
					dif[i][j] = 1;
					dfs(make_pair(i, j));
				}
			}
		}
		ans.push_back(count);
	}
	for (int i = 0; i < ans.size(); i++) {
		cout << ans[i] << endl;
	}
	return 0;
}

C++: #include<bits//stdc++.h>
using namespace std;
int sum;
void solve(bool a[50][50],int h, int w, int i, int j){
	for(int z=-1;z<2;z++){
		for(int x=-1;x<2;x++){
			if(i+z>=0&&i+z<h&&j+x>=0&&j+x<w&&a[i+z][j+x]){
				a[i+z][j+x]=false;
				solve(a,h,w,i+z,j+x);
			}
		}
	}
}
int main(){
	int h,w;
	while(cin>>w>>h&&h!=0&&w!=0){
		bool a[50][50];
		for(int i=0;i<h;i++){
			for(int j=0;j<w;j++){
				int x;
				cin>>x;
				if(x==0) a[i][j]=false;
				else a[i][j]=true;
			}
		}
		sum=0;
		for(int i=0;i<h;i++){
			for(int j=0;j<w;j++){
				if(a[i][j]){
					sum++;
					solve(a,h,w,i,j);
				}
			}
		}
		cout<<sum<<endl;
	}
}

C++: #include <stdio.h>

void dps(int il[50][50], int y, int x)	{
	il[y][x] = 0;
	
	for (int dy = -1; dy <= 1; dy++)	{
		for (int dx = -1; dx <= 1; dx++)	{
			if (dy == 0 && dx == 0)	continue;
			if (y+dy < 0 || y+dy >= 50 || x+dx < 0 || x+dx >= 50)	continue;
			if (il[y+dy][x+dx] == 1)	dps(il, y+dy, x+dx);
		}
	}
}

int main()	{
	
	int w, h, cnt;
	int il[50][50];
	
	while (true)	{
		
		scanf("%d %d", &w, &h);
		if (w == 0)	break;
		
		cnt = 0;
		
		for (int y = 0; y < 50; y++)	{
			for (int x = 0; x < 50; x++)	{
				il[y][x] = 0;
			}
		}
		
		for (int y = 0; y < h; y++)	{
			for (int x = 0; x < w; x++)	{
				scanf("%d", &il[y][x]);
			}
		}
		
		for (int y = 0; y < h; y++)	{
			for (int x = 0; x < w; x++)	{
				if (il[y][x] == 1)	{
					dps(il, y, x);
					cnt++;
				}
			}
		}
		
		printf("%d\n", cnt);
	}
	
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;

int fill_land(int y,int x,int h,int w,vector<vector<int>> &c);

int main()
{

	for (;;)
	{
		int w,h;
		cin >> w >> h;
		if ((w==0)&&(h==0))
		{
			return 0;
		}

		vector<vector<int>> c(h,vector<int>(w));

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

		int count=0;
		for (int i=0;i<h;++i)
		{
			for (int j=0;j<w;++j)
			{
				if (c[i][j]!=0)
				{
					// land
					++count;
					c[i][j]=0;
					fill_land(i,j,h,w,c);
				}
			}
		}
		cout << count << endl;
	}
}

int fill_land(int y,int x,int h,int w,vector<vector<int>> &c)
{
	stack<pair<int,int>> st;

	st.push(make_pair(y,x));
	while (st.empty()==0)
	{
		auto p=st.top();
		st.pop();

		if ( (p.first>0) && (c[p.first-1][p.second]!=0) )
		{
			c[p.first-1][p.second]=0;
			st.push(make_pair(p.first-1,p.second));
		}
		if ( (p.first<h-1) && (c[p.first+1][p.second]!=0) )
		{
			c[p.first+1][p.second]=0;
			st.push(make_pair(p.first+1,p.second));
		}
		if ( (p.second>0) && (c[p.first][p.second-1]!=0) )
		{
			c[p.first][p.second-1]=0;
			st.push(make_pair(p.first,p.second-1));
		}
		if ( (p.second<w-1) && (c[p.first][p.second+1]!=0) )
		{
			c[p.first][p.second+1]=0;
			st.push(make_pair(p.first,p.second+1));
		}
		if ( (p.first>0) && (p.second>0) && (c[p.first-1][p.second-1]!=0) )
		{
			c[p.first-1][p.second-1]=0;
			st.push(make_pair(p.first-1,p.second-1));
		}
		if ( (p.first>0) && (p.second<w-1) && (c[p.first-1][p.second+1]!=0) )
		{
			c[p.first-1][p.second+1]=0;
			st.push(make_pair(p.first-1,p.second+1));
		}
		if ( (p.first<h-1) && (p.second>0) && (c[p.first+1][p.second-1]!=0) )
		{
			c[p.first+1][p.second-1]=0;
			st.push(make_pair(p.first+1,p.second-1));
		}
		if ( (p.first<h-1) && (p.second<w-1) && (c[p.first+1][p.second+1]!=0) )
		{
			c[p.first+1][p.second+1]=0;
			st.push(make_pair(p.first+1,p.second+1));
		}
	}
	return 0;
}

C++: #include <iostream>
#include <vector>
using namespace std;
int c[100][100];
int w, h;
void dfs(int x,int y) {
	c[x][y] = 0;
	for (int i = -1; i <= 1; i++) {
		for (int j = -1; j <= 1; j++) {
			if (x + i < w&&x + i >= 0 && y + j >= 0 && y + j < h) {
				if (c[x + i][y + j] == 1) {
					dfs(x + i, y + j);
				}
			}
		}
	}
}

int main(void) {
	int ans;
	while (1) {
		ans = 0;
		cin >> w >> h;
		if (w == 0 && h == 0) {
			break;
		}
		for (int i = 0; i < h; i++) {
			for (int j = 0; j < w; j++) {
				cin >> c[j][i];
			}
		}
		for (int i = 0; i < h; i++) {
			for (int j = 0; j < w; j++) {
				if (c[j][i] == 1) {
					dfs(j, i);
					ans++;
				}
			}
		}
		cout << ans << endl;
	}
	return 0;
}