using System; using System.Linq; public class Test { public static void Main() { // your code goes here var snake=" tSyrep \n" + " r p \n" + " in Sli \n" + " g Sile\n" + " Snakes n\n" + "Ser ylt\n" + "a eh ilS \n" + "fe w t \n" + " emo h \n" + " Sre "; var result = new Test().f(snake); Console.WriteLine(result); } string f(string s){int o=-~s.IndexOf('\n'),m=99;var r=new string(' ',o);(s=r+s+r).Select((c,i)=>{int n=2,e=0,p,w=i%o+i/o;if(c>' '&w(s[i+d]>' '?(e=d)*--n:n)>0))for(m=w,r=""+c+s[p=i+e];new[]{e,o/e,-o/e}.Any(d=>s[p+(e=d)]>' ');)r+=s[p+=e];return i;}).Max();return r;} }