HTML & JS
#include<bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define fastread ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define openfile ifstream cin; ofstream cout; cin.open("input.txt"); cout.open("output.txt");
#define f(i, x, y) for(int i = x; i < y; i++)
#define all(X) X.begin(), X.end()
#define int long long
#define key pair<int, int>
#define keyd pair<double, double>
#define ff first
#define ss second
#define double long double
int mod = 1e9+7;
const int inf = 1e18;
using namespace std;

struct node
{
    int x, y, l;
};

main()
{
	fastread;
	int n, m; cin>>n>>m; char mat[n+1][m+1];
	f(i, 1, n+1)
	{
	    string s; cin>>s;
	    f(j, 0, s.length()) mat[i][j+1] = s[j];
	}
	int dp[4][n+2][m+2]; memset(dp, 0, sizeof(dp));
	f(i, 1, n+1)
	{
	    f(j, 1, m+1)
	        if(mat[i][j] == '*') dp[0][i][j] = dp[0][i][j-1] + 1;
	    for(int j = m; j > 0; j--)
	        if(mat[i][j] == '*') dp[1][i][j] = dp[1][i][j+1] + 1;
	    
	}
	
	f(j, 1, m+1)
	{
	    f(i, 1, n+1)
	        if(mat[i][j] == '*') dp[2][i][j] = dp[2][i-1][j] + 1;
	    for(int i = n; i > 0; i--)
	        if(mat[i][j] == '*') dp[3][i][j] = dp[3][i+1][j] + 1;
	}
	
	int tab[n+1][m+1];
	
	int dph[n+2][m+2], dpv[n+2][m+2];
	memset(dph, 0, sizeof(dph)); memset(dpv, 0, sizeof(dpv));
	
	f(i, 1, n+1)
	{
	    f(j, 1, m+1)
	    {
	        int t = min(dp[2][i-1][j], min(dp[3][i+1][j], min(dp[0][i][j-1], dp[1][i][j+1])));
	        if(t > 0){ dph[i][j-t] += 1; dph[i][j+t+1] += -1; }
	        tab[i][j] = t;
	    }
	    int cnt = 0;
	    f(j, 1, m+1)
	    {
	        cnt += dph[i][j]; dph[i][j] = cnt;
	    }
	}
	
	f(j, 1, m+1)
	{
	    f(i, 1, n+1)
	    {
	        int t = tab[i][j];
	        if(t > 0){ dpv[i-t][j] += 1; dpv[i+t+1][j] += -1; }
	    }
	    int cnt = 0;
	    f(i, 1, n+1)
	    {
	        cnt += dpv[i][j]; dpv[i][j] = cnt;
	    }
	}
	
// 	f(k, 0, 4) { f(i, 1, n+1){ f(j, 1, m+1) cout<<dp[k][i][j]<<" "; cout<<"\n"; } cout<<"\n"; } cout<<"\n";
//     f(i, 1, n+1){ f(j, 1, m+1) cout<<tab[i][j]<<" "; cout<<"\n"; } cout<<"\n";
//     f(i, 1, n+1){ f(j, 1, m+1) cout<<dph[i][j]<<" "; cout<<"\n"; } cout<<"\n";
//     f(i, 1, n+1){ f(j, 1, m+1) cout<<dpv[i][j]<<" "; cout<<"\n"; } cout<<"\n";
    
	vector<node> v;
	
	f(i, 1, n+1)
	    f(j, 1, m+1)
	    {
	        if(mat[i][j] == '*' && dph[i][j] == 0 && dpv[i][j] == 0)
	        {
	            cout<<"-1"; return 0;
	        }
	        if(tab[i][j] > 0) v.pb({i, j, tab[i][j]});
	    }
	
	cout<<v.size()<<"\n";
	for(node i : v) cout<<i.x<<" "<<i.y<<" "<<i.l<<"\n";
}

Generated URL:

Output:

4
2 2 1
2 6 1
6 2 1
6 6 1
Report Bug