#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; const char infile[] = "input.in"; const char outfile[] = "output.out"; ifstream fin(infile); ofstream fout(outfile); const int MAXN = 505; const int oo = 0x3f3f3f3f; typedef vector Graph[MAXN]; typedef vector :: iterator It; const inline int min(const int &a, const int &b) { if( a > b ) return b; return a; } const inline int max(const int &a, const int &b) { if( a < b ) return b; return a; } const inline void Get_min(int &a, const int b) { if( a > b ) a = b; } const inline void Get_max(int &a, const int b) { if( a < b ) a = b; } char a[MAXN][MAXN]; int N, M; int Ans = 0; int xst, yst; bool found = 1; set > visited; const int dx[] = {-1, 1, 0, 0}; const int dy[] = { 0, 0,-1, 1}; inline bool inside(int x, int y) { return x >= 1 && x <= N && y >= 1 && y <= M; } void DFs(int x, int y, int cnt) { if(!found) return; visited.insert(make_pair(x, y)); for(int d = 0 ; d < 4 ; ++ d) { int newx = x + dx[d]; int newy = y + dy[d]; if(inside(newx, newy) && a[newx][newy] == '1') { if(visited.find(make_pair(newx, newy)) == visited.end()) DFs(newx, newy, cnt + 1); else if(cnt >= 4 && newx == xst && newy == yst) { if(found) ++ Ans; //cerr << xst << ' ' << yst << '\n'; found = 0; return; } } } } int main() { #ifndef ONLINE_JUDGE freopen(infile, "r", stdin); freopen(outfile, "w", stdout); cin.sync_with_stdio(false); #endif cin >> N >> M; for(int i = 1 ; i <= N ; ++ i) cin >> (a[i] + 1); for(int i = 1 ; i <= N ; ++ i) for(int j = 1 ; j <= M ; ++ j) { if(a[i][j] == '1') { found = 1; xst = i; yst = j; DFs(i, j, 1); visited.clear(); } } cout << Ans << '\n'; return 0; }