A star is a figure of the following type: an asterisk character '*' in the center of the figure and four rays (to the left, right, top, bottom) of the same positive length. The size of a star is the length of its rays. The size of a star must be a positive number (i.e. rays of length $0$ are not allowed).

Let's consider empty cells are denoted by '.', then the following figures are stars:

 [Image] The leftmost figure is a star of size $1$, the middle figure is a star of size $2$ and the rightmost figure is a star of size $3$. 

You are given a rectangular grid of size $n \times m$ consisting only of asterisks '*' and periods (dots) '.'. Rows are numbered from $1$ to $n$, columns are numbered from $1$ to $m$. Your task is to draw this grid using any number of stars or find out that it is impossible. Stars can intersect, overlap or even coincide with each other. The number of stars in the output can't exceed $n \cdot m$. Each star should be completely inside the grid. You can use stars of same and arbitrary sizes.

In this problem, you do not need to minimize the number of stars. Just find any way to draw the given grid with at most $n \cdot m$ stars.


-----Input-----

The first line of the input contains two integers $n$ and $m$ ($3 \le n, m \le 1000$) — the sizes of the given grid.

The next $n$ lines contains $m$ characters each, the $i$-th line describes the $i$-th row of the grid. It is guaranteed that grid consists of characters '*' and '.' only.


-----Output-----

If it is impossible to draw the given grid using stars only, print "-1".

Otherwise in the first line print one integer $k$ ($0 \le k \le n \cdot m$) — the number of stars needed to draw the given grid. The next $k$ lines should contain three integers each — $x_j$, $y_j$ and $s_j$, where $x_j$ is the row index of the central star character, $y_j$ is the column index of the central star character and $s_j$ is the size of the star. Each star should be completely inside the grid.


-----Examples-----
Input
6 8
....*...
...**...
..*****.
...**...
....*...
........

Output
3
3 4 1
3 5 2
3 5 1

Input
5 5
.*...
****.
.****
..**.
.....

Output
3
2 2 1
3 3 1
3 4 1

Input
5 5
.*...
***..
.*...
.*...
.....

Output
-1

Input
3 3
*.*
.*.
*.*

Output
-1



-----Note-----

In the first example the output 2

3 4 1

3 5 2



is also correct.
[{"code": "n,m=map(int,input().split())\ng=[[*input()] for _ in range(n)]\nc=[[0 for _ in range(m)] for _ in range(n)]\nfor i in range(n):\n v=0\n for j in range(m):\n  v=(v+1)*(g[i][j]=='*')\n  c[i][j]=v\n v=0\n for j in range(m-1,-1,-1):\n  v=(v+1)*(g[i][j]=='*')\n  c[i][j]=min(c[i][j],v)\nfor j in range(m):\n v=0\n for i in range(n):\n  v=(v+1)*(g[i][j]=='*')\n  c[i][j]=min(c[i][j],v)\n v=0\n for i in range(n-1,-1,-1):\n  v=(v+1)*(g[i][j]=='*')\n  c[i][j]=min(c[i][j],v)\nfor i in range(n):\n for j in range(m):\n  if c[i][j]==1: c[i][j]=0\nfor i in range(n):\n v=0\n for j in range(m):\n  v=max(v-1,c[i][j])\n  if v:g[i][j]='.'\n v=0\n for j in range(m-1,-1,-1):\n  v=max(v-1,c[i][j])\n  if v:g[i][j]='.'\nfor j in range(m):\n v=0\n for i in range(n):\n  v=max(v-1,c[i][j])\n  if v:g[i][j]='.'\n for i in range(n-1,-1,-1):\n  v=max(v-1,c[i][j])\n  if v:g[i][j]='.'\nif all(g[i][j]=='.' for i in range(n) for j in range(m)):\n r=[(i+1,j+1,c[i][j]-1) for i in range(n) for j in range(m) if c[i][j]]\n print(len(r))\n for t in r: print(*t)\nelse:\n print(-1)", "passed": true, "time": 0.15, "memory": 14456.0, "status": "done"}, {"code": "import sys\nn,m=list(map(int,input().split()))\ns=[list(input()) for i in range(n)]\nu=[[-1 for i in range(m)] for j in range(n)]\nd=[[-1 for i in range(m)] for j in range(n)]\nl=[[-1 for i in range(m)] for j in range(n)]\nr=[[-1 for i in range(m)] for j in range(n)]\nfor i in range(m):\n    acum=0\n    for j in range(n):\n        if s[j][i]==\".\":\n            acum=0\n        else:\n            acum+=1\n        u[j][i]=acum\nfor i in range(m):\n    acum=0\n    for j in range(n-1,-1,-1):\n        if s[j][i]==\".\":\n            acum=0\n        else:\n            acum+=1\n        d[j][i]=acum\nfor i in range(n):\n    acum=0\n    for j in range(m):\n        if s[i][j]==\".\":\n            acum=0\n        else:\n            acum+=1\n        l[i][j]=acum\nfor i in range(n):\n    acum=0\n    for j in range(m-1,-1,-1):\n        if s[i][j]==\".\":\n            acum=0\n        else:\n            acum+=1\n        r[i][j]=acum\nans=[]\nt1=[[0 for i in range(m)] for j in range(n)]\nt2=[[0 for i in range(m)] for j in range(n)]\nfor i in range(n):\n    for j in range(m):\n        d1=min(l[i][j],r[i][j],u[i][j],d[i][j])-1\n        if d1>0:\n            ans.append([i+1,j+1,d1])\n            t1[i+d1][j]+=1\n            t1[i-d1][j]-=1\n            t2[i][j-d1]+=1\n            t2[i][j+d1]-=1\ndp=[['.' for i in range(m)] for j in range(n)]\nfor i in range(n):\n    acum=0\n    for j in range(m):\n        acum+=t2[i][j]\n        if acum!=0 or t2[i][j]!=0:\n            dp[i][j]='*'\nfor i in range(m):\n    acum=0\n    for j in range(n):\n        acum+=t1[j][i]\n        if acum!=0 or t1[j][i]!=0 :\n            dp[j][i]='*'           \nif dp!=s:\n    print(-1)\n    return\nprint(len(ans))\nfor i in ans:\n    print(*i)\n        \n        \n", "passed": true, "time": 0.16, "memory": 14468.0, "status": "done"}, {"code": "import sys\ninput = sys.stdin.readline\n\nn,m=list(map(int,input().split()))\nMAP=[list(input().strip()) for i in range(n)]\n\nT0=[[0]*(m+1) for i in range(n+1)]\nT1=[[0]*(m+1) for i in range(n+1)]\nY0=[[0]*(m+1) for i in range(n+1)]\nY1=[[0]*(m+1) for i in range(n+1)]\n\nfor i in range(n):\n    for j in range(m):\n        if MAP[i][j]==\"*\":\n            T0[i][j]=T0[i-1][j]+1\n            Y0[i][j]=Y0[i][j-1]+1\n\nfor i in range(n-1,-1,-1):\n    for j in range(m-1,-1,-1):\n        if MAP[i][j]==\"*\":\n            T1[i][j]=T1[i+1][j]+1\n            Y1[i][j]=Y1[i][j+1]+1\n\nANS=[[0]*m for i in range(n)]\n\nfor i in range(n):\n    for j in range(m):\n        score=min(T0[i][j],T1[i][j],Y0[i][j],Y1[i][j])\n        if score>=2:\n            ANS[i][j]=score\n\nT0=[[0]*(m+1) for i in range(n+1)]\nT1=[[0]*(m+1) for i in range(n+1)]\nY0=[[0]*(m+1) for i in range(n+1)]\nY1=[[0]*(m+1) for i in range(n+1)]\n\nfor i in range(n):\n    for j in range(m):\n        T0[i][j]=max(ANS[i][j],T0[i-1][j]-1)\n        Y0[i][j]=max(ANS[i][j],Y0[i][j-1]-1)\n\nfor i in range(n-1,-1,-1):\n    for j in range(m-1,-1,-1):\n        T1[i][j]=max(ANS[i][j],T1[i+1][j]-1)\n        Y1[i][j]=max(ANS[i][j],Y1[i][j+1]-1)\n\n\n\nSUF=[[\".\"]*m for i in range(n)]\nfor i in range(n):\n    for j in range(m):\n        if T0[i][j] or T1[i][j] or Y0[i][j] or Y1[i][j]:\n            SUF[i][j]=\"*\"\n\nif SUF!=MAP:\n    print(-1)\nelse:\n    ANSLIST=[]\n    for i in range(n):\n        for j in range(m):\n            if ANS[i][j]!=0:\n                ANSLIST.append((i+1,j+1,ANS[i][j]-1))\n\n    print(len(ANSLIST))\n    for ans in ANSLIST:\n        print(*ans)\n", "passed": true, "time": 0.55, "memory": 14300.0, "status": "done"}, {"code": "# -*- coding: utf-8 -*-\n\nimport sys\nfrom itertools import accumulate\n\ndef input(): return sys.stdin.readline().strip()\ndef list2d(a, b, c): return [[c] * b for i in range(a)]\ndef list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]\ndef list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]\ndef ceil(x, y=1): return int(-(-x // y))\ndef INT(): return int(input())\ndef MAP(): return map(int, input().split())\ndef LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]\ndef Yes(): print('Yes')\ndef No(): print('No')\ndef YES(): print('YES')\ndef NO(): print('NO')\nINF = 10 ** 18\nMOD = 10 ** 9 + 7\n\ndef build_grid(H, W, intv, _type, space=True, padding=False):\n    if space:\n        _input = lambda: input().split()\n    else:\n        _input = lambda: input()\n    _list = lambda: list(map(_type, _input()))\n    if padding:\n        offset = 1\n    else:\n        offset = 0\n    grid = list2d(H+offset*2, W+offset*2, intv)\n    for i in range(offset, H+offset):\n        row = _list()\n        for j in range(offset, W+offset):\n            grid[i][j] = row[j-offset]\n    return grid\n\nH, W = MAP()\ngrid = build_grid(H, W, '#', str, space=0, padding=1)\n\nans = []\nimosw = list2d(H+2, W+2, 0)\nimosh = list2d(H+2, W+2, 0)\ndef check(i, j):\n    sz = min(L[i][j], R[i][j], U[i][j], D[i][j])\n    if sz > 1:\n        imosw[i][j-sz+1] += 1\n        imosw[i][j+sz] -= 1\n        imosh[i-sz+1][j] += 1\n        imosh[i+sz][j] -= 1\n        ans.append((i, j, sz-1))\n\ndef check2():\n    for i in range(1, H+1):\n        for j in range(1, W+1):\n            if grid[i][j] == '*' and not imosw[i][j] and not imosh[i][j]:\n                return False\n    return True\n\nL = list2d(H+2, W+2, 0)\nR = list2d(H+2, W+2, 0)\nU = list2d(H+2, W+2, 0)\nD = list2d(H+2, W+2, 0)\nfor i in range(1, H+1):\n    for j in range(1, W+1):\n        if grid[i][j] == '.':\n            L[i][j] = 0\n        else:\n            L[i][j] = L[i][j-1] + 1\nfor i in range(1, H+1):\n    for j in range(W, 0, -1):\n        if grid[i][j] == '.':\n            R[i][j] = 0\n        else:\n            R[i][j] = R[i][j+1] + 1\nfor j in range(1, W+1):\n    for i in range(1, H+1):\n        if grid[i][j] == '.':\n            U[i][j] = 0\n        else:\n            U[i][j] = U[i-1][j] + 1\nfor j in range(1, W+1):\n    for i in range(H, 0, -1):\n        if grid[i][j] == '.':\n            D[i][j] = 0\n        else:\n            D[i][j] = D[i+1][j] + 1\n\nfor i in range(1, H+1):\n    for j in range(1, W+1):\n        if grid[i][j] == '*':\n            check(i, j)\nfor i in range(1, H+1):\n    for j in range(W+1):\n        imosw[i][j+1] += imosw[i][j]\nfor j in range(1, W+1):\n    for i in range(H+1):\n        imosh[i+1][j] += imosh[i][j]\n\nif check2():\n    print(len(ans))\n    [print(h, w, sz) for h, w, sz in ans]\nelse:\n    print(-1)\n", "passed": true, "time": 0.14, "memory": 14400.0, "status": "done"}, {"code": "n, m = list(map(int, input().split()))\nc = []\nfor j in range(n):\n    d = []\n    s = input()\n    for i in s:\n        d.append(i)\n    c.append(d)\na = []\nb = []\ne=[]\ng=[]\n\nfor j in range(n):\n    k=[0]*(m)\n    e.append(k)\nfor j in range(n):\n    k=[0]*(m)\n    g.append(k)\n\ndpu = []\nfor j in range(n):\n    k=[0]*(m)\n    dpu.append(k)\ndpd = []\nfor j in range(n):\n    k=[0]*(m)\n    dpd.append(k)\ndpl = []\nfor j in range(n):\n    k=[0]*(m)\n    dpl.append(k)\ndpr = []\nfor j in range(n):\n    k=[0]*(m)\n    dpr.append(k)\nfor i in range(n):\n    for j in range(m):\n        if c[i][j] == \"*\":\n            if i>0:\n                dpu[i][j]+=dpu[i-1][j]+1\n            else:\n                dpu[i][j]=1\n            if j>0:\n                dpl[i][j]=dpl[i][j-1]+1\n            else:\n                dpl[i][j]=1\ni=n-1\nwhile(i>=0):\n    j=m-1\n    while(j>=0):\n        if c[i][j] == \"*\":\n            if i<(n-1):\n                dpd[i][j] += dpd[i + 1][j] + 1\n            else:\n                dpd[i][j] = 1\n            if j<(m-1):\n                dpr[i][j] = dpr[i][j + 1] + 1\n            else:\n                dpr[i][j] = 1\n        j+=-1\n    i+=-1\nfor i in range(1,n-1):\n    for j in range(1,m-1):\n        if c[i][j] == \"*\":\n            k=min(dpd[i][j]-1,dpu[i][j]-1,dpr[i][j]-1,dpl[i][j]-1)\n            if k==0:\n                pass\n            elif k>0:\n                a.append([i+1,j+1,k])\n                e[i-k][j]+=1\n                if (i+k)<(n-1):\n                    e[i+k+1][j]+=-1\n                g[i][j-k] += 1\n                if (j + k) < (m - 1):\n                    g[i][j+k+1] += -1\nfor i in range(m):\n    for j in range(1,n):\n        if c[j-1][i]==\"*\":\n            e[j][i]+=e[j-1][i]\nfor i in range(n):\n    for j in range(1,m):\n        if c[i][j-1]==\"*\":\n            g[i][j]+=g[i][j-1]\nf=0\nfor i in range(n):\n    for j in range(m):\n        if c[i][j]==\"*\" and e[i][j]<=0 and g[i][j]<=0:\n            f=1\n            break\nif f==1:\n    print(-1)\nelse:\n    print(len(a))\n    for j in a:\n        print(*j)\n\n\n\n\n\n\n\n\n\n\n\n\n", "passed": true, "time": 0.14, "memory": 14332.0, "status": "done"}, {"code": "\nn, m = list(map(int, input().split()))\nc = []\nfor j in range(n):\n    d = []\n    s = input()\n    for i in s:\n        d.append(i)\n    c.append(d)\na = []\nb = []\ne=[]\ng=[]\n\nfor j in range(n):\n    k=[0]*(m)\n    e.append(k)\nfor j in range(n):\n    k=[0]*(m)\n    g.append(k)\n\ndpu = []\nfor j in range(n):\n    k=[0]*(m)\n    dpu.append(k)\ndpd = []\nfor j in range(n):\n    k=[0]*(m)\n    dpd.append(k)\ndpl = []\nfor j in range(n):\n    k=[0]*(m)\n    dpl.append(k)\ndpr = []\nfor j in range(n):\n    k=[0]*(m)\n    dpr.append(k)\nfor i in range(n):\n    for j in range(m):\n        if c[i][j] == \"*\":\n            if i>0:\n                dpu[i][j]+=dpu[i-1][j]+1\n            else:\n                dpu[i][j]=1\n            if j>0:\n                dpl[i][j]=dpl[i][j-1]+1\n            else:\n                dpl[i][j]=1\ni=n-1\nwhile(i>=0):\n    j=m-1\n    while(j>=0):\n        if c[i][j] == \"*\":\n            if i<(n-1):\n                dpd[i][j] += dpd[i + 1][j] + 1\n            else:\n                dpd[i][j] = 1\n            if j<(m-1):\n                dpr[i][j] = dpr[i][j + 1] + 1\n            else:\n                dpr[i][j] = 1\n        j+=-1\n    i+=-1\nfor i in range(1,n-1):\n    for j in range(1,m-1):\n        if c[i][j] == \"*\":\n            k=min(dpd[i][j]-1,dpu[i][j]-1,dpr[i][j]-1,dpl[i][j]-1)\n            if k==0:\n                pass\n            elif k>0:\n                a.append([i+1,j+1,k])\n                e[i-k][j]+=1\n                if (i+k)<(n-1):\n                    e[i+k+1][j]+=-1\n                g[i][j-k] += 1\n                if (j + k) < (m - 1):\n                    g[i][j+k+1] += -1\nfor i in range(m):\n    for j in range(1,n):\n        if c[j-1][i]==\"*\":\n            e[j][i]+=e[j-1][i]\nfor i in range(n):\n    for j in range(1,m):\n        if c[i][j-1]==\"*\":\n            g[i][j]+=g[i][j-1]\nf=0\nfor i in range(n):\n    for j in range(m):\n        if c[i][j]==\"*\" and e[i][j]<=0 and g[i][j]<=0:\n            f=1\n            break\nif f==1:\n    print(-1)\nelse:\n    print(len(a))\n    for j in a:\n        print(*j)\n\n\n\n\n\n\n\n\n\n\n\n\n", "passed": true, "time": 0.14, "memory": 14520.0, "status": "done"}, {"code": "n,m = map(int,input().split())\nmat = []\n\nfor i in range(n):\n    mat.append(list(map(int,input().replace(\"*\",\"1\").replace(\".\",\"0\"))))\n\nver,hor = [[0 for i in range(m)] for j in range(n)],[[0 for i in range(m)] for j in range(n)]\n\ndp = [[[0 for i in range(4)]for j in range(m) ] for k in range(n)]\nfor i in range(1,n):\n    for j in range(1,m):\n        x,y = n-i-1,m-j-1\n        if mat[i][j]==1:\n            dp[i][j][0] = max(dp[i][j-1][0],mat[i][j-1]) + 1\n            dp[i][j][1] = max(dp[i-1][j][1],mat[i-1][j]) + 1\n        if mat[x][y]==1:\n            dp[x][y][2] = max(dp[x][y+1][2],mat[x][y+1]) + 1\n            dp[x][y][3] = max(dp[x+1][y][3],mat[x+1][y]) + 1\n\nstars = []\nfor i in range(1,n-1):\n    for j in range(1,m-1):\n        if mat[i][j]==1:\n            s = min(dp[i][j])-1\n            if s>0:\n                stars.append((i+1,j+1,s))\n                ver[i-s][j]+=1\n                if i+s+1<n:\n                    ver[i+s+1][j] -= 1\n                hor[i][j-s]+=1\n                if j+s+1<m:\n                    hor[i][j+s+1] -= 1\n\nfor i in range(1,n):\n    for j in range(1,m):\n        ver[i][j] += ver[i-1][j]\n        hor[i][j] += hor[i][j-1]\n\nchk = True\nfor i in range(n):\n    for j in range(m):\n        if mat[i][j] and max(ver[i][j],hor[i][j])<=0:\n            chk=False\n            break\n\nif chk:\n    print(len(stars))\n    for i in stars:\n        print(*i)\nelse:\n    print(-1)", "passed": true, "time": 0.14, "memory": 14532.0, "status": "done"}, {"code": "import sys\ninput=lambda:sys.stdin.readline().rstrip()\n\nh,w=map(int,input().split())\ns=[list(\".\"*(w+2))]+[list(\".\"+input()+\".\") for _ in range(h)]+[list(\".\"*(w+2))]\nb=[[0]*(w+2)for _ in range(h+2)]\nc=[[0]*(w+2)for _ in range(h+2)]\nfor i in range(1,h+2):\n  for j in range(1,w+2):\n    if s[i][j]==\"*\":\n      b[i][j]=b[i-1][j]+1\n      c[i][j]=c[i][j-1]+1\nfor i in range(h,-1,-1):\n  for j in range(w,-1,-1):\n    if s[i][j]==\"*\":\n      b[i][j]=min(b[i][j],b[i+1][j]+1)\n      c[i][j]=min(c[i][j],c[i][j+1]+1)\nans=[]\nfor i in range(1,h+1):\n  for j in range(1,w+1):\n    t=min(b[i][j],c[i][j])-1\n    if t>0:\n      ans.append((i,j,t))\nb=[[0]*(w+2)for _ in range(h+2)]\nc=[[0]*(w+2)for _ in range(h+2)]\nfor i,j,t in ans:\n  b[i-t][j]+=1\n  b[i+t+1][j]-=1\n  c[i][j-t]+=1\n  c[i][j+t+1]-=1\nfor i in range(h+1):\n  for j in range(w+1):\n    b[i+1][j]+=b[i][j]\n    c[i][j+1]+=c[i][j]\n    if i!=0 and j!=0:\n      if (b[i][j]+c[i][j]>0)!=(s[i][j]==\"*\"):\n        print(-1)\n        return\n\nprint(len(ans))\nfor i in ans:print(*i)", "passed": true, "time": 0.15, "memory": 14480.0, "status": "done"}, {"code": "def main():\n\n    n,m = list(map(int, input().split()))\n    S = [list(str(input())) for _ in range(n)]\n\n    L = [[0]*m for _ in range(n)]\n    R = [[0]*m for _ in range(n)]\n    U = [[0]*m for _ in range(n)]\n    D = [[0]*m for _ in range(n)]\n\n    for i in range(n):\n        cnt = 0\n        for j in range(m):\n            if S[i][j] == '.':\n                cnt = 0\n            else:\n                cnt += 1\n                L[i][j] = cnt\n        cnt = 0\n        for j in reversed(list(range(m))):\n            if S[i][j] == '.':\n                cnt = 0\n            else:\n                cnt += 1\n                R[i][j] = cnt\n\n    for j in range(m):\n        cnt = 0\n        for i in range(n):\n            if S[i][j] == '.':\n                cnt = 0\n            else:\n                cnt += 1\n                U[i][j] = cnt\n        cnt = 0\n        for i in reversed(list(range(n))):\n            if S[i][j] == '.':\n                cnt = 0\n            else:\n                cnt += 1\n                D[i][j] = cnt\n\n    #print(L)\n    #print(R)\n    #print(U)\n    #print(D)\n    #T = [['.']*m for _ in range(n)]\n    imosH = [[0]*(m+1) for _ in range(n)]\n    imosV = [[0]*m for _ in range(n+1)]\n    ans = []\n    for i in range(1, n-1):\n        for j in range(1, m-1):\n            if S[i][j] == '.':\n                continue\n            l = L[i][j]-1\n            r = R[i][j]-1\n            u = U[i][j]-1\n            d = D[i][j]-1\n            s = min([l, r, u, d])\n            if s == 0:\n                continue\n            ans.append((i+1, j+1, s))\n            imosV[i-s][j] += 1\n            imosV[i+s+1][j] -= 1\n            imosH[i][j-s] += 1\n            imosH[i][j+s+1] -= 1\n\n    #print(imosH)\n    #print(imosV)\n\n    from itertools import accumulate\n    for i in range(n):\n        imosH[i] = list(accumulate(imosH[i]))\n    for j in range(m):\n        for i in range(1, n+1):\n            imosV[i][j] += imosV[i-1][j]\n\n    #print(imosH)\n    #print(imosV)\n\n    #print(T)\n    for i in range(n):\n        for  j in range(m):\n            if S[i][j] == '*':\n                if imosH[i][j] <= 0 and imosV[i][j] <= 0:\n                    print(-1)\n                    return\n    else:\n        print(len(ans))\n        for i in range(len(ans)):\n            print(*ans[i])\n\ndef __starting_point():\n    main()\n\n__starting_point()", "passed": true, "time": 0.14, "memory": 14336.0, "status": "done"}]