fork(1) download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. const int N = 50;
  5. int n, m;
  6. char g[N][N + 1];
  7.  
  8. int main(int argc, char **argv) {
  9. int t;
  10. scanf("%d", &t);
  11. while (t-- != 0) {
  12. scanf("%d%d", &n, &m);
  13. for (int i = 0; i < n; ++i)
  14. scanf("%s", g[i]);
  15. int all = 0, ok = 0;
  16. for (int i = 0; i < n; ++i)
  17. for (int j = 0; j < m; ++j)
  18. if (g[i][j] == '1') {
  19. ++all;
  20. if (i == 0 || j == 0 || i == n - 1 || j == m - 1)
  21. ++ok;
  22. }
  23. int need = n * 2 + m * 2 - 4;
  24. if (all >= need)
  25. printf("%d\n", need - ok);
  26. else
  27. puts("-1");
  28. }
  29. return 0;
  30. }
Success #stdin #stdout 0s 15240KB
stdin
Standard input is empty
stdout
Standard output is empty