if (d[i + 1][j] == 1)
ans++;
if (d[i + 1][j + 1] == 1)
ans++;
if (d[i + 1][j - 1] == 1)
ans++;
if (d[i][j + 1] == 1)
ans++;
if (d[i][j - 1] == 1)
ans++;
if (d[i - 1][j] == 1)
ans++;
if (d[i - 1][j + 1] == 1)
ans++;
if (d[i - 1][j - 1] == 1)
ans++;