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)
	}
}

Python: anslist = []
def sarch(i, j, visited):

    if 0 < i:
        if visited[i - 1][j] == 0:
            visited[i - 1][j] = 1
            sarch(i - 1, j, visited)
        if 0 < j:
            if visited[i - 1][j - 1] == 0:
                visited[i - 1][j - 1] = 1
                sarch(i - 1, j - 1, visited)
        if j < len(visited[0]) - 1:
            if visited[i - 1][j + 1] == 0:
                visited[i - 1][j + 1] = 1
                sarch(i - 1, j + 1, visited)

    if i < len(visited) - 1:
        if visited[i + 1][j] == 0:
            visited[i + 1][j] = 1
            sarch(i + 1, j, visited)
        if 0 < j:
            if visited[i + 1][j - 1] == 0:
                visited[i + 1][j - 1] = 1
                sarch(i + 1, j - 1, visited)
        if j < len(visited[0]) - 1:
            if visited[i + 1][j + 1] == 0:
                visited[i + 1][j + 1] = 1
                sarch(i + 1, j + 1, visited)

    if 0 < j:
        if visited[i][j - 1] == 0:
            visited[i][j - 1] = 1
            sarch(i, j - 1, visited)
    if j < len(visited[0]) - 1:
        if visited[i][j + 1] == 0:
            visited[i][j + 1] = 1
            sarch(i, j + 1, visited)
    return visited


while True:
    w, h = map(int, input().split())
    if w == 0 and h == 0:
        break
    grid = [list(map(int, input().split())) for j in range(h)]
    visited = [[0 for i in range(w)] for j in range(h)]
    starti = 0
    startj = 0
    for i in range(h):
        for j in range(w):
            if grid[i][j] == 0:
                visited[i][j] = -1
            else:
                visited[i][j] = 0

    i = 0
    j = 0
    cnt = 0
    ans = 0
    #print(visited)
    while True:
        if i >= h:
            i = 0
            j = 0
        while True:
            cnt += 1
            if j >= w:
                j = 0
                break
            #print(visited[i][j], 100100)
            if visited[i][j] == 0:
                ans += 1
                visited[i][j] = 1
                visited = sarch(i, j, visited)
                cnt = 0
            j += 1
        i += 1
        #print(cnt, visited)
        if cnt > w * h:
            #print(10)
            break
    anslist.append(ans)

for i in anslist:
    print(i)

Python: import sys
from itertools import product

sys.setrecursionlimit(10000)

while True:
    W, H = map(int, input().split(' '))
    if W == 0 and H == 0:
        break

    m = tuple([list(map(int, input().split(' '))) for _ in range(H)])


    def dfs(y, x):
        if m[y][x] == 0:
            return False
        else:
            m[y][x] = 0

        for dy, dx in product((-1, 0, 1), repeat=2):
            ny = y + dy
            nx = x + dx
            if 0 <= ny < H and 0 <= nx < W and m[ny][nx] == 1:
                dfs(ny, nx)

        return True


    count = 0
    for i in range(H):
        for j in range(W):
            if m[i][j] == 1:
                count += int(dfs(i, j))

    print(count)

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(1):
        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: #template
def inputlist(): return [int(j) for j in input().split()]
#template
dx = [1,1,0,-1,-1,-1,0,1]
dy = [0,1,1,1,0,-1,-1,-1]
from collections import deque
def dfs(x,y,w,h,lis,flag):
    if lis[x][y] == 1:
        flag = 1
        lis[x][y] = 0
        stack = deque([])
        l = [x,y]
        stack.appendleft(l)
        while len(stack) != 0:
            li = stack.popleft()
            xa = li[0]
            ya = li[1]
            for i in range(8):
                nx = xa + dx[i]
                ny = ya + dy[i]
                if nx < 0 or ny < 0 or nx > h-1 or ny > w-1:
                    continue
                if lis[nx][ny] == 0:
                    continue
                stack.appendleft([nx,ny])
                lis[nx][ny] = 0
    if flag:
        return 1
    else:
        return 0

while True:
    w,h = inputlist()
    if w == 0 and h == 0:
        break
    maze = [[0] for _ in range(h)]
    for i in range(h):
        maze[i] = inputlist()
    ans = 0
    for x in range(h):
        for y in range(w):
            ans += dfs(x,y,w,h,maze,0)
    print(ans)

Python: import sys
sys.setrecursionlimit(10000)

def bfs(x, y, graph):
  dxy = [(1, 1), (1, 0), (1, -1), (0, 1), (0, -1), (-1, 1), (-1, 0), (-1, -1)]
  for dx, dy in dxy:
    nextx = x + dx
    nexty = y + dy
    if (graph[nextx][nexty] == 1):
      graph[nextx][nexty] = 0
      bfs(nextx, nexty, graph)

while(1):
  W, H = map(int, input().split())
  if(H == 0 and W == 0):
    break
    
  graph = [[0] * (W+2) for _ in range(H+2)]
  for i in range(H):
    x = list(map(int, input().split()))
    for j in range(W):
      graph[i+1][j+1] = x[j]
  
  cnt = 0
  for i in range(1,H+1):
    for j in range(1,W+1):
      if(graph[i][j] == 1):
        cnt += 1
        bfs(i, j, graph)
  print(cnt)

Python: from collections import deque

def main(H,W):
  area = [[] for i in range(H)]
  island = []
  
  for _ in range(H):
    h = list(map(int,input().split()))
    area[_] = h
    for i in range(W):
      if h[i] == 1:
        island.append([_,i])
  
  visited = [[0] * W for i in range(H)]
  
  def dfs(area, visited,sh,sw):
    stack = deque([[sh,sw]])
    visited[sh][sw] = 1
    while stack:
      h,w = stack.pop()
      for j in range(-1,2):
        for k in range(-1,2):
          new_h, new_w = h + j, w + k
          if new_h < 0 or new_h >= H or \
            new_w < 0 or new_w >= W:
              continue
          elif area[new_h][new_w] == 1 and \
            visited[new_h][new_w] == 0:
            visited[new_h][new_w] = 1
            stack.append([new_h, new_w])
    return 
  
  ans = 0
  
  for i in island:
    sh,sw = i[0],i[1]
    if visited[sh][sw] == 0:
      dfs(area,visited,sh,sw)
      ans += 1
  
  print(ans)
  
while True:
  W,H = map(int,input().split())
  if H == 0 and W == 0:
    break
  else:
    main(H,W)