malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   int f(string s, int i) {     if (s[i] ==  ( ) return 0;     if (s[i] ==  [ ) return 1;     if (s[i] ==  8 ) return 2;   }   int main() {     int a, b;     string s1, s2;     cin >> s1 >> s2;     a = b = 0;     for (int i = 0; i < s1.size(); i += 2) {       if ((f(s1, i) == 0 && f(s2, i) == 2) || (f(s1, i) == 1 && f(s2, i) == 0) ||           (f(s1, i) == 2 && f(s2, i) == 1))         a++;       else if (f(s1, i) != f(s2, i))         b++;     }     if (a == b) cout <<  TIE ;     if (a > b) cout <<  TEAM 1 WINS ;     if (b > a) cout <<  TEAM 2 WINS ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int mod = 1e9 + 7;   const int max_n = 200005;   vector<pair<int, int>> g[max_n];   int sz[max_n];   long long dis[max_n];   void dfs(int x) {     sz[x] = 1;     for (auto p : g[x]) {       int u = p.first;       if (sz[u]) continue;       dis[u] = p.second;       dfs(u);       sz[x] += sz[u];     }   }   void solve() {     int n;     cin >> n;     n *= 2;     for (int i = 0; i <= n; i++) g[i].clear();     for (int i = 0; i <= n; i++) sz[i] = dis[i] = 0;     for (int i = 1; i < n; i++) {       int u, v, w;       cin >> u >> v >> w;       g[u].push_back({v, w});       g[v].push_back({u, w});     }     dfs(1);     long long ans1 = 0, ans2 = 0;     for (int i = 2; i <= n; i++) {       if (sz[i] & 1) ans1 += dis[i];       ans2 += min(sz[i], n - sz[i]) * dis[i];     }     cout << ans1 <<     << ans2 << endl;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(nullptr);     int t;     cin >> t;     while (t--) solve();     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     long long n, i, tmp_ara1, tmp_ara2;     vector<long long> ara1(2000007, -1), ara2(2000007, -1), ans(200007, -1),         in_front;     cin >> n;     for (i = 1; i <= n; i++) {       cin >> tmp_ara1 >> tmp_ara2;       ara1[tmp_ara1] = tmp_ara2;       ara2[tmp_ara2] = tmp_ara1;       in_front.push_back(tmp_ara1);     }     long long key = 0;     for (i = 2; i <= n; i += 2) {       ans[i] = ara1[key];       key = ara1[key];     }     long long first;     for (i = 0; i < n; i++) {       if (ara2[in_front[i]] == -1) {         first = in_front[i];         break;       }     }     key = first;     ans[1] = first;     for (i = 3; i <= n; i += 2) {       ans[i] = ara1[key];       key = ara1[key];     }     for (i = 1; i <= n; i++) {       cout << ans[i] <<    ;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n, m;   char gird[64][64];   set<long long int> set_of_cols;   int main(void) {     int i, j;     scanf( %d%d , &n, &m);     for (i = 0; i < n; ++i) scanf( %s , gird[i]);     for (i = 0; i < n; ++i) {       long long int id = 0;       for (j = 0; j < m; ++j) {         id <<= 1;         id |= ( #  == gird[i][j]) ? 1 : 0;       }       for (auto have : set_of_cols) {         if (have != id && ((have & id) != 0)) {           puts( No );           return 0;         }       }       set_of_cols.insert(id);     }     puts( Yes );     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     string str;     cin >> str;     int t;     cin >> t;     int type, pos, l, r;     char c;     set<int> s[26];     for (int i = 0; i < str.length(); i++) s[str[i] -  a ].insert(i + 1);     while (t--) {       cin >> type;       if (type == 1) {         cin >> pos >> c;         s[str[pos - 1] -  a ].erase(pos);         s[c -  a ].insert(pos);         str[pos - 1] = c;       } else {         cin >> l >> r;         int count = 0;         for (int i = 0; i < 26; i++)           if (s[i].lower_bound(l) != s[i].upper_bound(r)) count++;         cout << count << endl;       }     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   bool check(char a) {     if (a ==  a  || a ==  e  || a ==  i  || a ==  o  || a ==  u ) return true;     if (a ==  1  || a ==  3  || a ==  5  || a ==  7  || a ==  9 ) return true;     return false;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     string s;     cin >> s;     int ans = 0;     for (int i = 0; i < s.length(); i++)       if (check(s[i])) ans++;     cout << ans;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int t;     string s;     getline(cin, s);     long long n = s.size(), i;     for (i = n - 1; i >= 0; i--) {       if (isupper(s[i])) {         s[i] = tolower(s[i]);       }       if (s[i] ==  ?  || s[i] ==    ) {         continue;       } else if (s[i] ==  a  || s[i] ==  e  || s[i] ==  i  || s[i] ==  o  ||                  s[i] ==  u  || s[i] ==  y ) {         cout <<  YES  << endl;         break;       } else {         cout <<  NO  << endl;         break;       }     }   }
#include <bits/stdc++.h>   long long inp() {     char c = getchar();     int neg = 1;     while (c <  0  || c >  9 ) {       if (c ==  - ) neg = -1;       c = getchar();     }     long long sum = 0;     while (c >=  0  && c <=  9 ) {       sum = sum * 10 + c -  0 ;       c = getchar();     }     return sum * neg;   }   long long v[300010], mask[300010];   int main() {     int n = inp();     long long sum = 0;     for (int i = 1; i <= n; i++) {       v[i] = inp();       sum += v[i];       mask[i] = inp();     }     if (sum < 0)       for (int i = 1; i <= n; i++) v[i] *= -1;     long long s = 0;     for (int i = 0; i <= 61; i++) {       long long sum = 0;       for (int j = 1; j <= n; j++) {         if (mask[j] & (1ll << i)) {           if (mask[j] <= (1ll << (i + 1))) {             if (__builtin_popcountll(s & mask[j]) & 1)               sum += v[j];             else               sum -= v[j];           }         }       }       if (sum < 0) s |= (1ll << i);     }     std::cout << s << std::endl;   }
#include <bits/stdc++.h>   using namespace std;   pair<int, int> s[100010];   int a[100010], b[100010];   int main() {     int N, P, i, x;     scanf( %d n , &N);     for (i = 0; i < N; i++) {       scanf( %d , &x);       s[i] = make_pair(x, i);     }     sort(s, s + N);     P = N / 3;     for (i = 0; i < P; i++) {       a[s[i].second] = s[i].first;       b[s[i].second] = 0;     }     P = (2 * N) / 3;     for (; i < P; i++) {       a[s[i].second] = 0;       b[s[i].second] = s[i].first;     }     for (; i < N; i++) {       a[s[i].second] = s[i].first - (N - i - 1);       b[s[i].second] = N - i - 1;     }     printf( YES n );     for (i = 0; i < N; i++) printf( %d  , a[i]);     printf(  n );     for (i = 0; i < N; i++) printf( %d  , b[i]);     printf(  n );     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n, m, t[100010], nowt, pos, ans[100010];   pair<int, int> a[100010];   int main() {     scanf( %d%d , &n, &m);     for (int i = 1; i <= n; ++i) {       scanf( %d%d , &t[i], &a[i].first);       a[i].second = i;     }     for (int i = 1, j; i <= n; i = j + 1) {       j = min(n, i + m - 1);       sort(a + i, a + j + 1);       nowt = max(nowt, t[j]);       pos = 0;       for (int k = i, l; k <= j; k = l) {         l = k;         while (l <= j && a[l].first == a[k].first) ++l;         nowt += a[k].first - pos;         for (int h = k; h < l; ++h) ans[a[h].second] = nowt;         nowt += 1 + (l - k) / 2;         pos = a[k].first;       }       nowt += pos;     }     for (int i = 1; i <= n; ++i) printf( %d  , ans[i]);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long fastPow(long long a, long long b) {     long long ret = 1;     while (b) {       if (b & 1) ret *= a;       a *= a;       b >>= 1;     }     return ret;   }   int main() {     ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);     long long n, e, o, ce, co, ret = INT_MIN, c;     cin >> n;     vector<long long> nums(n + 1);     for (int i = int(1); i < int(n + 1); i++) cin >> nums[i];     e = 0, o = 0, ce = 0, co = 0, c = 0;     for (int l = int(1); l < int(n); l++) {       if (!(l % 2)) {         e = abs(nums[l] - nums[l + 1]);         ce = max(ce + e, c);         o = e * -1;         co = max(co + o, c);       } else {         e = abs(nums[l] - nums[l + 1]) * -1;         ce = max(ce + e, c);         o = e * -1;         co = max(co + o, c);       }       ret = max(ret, max(ce, co));     }     cout << ret << endl;     return 0;   }
#include <bits/stdc++.h>   int cut;   int q[100005] = {0};   int totalnode, totaledge, i, j, k, l, a, b, t1, t2;   int c(int q) {     if (q == 2) {       return cut;     } else if (q == cut) {       return 2;     } else       return q;   }   int main() {     for (i = 0; i < 100005; i++) q[i] = i;     scanf( %d %d %d , &totalnode, &totaledge, &cut);     q[cut] = 2;     q[2] = cut;     if (totaledge >= totalnode - 1 &&         (totalnode - 1) * (totalnode - 2) / 2 + 1 >= totaledge) {       for (i = 1; i <= totalnode - 1; i++) {         printf( %d %d n , q[i], q[i + 1]);         totaledge--;       }       for (i = 2; i <= totalnode && totaledge > 0; i++) {         for (j = i + 2; j <= totalnode && totaledge > 0; j++) {           printf( %d %d n , q[i], q[j]);           totaledge--;         }       }     } else {       printf( -1 );     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   struct Date {     int y, m, d;   };   const int monthDays[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};   int countLeapYears(Date d) {     int years = d.y;     if (d.m <= 2) years--;     return years / 4 - years / 100 + years / 400;   }   int getDifference(Date dt1, Date dt2) {     long int n1 = dt1.y * 365 + dt1.d;     for (int i = 0; i < dt1.m - 1; i++) n1 += monthDays[i];     n1 += countLeapYears(dt1);     long int n2 = dt2.y * 365 + dt2.d;     for (int i = 0; i < dt2.m - 1; i++) n2 += monthDays[i];     n2 += countLeapYears(dt2);     return (n2 - n1);   }   int main() {     int y1, y2, m1, m2, d1, d2;     char ch1, ch2, ch3, ch4;     int tmp;     cin >> y1 >> ch1 >> m1 >> ch2 >> d1;     cin >> y2 >> ch3 >> m2 >> ch4 >> d2;     if ((y1 > y2) || (y1 == y2 && m1 > m2) || (y1 == y2 && m1 == m2 && d1 > d2)) {       tmp = y1;       y1 = y2;       y2 = tmp;       tmp = m1;       m1 = m2;       m2 = tmp;       tmp = d1;       d1 = d2;       d2 = tmp;     }     Date dt1 = {y1, m1, d1};     Date dt2 = {y2, m2, d2};     cout << getDifference(dt1, dt2) << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const long long INF = 1e16;   long long n, a[200005], p[200005], MA = 0, z = 0;   int main() {     scanf( %lld , &n);     memset(p, 0, sizeof(p));     for (int i = 0; i < n; i++) {       scanf( %lld , &a[i]);       p[a[i]]++;       if (p[a[i]] > MA) {         MA = p[a[i]];         z = a[i];       }     }     printf( %lld n , n - MA);     for (int i = 0; i < n - 1; i++) {       if (a[i] == z) {         if (a[i + 1] < a[i]) {           printf( 1 %d %d n , i + 2, i + 1);           a[i + 1] = z;         } else if (a[i + 1] > a[i]) {           printf( 2 %d %d n , i + 2, i + 1);           a[i + 1] = z;         }       }     }     for (int i = n - 1; i >= 1; i--) {       if (a[i] == z) {         if (a[i - 1] < a[i]) {           printf( 1 %d %d n , i, i + 1);           a[i - 1] = z;         } else if (a[i - 1] > a[i]) {           printf( 2 %d %d n , i, i + 1);           a[i - 1] = z;         }       }     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   set<pair<pair<long long int, long long int>, long long int> > ev;   vector<long long int> cnt, len;   multiset<pair<long long int, long long int> > flug;   void expand(       set<pair<pair<long long int, long long int>, long long int> >::iterator it,       long long int a) {     pair<pair<long long int, long long int>, long long int> p = *it;     p.first.first += a;     cnt[p.second]++, len[p.second] += a;     auto nt = ev.insert(p).first;     ev.erase(it, nt);     auto ft = flug.upper_bound(make_pair(p.first.first, 1e18));     if (ft == flug.begin()) return;     ft--;     if (ft->first >= -p.first.second) {       long long int add = ft->second;       flug.erase(ft);       expand(nt, add);     }   }   int main() {     int n, m;     scanf( %d%d , &n, &m);     vector<pair<pair<long long int, long long int>, long long int> > g(n), f(m);     cnt = len = vector<long long int>(n, 0);     for (int((i)) = (0); ((i)) < ((n)); ((i))++) {       int x, sz;       scanf( %d%d , &x, &sz);       g[i] = make_pair(make_pair((x), (sz)), (i));       len[i] = sz;     }     for (int((i)) = (0); ((i)) < ((m)); ((i))++) {       int x, sz;       scanf( %d%d , &x, &sz);       f[i] = make_pair(make_pair((x), (sz)), (i));     }     sort((g).begin(), (g).end());     reverse((g).begin(), (g).end());     for (int((i)) = (0); ((i)) < ((n)); ((i))++) {       auto it =           ev.insert(make_pair(make_pair((g[i].first.first + g[i].first.second),                                         (-g[i].first.first)),                               (g[i].second)))               .first;       ev.erase(ev.begin(), it);     }     for (int((i)) = (0); ((i)) < ((m)); ((i))++) {       pair<pair<long long int, long long int>, long long int> fp =           make_pair(make_pair((f[i].first.first), (-1e18)), (0));       auto it = ev.lower_bound(fp);       if (it != ev.end() && -it->first.second <= f[i].first.first)         expand(it, f[i].first.second);       else         flug.insert({f[i].first.first, f[i].first.second});     }     for (int((i)) = (0); ((i)) < ((n)); ((i))++)       printf( %I64d %I64d n , cnt[i], len[i]);   }
#include <bits/stdc++.h>   using namespace std;   const long double eps = 1e-9;   const long long MOD = 1e9 + 7;   const int N = 2e6 + 10;   const long double pi = 3.1415926535897932384650288;   int n, m;   string s, t;   bool ok(int i, int j, int d) {     if (j == m) return 1;     if (i == n) return 0;     if (i < 0) return 0;     if (s[i] != t[j]) return 0;     if (d) {       return ok(i - 1, j + 1, d);     }     return ok(i + 1, j + 1, d) || ok(i - 1, j + 1, 1);   }   void solve() {     cin >> s >> t;     n = s.size();     m = t.size();     for (int i = 0; i < n; i++) {       if (ok(i, 0, 0)) {         puts( Yes );         return;       }     }     puts( No );   }   int main() {     int t = 1;     scanf( %d , &t);     while (t--) {       solve();     }   }
#include <bits/stdc++.h>   using namespace std;   int readint() {     int x = 0, f = 1;     char ch = getchar();     while (ch <  0  || ch >  9 ) {       if (ch ==  - ) f = -1;       ch = getchar();     }     while (ch >=  0  && ch <=  9 ) {       x = x * 10 + ch -  0 ;       ch = getchar();     }     return x * f;   }   long long n;   long long a[100005], d[100005][10];   char s[100005];   int main() {     n = readint();     scanf( %s , s + 1);     for (int i = 1; i <= n; i++) a[i] = readint();     memset(d, 0x3f, sizeof(d));     d[0][0] = 0;     for (int i = 1; i <= n; i++) {       if (s[i] ==  h ) {         for (int j = 0; j <= 4; j++)           if (j != 0 && j != 1) d[i][j] = d[i - 1][j];         d[i][1] = min(d[i][1], min(d[i - 1][1], d[i - 1][0]));         d[i][0] = min(d[i][0], d[i - 1][0] + a[i]);       } else if (s[i] ==  a ) {         for (int j = 0; j <= 4; j++)           if (j != 1 && j != 2) d[i][j] = d[i - 1][j];         d[i][2] = min(d[i][2], min(d[i - 1][2], d[i - 1][1]));         d[i][1] = min(d[i][1], d[i - 1][1] + a[i]);       } else if (s[i] ==  r ) {         for (int j = 0; j <= 4; j++)           if (j != 2 && j != 3) d[i][j] = d[i - 1][j];         d[i][3] = min(d[i][3], min(d[i - 1][3], d[i - 1][2]));         d[i][2] = min(d[i][2], d[i - 1][2] + a[i]);       } else if (s[i] ==  d ) {         for (int j = 0; j <= 4; j++)           if (j != 3 && j != 4) d[i][j] = d[i - 1][j];         d[i][4] = min(d[i][4], min(d[i - 1][4], d[i - 1][3]));         d[i][3] = min(d[i][3], d[i - 1][3] + a[i]);       } else {         for (int j = 0; j <= 4; j++) d[i][j] = d[i - 1][j];       }     }     long long ans = 1ll << 60;     for (int i = 0; i < 4; i++) ans = min(ans, d[n][i]);     cout << ans << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int t;     cin >> t;     while (t--) {       int arr[102];       int n;       cin >> n;       for (int i = 1; i <= n; i++) cin >> arr[i];       int myseq[102];       for (int i = 1; i <= n; i++) {         if (i & 1)           myseq[i] = arr[i + 1];         else           myseq[i] = -arr[i - 1];       }       for (int i = 1; i <= n; i++) cout << myseq[i] <<    ;       cout << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int maxn = 2005;   const long long mod = 998244353;   string s, t;   long long p[maxn], n;   long long dp[maxn][maxn];   long long ksm(long long a, long long b) {     long long r = 1;     while (b) {       if (b & 1) r = (r * a) % mod;       a = a * a % mod;       b >>= 1;     }     return r;   }   long long dfs(int l, int r) {     if (dp[l][r] != -1) return dp[l][r];     if (l >= r || l > n || r <= 0) return dp[l][r] = 0;     long long ans = 0;     if (s[l] !=  ( ) ans = (ans + dfs(l + 1, r)) % mod;     if (s[r] !=  ) ) ans = (ans + dfs(l, r - 1)) % mod;     if (s[l] !=  (  && s[r] !=  ) ) ans = (ans - dfs(l + 1, r - 1) + mod) % mod;     if (s[l] !=  )  && s[r] !=  ( )       ans = (ans + dfs(l + 1, r - 1) + ksm(2, p[r - 1] - p[l])) % mod;     return dp[l][r] = ans;   }   int main() {     s +=  # ;     cin >> t;     s += t;     n = t.size();     memset(dp, -1, sizeof(dp));     for (int i = 1; i <= n; i++) p[i] = p[i - 1] + (s[i] ==  ? );     dfs(1, n);     cout << dp[1][n] << endl;   }
#include <bits/stdc++.h>   const char en =   n ;   using namespace std;   int main() {     ios::sync_with_stdio(false);     int n;     cin >> n;     vector<long long> V(n + 47, 0);     long long ma2 = 0;     long long ma3 = 0;     vector<long long> X;     for (int i = 0; i < n; i++) {       cin >> V[i];       long long p2 = 0, p3 = 0;       long long k = V[i];       while (k % 2 == 0) {         p2++;         k /= 2;       }       while (k % 3 == 0) {         p3++;         k /= 3;       }       X.push_back(k);     }     sort(X.begin(), X.end());     if (X[0] == X[n - 1])       cout <<  Yes  << en;     else       cout <<  No  << en;   }
#include <bits/stdc++.h>   using namespace std;   const int oo = 1000000000;   const double H = sqrt(3.0) * 0.5;   const double eps = 1e-6;   int main() {     double r;     scanf( %lf , &r);     int i;     long long ans = 0;     for (i = 0; i <= r; i++) {       double x = 1.5 * i + 1.0;       if (x > r) break;       double y = sqrt(r * r - x * x);       int k;       if (i % 2)         k = (floor(y / H + eps) + 1) / 2;       else         k = floor(y / H + eps) / 2 + 1;       x = 1.5 * i + 0.5;       y = sqrt(r * r - x * x);       if (i % 2)         k = 2 * min(k, (int)floor(y / H + eps) / 2);       else         k = 2 * min(k, ((int)floor(y / H + eps) + 1) / 2) - 1;       ans += i ? 2 * k : k;     }     cout << ans << endl;   }
#include <bits/stdc++.h>   using namespace std;   void solve() {     string s;     cin >> s;     unordered_map<char, vector<long long int> > um;     long long int i;     for (i = 0; i < s.length(); i++) {       um[s[i]].push_back(i);     }     long long int last = -1;     bool flag;     string ans =   ;     for (i = 25; i >= 0; i--) {       char ch =  a  + i;       auto f = um.find(ch);       if (f != um.end()) {         for (auto it : um[ch]) {           if (it > last) {             ans += string(1, s[it]);             flag = true;           }         }         if (flag == true) last = um[ch][um[ch].size() - 1];       }       flag = false;     }     cout << ans << endl;   }   int main() {     solve();     return 0;   }
#include <bits/stdc++.h>   using namespace std;   using pii = pair<int, int>;   using ll = long long;   const int N = 3e3 + 3;   int a[N][N];   pii sum[N];   int main() {     ios_base::sync_with_stdio(false);     int n, m;     cin >> n >> m;     for (int i = 0; i < m; i++) {       for (int j = 0; j < n; j++) cin >> a[i][j];     }     vector<int> res;     int mn = 1e9;     for (int i = 0; i < n - 1; i++) {       vector<pii> ve;       int sum = 0;       for (int j = 0; j < m; j++) {         sum += a[j][i] - a[j][n - 1];         ve.push_back({a[j][i] - a[j][n - 1], j});       }       sort(ve.begin(), ve.end());       vector<int> cur;       for (int i = 0; i < m; i++) {         if (sum >= 0) break;         sum -= ve[i].first;         cur.push_back(ve[i].second);       }       if (cur.size() < mn) {         mn = cur.size();         res = cur;       }     }     cout << res.size() <<   n ;     for (auto x : res) cout << ++x <<    ;     cout <<   n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 1000005;   long long A[N], B[N], n, xx, mx;   bool check(int n, int m) {     int x = n - xx, y = mx - x;     if (x < 0 || x >= n || y < 0 || y >= m) return 0;     memset(B, 0, sizeof(B));     for (int i = 0; i < n; ++i)       for (int j = 0; j < m; ++j) ++B[abs(y - j) + abs(i - x)];     if (!memcmp(A, B, sizeof(A))) {       printf( %d %d n%d %d n , n, m, x + 1, y + 1);       return 1;     }     return 0;   }   int main() {     cin >> n;     for (int i = 1; i <= n; ++i) {       scanf( %lld , &xx);       A[xx]++;       if (xx > mx) mx = xx;     }     if (A[0] != 1) {       printf( -1 );       return 0;     }     for (xx = 1; xx <= mx; ++xx)       if (A[xx] != xx * 4) {         if (A[xx] > xx * 4) {           printf( -1 );           return 0;         } else           break;       }     for (int i = 1; i * i <= n; ++i) {       if (n % i) continue;       if (check(i, n / i)) return 0;       if (check(n / i, i)) return 0;     }     printf( -1 n );     return 0;   }
#include <bits/stdc++.h>   using namespace std;   char A[1005][1005];   int P[1000005], MinX[1000005], MinY[1000005], MaxX[1000005], MaxY[1000005],       M[1005][1005];   int xr[] = {-1, 1, 0, 0};   int xc[] = {0, 0, -1, 1};   int n, m;   void dfs(int x, int y, int i) {     if ((x < 0) || (x >= n) || (y < 0) || (y >= m) || (A[x][y] ==  . ) ||         (M[x][y]))       return;     M[x][y] = 1;     MinX[i] = min(MinX[i], x);     MinY[i] = min(MinY[i], y);     MaxX[i] = max(MaxX[i], x);     MaxY[i] = max(MaxY[i], y);     for (int j = 0; j < 4; ++j) {       dfs(x + xr[j], y + xc[j], i);     }   }   bool check_convex_island(int len) {     set<int> X, Y;     for (int i = 0; i < n; ++i) X.insert(i);     for (int i = 0; i < m; ++i) Y.insert(i);     int x, i, j, ans = 1;     for (x = 0; x < len; ++x) {       int minx = MinX[x];       int miny = MinY[x];       int maxx = MaxX[x];       int maxy = MaxY[x];       for (i = minx; i <= maxx; ++i) {         if (!X.count(i)) return false;         X.erase(i);         for (j = miny; A[i][j] ==  . ; ++j)           ;         for (; (j <= maxy) && (A[i][j] ==  # ); ++j)           ;         for (; (j <= maxy) && (A[i][j] ==  . ); ++j)           ;         ans &= (j > maxy);       }       for (j = miny; j <= maxy; ++j) {         if (!Y.count(j)) return false;         Y.erase(j);         for (i = minx; A[i][j] ==  . ; ++i)           ;         for (; (i <= maxx) && (A[i][j] ==  # ); ++i)           ;         for (; (i <= maxx) && (A[i][j] ==  . ); ++i)           ;         ans &= (i > maxx);       }     }     return ans;   }   int main() {     ios_base::sync_with_stdio(false);     cout.tie(0);     cin.tie(0);     int i, j, k, x, y, z, a, b;     for (i = 0; i <= 1000000; ++i) MinX[i] = MinY[i] = 1000000007;     cin >> n >> m;     for (i = 0; i < n; ++i) cin >> A[i];     int ans = 1, len = 0;     set<int> X, Y;     for (i = 0; i < n; ++i) X.insert(i);     for (i = 0; i < m; ++i) Y.insert(i);     for (i = 0; i < n; ++i) {       for (j = 0; j < m; ++j) {         if (A[i][j] ==  . ) continue;         if (X.find(i) != X.end()) X.erase(i);         if (Y.find(j) != Y.end()) Y.erase(j);         if (!M[i][j]) dfs(i, j, len++);       }     }     ans &= ((X.size() == 0 && Y.size() == 0) || (X.size() && Y.size()));     ans &= check_convex_island(len);     if (ans)       cout << len << endl;     else       cout << -1 << endl;     return 0;   }