Go: package main

import (
	"fmt"
)

func main() {
	var h, w int
	for {
		fmt.Scan(&w, &h)
		if w == 0 && h == 0 {
			return
		}
		matrix := make([][]int, h)
		Sean := make([][]bool, h)
		for i := 0; i < h; i++ {
			matrix[i] = make([]int, w)
			Sean[i] = make([]bool, w)
			for j := 0; j < w; j++ {
				fmt.Scan(&matrix[i][j])
			}
		}
		var count int
		for i := 0; i < h; i++ {
			for j := 0; j < w; j++ {
				if matrix[i][j] == 1 && Sean[i][j] == false {
					search(i, j, h, w, matrix, Sean)
					count++
				}
			}
		}
		fmt.Println(count)
	}
}
func search(x int, y int, h int, w int, matrix [][]int, Sean [][]bool) {
	if x < 0 || y < 0 || x >= h || y >= w || matrix[x][y] == 0 {
		return
	}
	if Sean[x][y] == true {
		return
	}

	Sean[x][y] = true

	search(x+1, y, h, w, matrix, Sean)
	search(x-1, y, h, w, matrix, Sean)
	search(x, y+1, h, w, matrix, Sean)
	search(x, y-1, h, w, matrix, Sean)
	search(x+1, y-1, h, w, matrix, Sean)
	search(x-1, y+1, h, w, matrix, Sean)
	search(x+1, y+1, h, w, matrix, Sean)
	search(x-1, y-1, h, w, matrix, Sean)
}

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

int state[100][100];
bool reached[100][100];
int w,h;

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

void dfs(int x, int y) {

	if(x==0 || w<x || y==0 || h<y || state[x][y] == 0)
		return;
	if(reached[x][y] == true)
		return;

	reached[x][y]=true;

	for(int i=0; i<8; i++) {
		dfs(x+dx[i],y+dy[i]);
	}
}

int main() {

	memset(state, 0, sizeof(state));
	memset(reached, false, sizeof(reached));

	cin >> w >> h;
	for(int i=1; i<=w; i++) {
		for(int j=1; j<=h; j++) {
			cin >> state[i][j];
		}
	}
	int count=0;
	for(int i=1; i<=w; i++) {
		for(int j=1; j<=h; j++) {
			if(reached[i][j]==true || state[i][j] == 0)
				continue;
			else{
				dfs(i,j);
				count++;
			}
		}
	}
	cout << count << endl;
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define FOR(i,o,n) for(long long i = o;i<n;i++)
#define oneforall ios::sync_with_stdio(false);cin.tie(0);
#define all(v) (v).begin(),(v).end()
#define ini(...) int __VA_ARGS__; in(__VA_ARGS__)
#define inl(...) long long __VA_ARGS__; in(__VA_ARGS__)
#define ins(...) string __VA_ARGS__; in(__VA_ARGS__)
#define int long long 
const long long inf=1e18;
void in(){} template <typename T,class... U> void in(T &t,U &...u){ cin >> t; in(u...);}
void out(){cout << "\n";} template <typename T,class... U> void out(const T &t,const U &...u){ cout << t; if(sizeof...(u)) cout << " "; out(u...);}
typedef vector<int> vi;
typedef vector<long long> vl;
typedef long long ll;
typedef vector<pair<long, long > > vpll;
typedef vector<pair<int, int > > vpii;
#define FORR(x,arr) for(auto& x:arr)
#define ZERO(a) memset(a,0,sizeof(a))





//3111111111111111111111111111111

	int x,x1;
	int morty[310][310];
	int tmpx[3] = {-1,1,0};
	void dfs(int tmp,int tmp1){
		morty[tmp][tmp1] = 0;
		FOR(i,0,3)FOR(i1,0,3){
			int nowtmp = tmp+tmpx[i];
			int nowtmp1 = tmp1+tmpx[i1];
			if(!(nowtmp == tmp and nowtmp1 == tmp1) and nowtmp>=0 and nowtmp<=x-1 and nowtmp1>=0 and nowtmp1<=x1-1 and morty[nowtmp][nowtmp1] == 1){
				dfs(nowtmp,nowtmp1);
			}
		}
	}




	void solve(){
		
		while(1){
			
			in(x1,x);
			if(x == 0 and x1 == 0)break;
			FOR(i,0,x)FOR(i1,0,x1){
				ini(tmp);
				if(tmp == 0){
					morty[i][i1] = 0;
				}
				else{
					morty[i][i1] = 1;
				}
			}
			int rick = 0;
			//FOR(i,0,x){
			//	FOR(i1,0,x1){
				//cout<<morty[i][i1]<<" ";			
			//	}
				//out();
			//}
			FOR(i,0,x)FOR(i1,0,x1){
				if(morty[i][i1] == 1){
					dfs(i,i1);
					rick++;
				}
			}
			out(rick);
		}








		
		
	}























int32_t main() {
	oneforall
	oneforall
	oneforall
	oneforall
	oneforall
	oneforall
	oneforall
	oneforall
	oneforall
	oneforall
	oneforall
	oneforall
	oneforall 
	oneforall

	solve();
	


	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
int w,h,c[100][100],ans;
pair<int,int> movemy[8]={{0,1},{1,0},{1,1},{-1,0},{0,-1},{-1,1},{1,-1},{-1,-1}};


void land(int y,int x){
	if(y<0||y>h-1||x<0||x>w-1||c[y][x]==0)return;
	c[y][x]=0;
	for(int i=0;i<8;i++){
		land(y+movemy[i].first,x+movemy[i].second);
	}
}

int main(){
	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[i][j];
				//cout<<"AAA"<<endl;
			}
		}
		//cout<<"AAA"<<endl;
		for(int i=0;i<h;i++){
			for(int j=0;j<w;j++){
				if(c[i][j]){land(i,j);ans++;}
				
			}
		}
		cout<<ans<<endl;
	}
}

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

using i64 = long long int;

int c[51][51] = {0};

void bfs(int w, int h, int x, int y){
    if(x < 0 || x >= w)return;
    if(y < 0 || y >= h)return;
    if(c[y][x] == 0)return;

    c[y][x] = 0;
    for(int dy=-1;dy<=1;++dy){
        for(int dx=-1;dx<=1;++dx)
            bfs(w, h, x+dx, y+dy);
    }
}

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

        int ans = 0;
        for(int i=0;i<h;++i){
            for(int j=0;j<w;++j){
                if(c[i][j] == 1){
                    bfs(w, h, j, i);
                    ans++;
                }
            }
        }

        cout << ans << endl;
    }

    return 0;
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <utility>
#include <queue>
#include <set>
#include <stdio.h>
#include <string>
using namespace std;
#define ll long long

int W, H;
vector<vector<int>> field;

void dfs(int h, int w) {
	field[h][w] = 0;

	for (int dx = -1; dx <= 1; dx++) {
		for (int dy = -1; dy <= 1; dy++) {
			int nh = h + dy;
			int nw = w + dx;

			if (nh < 0 || nw < 0 || nh >= H || nw >= W)continue;
			if (field[nh][nw] == 0)continue;

			dfs(nh, nw);
		}
	}
}

int main() {
	while (1) {
		cin >> W >> H;
		if (W == 0 && H == 0)break;
		field.assign(H, vector<int>(W, 0));
		for (int i = 0; i < H; i++) {
			for (int j = 0; j < W; j++) {
				cin >> field[i][j];
			}
		}
		int ans = 0;
		for (int i = 0; i < H; i++) {
			for (int j = 0; j < W; j++) {
				if (field[i][j] == 0)continue;
				dfs(i, j);
				ans++;
			}
		}
		cout << ans << endl;
	}
	return 0;
}

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

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

int H, W;
int sima[55][55];

bool seen[55][55];
void dfs(int h, int w){
  seen[h][w] = true;
  for(int dir=0; dir<8; dir++){
    int nh = h + dy[dir];
    int nw = w + dx[dir];
    if(nh>=H || nh<0 || nw>=W || nw<0){
      continue;
    }
    if(sima[nh][nw] == 0){
      continue;
    }
    if(seen[nh][nw]){
      continue;
    }
    dfs(nh, nw);
  }
}

int main(){
  while(1){
    cin >> W >> H;
    if(H == 0 && W == 0){
      break;
    }
    for(int h=0; h<H; h++){
      for(int w=0; w<W; w++){
        cin >> sima[h][w];
      }
    }
    memset(seen, 0, sizeof(seen));
    int count=0;
    for(int i=0; i<H; i++){
      for(int j=0; j<W; j++){
        if(seen[i][j]){
          continue;
        }
        if(sima[i][j] == 0){
          continue;
        }
        dfs(i, j);
        count++;
      }
    }
    cout << count << endl;
  }
  return 0;
}