#include #include #include #include #include #include #include #include #include #define MAX_N 100001 #define INF_INT 2147483647 #define INF_LL 9223372036854775807 #define REP(i,n) for(int i=0;i<(int)(n);i++) int dx[4] = {1,0,0,-1}; int dy[4] = {0,1,-1,0}; using namespace std; typedef long long int ll; typedef pair P; int main() { int N,H,W,res=0; char pond[3010][3010]; bool visited[3010][3010]; REP(i,3010)REP(j,3010)visited[i][j] = false; cin >> H >> W; REP(i,H)REP(j,W)cin >> pond[i][j]; stack

stk; REP(i,H)REP(j,W){ if(pond[i][j] == '1'){ stk.push(P(j,i)); while(!stk.empty()){ P p = stk.top();stk.pop(); pond[p.second][p.first] = '0'; REP(i,4){ int x = p.first+dx[i],y=p.second+dy[i]; if(x < W && 0 <= x && y < H && y >= 0){ if(visited[y][x] == false && pond[y][x] == '1'){ stk.push(P(x,y)); visited[y][x] = true; } } } } res++; } } cout << res << endl; return 0; }