Go: package main

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

var sc = bufio.NewScanner(os.Stdin)
var route [][]int

func scanInt() int {
	num, _ := strconv.Atoi(scanString())
	return num
}

func scanString() string {
	sc.Scan()
	return sc.Text()
}

func init() {
	sc.Split(bufio.ScanWords)
}

func dfs(h, w int) {
	if route[h][w] == 0 {
		return
	}

	route[h][w] = 0
	for _, x := range []int{-1, 0, 1} {
		for _, y := range []int{-1, 0, 1} {
			if route[h+x][w+y] == 0 {
				continue
			}
			dfs(h+x, w+y)
		}
	}
}

func main() {
	writer := bufio.NewWriter(os.Stdout)
	for {
		w, h := scanInt(), scanInt()
		if w == 0 && h == 0 {
			break
		}

		route = make([][]int, h+2)
		route[0] = make([]int, w+2)
		for i := 1; i <= h; i++ {
			route[i] = make([]int, w+2)
			for j := 1; j <= w; j++ {
				route[i][j] = scanInt()
			}
		}
		route[h+1] = make([]int, w+2)

		total := 0
		for i := 1; i <= h; i++ {
			for j := 1; j <= w; j++ {
				if route[i][j] == 1 {
					total++
					dfs(i, j)
				}
			}
		}
		fmt.Fprintln(writer, total)
	}
	writer.Flush()
}

Python: from collections import deque
def dfs(i,j,seen):
    q=deque()
    q.append((i,j,0))
    seen[i][j]=0
    dx=[0,1,0,-1,-1,-1,1,1]
    dy=[1,0,-1,0,1,-1,1,-1]
    while q:
        X,Y,S=q.pop()
        if S==0:
            #下に遷移していくときの処理
            #q.append((V,P,1))
            for k in range(8):
                nx=X+dx[k]
                ny=Y+dy[k]
                if not(0<=nx<h and 0<=ny<w):continue
                if seen[nx][ny]==0:continue
                seen[nx][ny]=0
                q.append((nx,ny,0))
    return 1

def main(w,h):
    grid=[list(map(int,input().split())) for i in range(h)]
    ans=0
    for i in range(h):
        for j in range(w):
            if grid[i][j]:
                ans+=dfs(i,j,grid)
    print(ans)

while True:
    w,h=map(int,input().split())
    if (w,h)==(0,0):break
    main(w,h)

Python: import sys
input=sys.stdin.readline
sys.setrecursionlimit(10**6)

def dfs(x,y):
    c[x][y]='0'
    for dx in range(-1,2):
        for dy in range(-1,2):
            nx=dx+x
            ny=dy+y
            if c[nx][ny]=='1':dfs(nx,ny)

while 1:
    c=[]
    w,h=map(int,input().split())
    if w==h==0:exit()
    w+=2
    c=[list('@'*w)]
    for i in range(h):
        c+=[list('@'+input().replace(' ','').replace('\n','')+'@')]
    c+=[list('@'*w)]
    h+=2
    cnt=0

    for i in range(1,h):
        for j in range(1,w):
            if c[i][j]=='1':
                dfs(i,j)
                cnt+=1
    print(cnt)

Python: #!/usr/bin/env python
# -*- coding: utf-8 -*-

def dfs(s,Map,H,W,c):
    dxdy = [(1,0),(-1,0),(0,1),(0,-1),(1,1),(-1,-1),(-1,1),(1,-1)]
    for i in range(4):
        Map[s[0]][s[1]] = c
        nextPos = (s[0]+dxdy[i][0],s[1]+dxdy[i][1])
        if nextPos[0] >= 0 and nextPos[1] >= 0 and nextPos[0] < H and nextPos[1] < W:
            if Map[nextPos[0]][nextPos[1]] == 1:
                dfs(nextPos,Map,H,W,c)

while True:
    W,H = map(int,input().split(" "))
    if W == 0 and H == 0:
        break
    Map = [list(map(int,input().split())) for i in range(0,H)]
    c = 1
    for i in range(H):
        for j in range(W):
            if Map[i][j] == 1:
                c += 1
                dfs((i,j),Map,H,W,c)
    print(c-1)

Python: import sys
 
sys.setrecursionlimit(10**6)


def lake_counting(field):

  lake_count = 0

  def dfs(x,y):
    field[x][y] = 0
    dx = [-1,-1,-1,0,0,1,1,1]
    dy = [-1,0,1,-1,1,1,0,-1]
    

    for i in range(8):
        nx = x + dx[i]
        ny = y + dy[i]
        if (0 <= nx < h and 0 <= ny < w and field[nx][ny] == 1):
          dfs(nx,ny)

  for i in range(h):
    for j in range(w):
      if field[i][j] == 1:
        dfs(i,j)
        lake_count += 1

  return lake_count

while True:
  w, h = map(int,input().split())
  if w == h == 0:
    break
  A = []
  for i in range(h):
    c = list(map(int,input().split()))
    A.append(c)
  print(lake_counting(A))

Python: # http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1160&lang=jp
import sys
read = sys.stdin.read
readline = sys.stdin.readline

def main():
    ans = []
    while True:
        w, h = map(int, readline().split())
        if w == 0:
            break

        c = [[0] * (w+2)]
        for _ in range(h):
            c.append([0] + [int(i) for i in readline().split()] + [0])
        c.append([0] * (w+2))

        cnt = 0
        seen = [[-1] * (w+2) for _ in range(h+2)]
        def dfs(sx, sy):
            stack = [(sx, sy)]
            
            while stack:
                x, y = stack.pop()

                for dx in [-1, 0, 1]:
                    for dy in [-1, 0, 1]:
                        if dx == 0 and dy == 0:
                            continue
                        nx = x + dx
                        ny = y + dy

                        if c[nx][ny] and seen[nx][ny] == -1:
                            seen[nx][ny] = 1
                            stack.append((nx, ny))

        for i in range(1,h+1):
            for j in range(1,w+1):
                if c[i][j] and seen[i][j] == -1:
                    cnt += 1
                    dfs(i, j)

        ans.append(cnt)
    
    print("\n".join(map(str, ans)))    


if __name__ == "__main__":
    main()

Python: # -*- coding: utf-8 -*-
import sys
sys.setrecursionlimit(10000)

while(True):
    w,h = map(int,input().split())
    if w == 0 and h == 0:
        break
    c = [0] * h
    for i in range(h):
        c[i] = list(map(int,input().split()))

    checked = [[0] * w for _ in range(h)]
    def dfs(x,y):
        global checked,c
        checked[y][x] = 1
        c[y][x] = 0
        for i in range(-1,2):
            for j in range(-1,2):
                if 0 <= x + i < w and 0 <= y + j < h:
                    if checked[y + j][x + i] == 0 and c[y + j][x + i] == 1:
                        dfs(x + i,y + j)

    cnt = 0
    for i in range(h):
        for j in range(w):
            if c[i][j] == 1:
                dfs(j,i)
                cnt += 1

    print(cnt)

Python: import sys
sys.setrecursionlimit(10000)
d = [-1,0,1]
def search(x,y,m):
    if not(0<=x<len(m[0]) and 0<=y<len(m)) or m[y][x]=='0': return
    m[y][x] = '0'
    for dy in d:
        for dx in d:
            search(x+dx,y+dy,m)
while True:
    w,h = map(int,input().split())
    if w==h==0: break
    m = [input().split() for _ in range(h)]
    c = 0
    for y in range(h):
        for x in range(w):
            if m[y][x]=='1':
                c += 1
                search(x,y,m)
    print(c)

Python: import sys
sys.setrecursionlimit(10000)
def fill(y,x):
    global w,h,l
    if 0<=y<h and 0<=x+1<w and l[y][x+1]==1:
        l[y][x+1]=0
        fill(y,x+1)
    if 0<=y<h and 0<=x-1<w and l[y][x-1]==1:
        l[y][x-1]=0
        fill(y,x-1)
    if 0<=y+1<h and 0<=x<w and l[y+1][x]==1:
        l[y+1][x]=0
        fill(y+1,x)
    if 0<=y-1<h and 0<=x<w and l[y-1][x]==1:
        l[y-1][x]=0
        fill(y-1,x)
    
    if 0<=y+1<h and 0<=x+1<w and l[y+1][x+1]==1:
        l[y+1][x+1]=0
        fill(y+1,x+1)
    if 0<=y-1<h and 0<=x+1<w and l[y-1][x+1]==1:
        l[y-1][x+1]=0
        fill(y-1,x+1)
    if 0<=y+1<h and 0<=x-1<w and l[y+1][x-1]==1:
        l[y+1][x-1]=0
        fill(y+1,x-1)
    if 0<=y-1<h and 0<=x-1<w and l[y-1][x-1]==1:
        l[y-1][x-1]=0
        fill(y-1,x-1)
    
while 1:
    w,h=map(int,input().split())
    if not w and not h:break
    l=[list(map(int,input().split())) for _ in range(h)]

    cnt=0
    while 1:
        cds=[[i,j.index(1)] for (i,j) in enumerate(l) if 1 in j]
        if not cds:
            break
        else:
            y,x=cds[0]
        l[y][x]=0
        fill(y,x)
        cnt+=1
    print(cnt)