fork download
  1. #include<bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. int main(){
  6. int m, n;
  7. cin >> m >> n;
  8. int polje[m][n];
  9. for (int i = 0; i < m; i++) {
  10. for (int j = 0; j < n; j++) {
  11. cin >> polje[i][j];
  12. }
  13. }
  14.  
  15. int brojac = 0;
  16.  
  17. for (int i = 0; i < m; i++) {
  18. for (int j = 0; j < n; j++) {
  19. if (polje[i-1][j-1] == 1)
  20. brojac++;
  21. if (polje[i][j-1] == 1)
  22. brojac++;
  23. if (polje[i+1][j-1] == 1)
  24. brojac++;
  25. if (polje[i+1][j] == 1)
  26. brojac++;
  27. if (polje[i-1][j] == 1)
  28. brojac++;
  29. if (polje[i-1][j+1] == 1)
  30. brojac++;
  31. if (polje[i][j+1] == 1)
  32. brojac++;
  33. if (polje[i+1][j+1] == 1)
  34. brojac++;
  35.  
  36. cout << brojac << " ";
  37. brojac = 0;
  38. }
  39. cout << '\n';
  40. }
  41. }
Success #stdin #stdout 0.01s 5360KB
stdin
3 4
0 1 0 1
1 0 1 0
0 1 0 0
stdout
3 3 3 2 
3 4 3 3 
3 2 2 1